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 (
"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

View File

@ -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)