diff --git a/src/jobservice/runtime/bootstrap.go b/src/jobservice/runtime/bootstrap.go index 8ecafd56d..aa87c736f 100644 --- a/src/jobservice/runtime/bootstrap.go +++ b/src/jobservice/runtime/bootstrap.go @@ -17,14 +17,16 @@ package runtime import ( "context" "fmt" - "github.com/goharbor/harbor/src/jobservice/mgt" - "github.com/goharbor/harbor/src/jobservice/migration" "os" "os/signal" "sync" "syscall" "time" + "github.com/goharbor/harbor/src/jobservice/mgt" + "github.com/goharbor/harbor/src/jobservice/migration" + "github.com/goharbor/harbor/src/pkg/retention" + "github.com/goharbor/harbor/src/jobservice/api" "github.com/goharbor/harbor/src/jobservice/common/utils" "github.com/goharbor/harbor/src/jobservice/config" @@ -243,6 +245,7 @@ func (bs *Bootstrap) loadAndRunRedisWorkerPool( job.ImageGC: (*gc.GarbageCollector)(nil), job.Replication: (*replication.Replication)(nil), job.ReplicationScheduler: (*replication.Scheduler)(nil), + job.Retention: (*retention.Job)(nil), }); err != nil { // exit return nil, err diff --git a/src/pkg/retention/job.go b/src/pkg/retention/job.go index 85c2c05ec..ccd7f10a2 100644 --- a/src/pkg/retention/job.go +++ b/src/pkg/retention/job.go @@ -16,6 +16,7 @@ package retention import ( "encoding/json" + "github.com/goharbor/harbor/src/jobservice/job" "github.com/goharbor/harbor/src/jobservice/logger" "github.com/goharbor/harbor/src/pkg/retention/dep" @@ -63,6 +64,12 @@ func (pj *Job) Run(ctx job.Context, params job.Parameters) error { repo, _ := getParamRepo(params) liteMeta, _ := getParamMeta(params) + // Stop check point 1: + if isStopped(ctx) { + logStop(myLogger) + return nil + } + // Retrieve all the candidates under the specified repository allCandidates, err := pj.client.GetCandidates(repo) if err != nil { @@ -76,6 +83,12 @@ func (pj *Job) Run(ctx job.Context, params job.Parameters) error { return logError(myLogger, err) } + // Stop check point 2: + if isStopped(ctx) { + logStop(myLogger) + return nil + } + // Run the flow results, err := processor.Process(allCandidates) if err != nil { @@ -95,6 +108,17 @@ func (pj *Job) Run(ctx job.Context, params job.Parameters) error { return nil } +func isStopped(ctx job.Context) (stopped bool) { + cmd, ok := ctx.OPCommand() + stopped = ok && cmd == job.StopCommand + + return +} + +func logStop(logger logger.Interface) { + logger.Info("Retention job is stopped") +} + func logError(logger logger.Interface, err error) error { wrappedErr := errors.Wrap(err, "retention job") logger.Error(wrappedErr)