Merge pull request #8367 from steven-zou/feature/tag_retention-job-enhancement

support stop in the retention job and register it
This commit is contained in:
Wenkai Yin(尹文开) 2019-07-23 13:06:44 +08:00 committed by GitHub
commit 8a96f39caa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 2 deletions

View File

@ -17,14 +17,16 @@ package runtime
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/goharbor/harbor/src/jobservice/mgt"
"github.com/goharbor/harbor/src/jobservice/migration"
"os" "os"
"os/signal" "os/signal"
"sync" "sync"
"syscall" "syscall"
"time" "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/api"
"github.com/goharbor/harbor/src/jobservice/common/utils" "github.com/goharbor/harbor/src/jobservice/common/utils"
"github.com/goharbor/harbor/src/jobservice/config" "github.com/goharbor/harbor/src/jobservice/config"
@ -243,6 +245,7 @@ func (bs *Bootstrap) loadAndRunRedisWorkerPool(
job.ImageGC: (*gc.GarbageCollector)(nil), job.ImageGC: (*gc.GarbageCollector)(nil),
job.Replication: (*replication.Replication)(nil), job.Replication: (*replication.Replication)(nil),
job.ReplicationScheduler: (*replication.Scheduler)(nil), job.ReplicationScheduler: (*replication.Scheduler)(nil),
job.Retention: (*retention.Job)(nil),
}); err != nil { }); err != nil {
// exit // exit
return nil, err return nil, err

View File

@ -16,6 +16,7 @@ package retention
import ( import (
"encoding/json" "encoding/json"
"github.com/goharbor/harbor/src/jobservice/job" "github.com/goharbor/harbor/src/jobservice/job"
"github.com/goharbor/harbor/src/jobservice/logger" "github.com/goharbor/harbor/src/jobservice/logger"
"github.com/goharbor/harbor/src/pkg/retention/dep" "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) repo, _ := getParamRepo(params)
liteMeta, _ := getParamMeta(params) liteMeta, _ := getParamMeta(params)
// Stop check point 1:
if isStopped(ctx) {
logStop(myLogger)
return nil
}
// Retrieve all the candidates under the specified repository // Retrieve all the candidates under the specified repository
allCandidates, err := pj.client.GetCandidates(repo) allCandidates, err := pj.client.GetCandidates(repo)
if err != nil { if err != nil {
@ -76,6 +83,12 @@ func (pj *Job) Run(ctx job.Context, params job.Parameters) error {
return logError(myLogger, err) return logError(myLogger, err)
} }
// Stop check point 2:
if isStopped(ctx) {
logStop(myLogger)
return nil
}
// Run the flow // Run the flow
results, err := processor.Process(allCandidates) results, err := processor.Process(allCandidates)
if err != nil { if err != nil {
@ -95,6 +108,17 @@ func (pj *Job) Run(ctx job.Context, params job.Parameters) error {
return nil 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 { func logError(logger logger.Interface, err error) error {
wrappedErr := errors.Wrap(err, "retention job") wrappedErr := errors.Wrap(err, "retention job")
logger.Error(wrappedErr) logger.Error(wrappedErr)