mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-30 06:03:45 +01:00
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:
commit
8a96f39caa
@ -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
|
||||||
|
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user