Fix sql error in FixDanglingStateExecution (#18411)

Fix sql error in fixDanglingStateExecution

  fixes #18408

Signed-off-by: stonezdj <daojunz@vmware.com>
This commit is contained in:
stonezdj(Daojun Zhang) 2023-03-28 12:57:02 +08:00 committed by GitHub
parent 85bcbbaf62
commit 088d18bccf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 5 deletions

View File

@ -200,11 +200,10 @@ func (sm *sweepManager) FixDanglingStateExecution(ctx context.Context) error {
ELSE 'Success' ELSE 'Success'
END END
WHERE status = 'Running' 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')` 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, config.MaxDanglingHour()).Exec()
_, err = ormer.Raw(sql, intervalStr).Exec()
if err != nil { if err != nil {
return errors.Wrap(err, "failed to fix dangling state execution") return errors.Wrap(err, "failed to fix dangling state execution")
} }

View File

@ -19,14 +19,16 @@ import (
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/suite"
"github.com/goharbor/harbor/src/lib/errors" "github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/pkg/task/dao" "github.com/goharbor/harbor/src/pkg/task/dao"
htesting "github.com/goharbor/harbor/src/testing"
"github.com/goharbor/harbor/src/testing/mock" "github.com/goharbor/harbor/src/testing/mock"
"github.com/stretchr/testify/suite"
) )
type sweepManagerTestSuite struct { type sweepManagerTestSuite struct {
suite.Suite htesting.Suite
execDao *mockExecutionDAO execDao *mockExecutionDAO
mgr *sweepManager mgr *sweepManager
} }
@ -36,6 +38,7 @@ func TestSweepManager(t *testing.T) {
} }
func (suite *sweepManagerTestSuite) SetupSuite() { func (suite *sweepManagerTestSuite) SetupSuite() {
suite.Suite.SetupSuite()
suite.execDao = &mockExecutionDAO{} suite.execDao = &mockExecutionDAO{}
suite.mgr = &sweepManager{execDAO: suite.execDao} suite.mgr = &sweepManager{execDAO: suite.execDao}
} }
@ -54,3 +57,8 @@ func (suite *sweepManagerTestSuite) TestGetCandidateMaxStartTime() {
suite.NoError(err, "should not got error") suite.NoError(err, "should not got error")
suite.Equal(now.String(), startTime.String()) 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")
}