From 088d18bccfe32a8efbd6f7e39509f6942466c2ea Mon Sep 17 00:00:00 2001 From: "stonezdj(Daojun Zhang)" Date: Tue, 28 Mar 2023 12:57:02 +0800 Subject: [PATCH] Fix sql error in FixDanglingStateExecution (#18411) Fix sql error in fixDanglingStateExecution fixes #18408 Signed-off-by: stonezdj --- src/pkg/task/sweep_manager.go | 5 ++--- src/pkg/task/sweep_manager_test.go | 12 ++++++++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/pkg/task/sweep_manager.go b/src/pkg/task/sweep_manager.go index 135b63d06..9e387072e 100644 --- a/src/pkg/task/sweep_manager.go +++ b/src/pkg/task/sweep_manager.go @@ -200,11 +200,10 @@ func (sm *sweepManager) FixDanglingStateExecution(ctx context.Context) error { ELSE 'Success' END WHERE status = 'Running' - AND start_time < now() - INTERVAL ? + AND EXTRACT(epoch FROM NOW() - start_time)/3600 > ? AND NOT EXISTS (SELECT 1 FROM task WHERE execution_id = execution.id AND status = 'Running')` - intervalStr := fmt.Sprintf("%d hour", config.MaxDanglingHour()) - _, err = ormer.Raw(sql, intervalStr).Exec() + _, err = ormer.Raw(sql, config.MaxDanglingHour()).Exec() if err != nil { return errors.Wrap(err, "failed to fix dangling state execution") } diff --git a/src/pkg/task/sweep_manager_test.go b/src/pkg/task/sweep_manager_test.go index 172404ecd..52b4eeddb 100644 --- a/src/pkg/task/sweep_manager_test.go +++ b/src/pkg/task/sweep_manager_test.go @@ -19,14 +19,16 @@ import ( "testing" "time" + "github.com/stretchr/testify/suite" + "github.com/goharbor/harbor/src/lib/errors" "github.com/goharbor/harbor/src/pkg/task/dao" + htesting "github.com/goharbor/harbor/src/testing" "github.com/goharbor/harbor/src/testing/mock" - "github.com/stretchr/testify/suite" ) type sweepManagerTestSuite struct { - suite.Suite + htesting.Suite execDao *mockExecutionDAO mgr *sweepManager } @@ -36,6 +38,7 @@ func TestSweepManager(t *testing.T) { } func (suite *sweepManagerTestSuite) SetupSuite() { + suite.Suite.SetupSuite() suite.execDao = &mockExecutionDAO{} suite.mgr = &sweepManager{execDAO: suite.execDao} } @@ -54,3 +57,8 @@ func (suite *sweepManagerTestSuite) TestGetCandidateMaxStartTime() { suite.NoError(err, "should not got error") suite.Equal(now.String(), startTime.String()) } + +func (suite *sweepManagerTestSuite) Test_sweepManager_FixDanglingStateExecution() { + err := suite.mgr.FixDanglingStateExecution(suite.Context()) + suite.NoError(err, "should not got error") +}