handle stop signal in GC job (#11612)

Stop GC job in the init if receives the stop signal

Signed-off-by: wang yan <wangyan@vmware.com>
This commit is contained in:
Wang Yan 2020-04-14 18:59:03 +08:00 committed by GitHub
parent 2d618370ae
commit c6860ac35f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 1 deletions

View File

@ -149,6 +149,11 @@ func (gc *GarbageCollector) Run(ctx job.Context, params job.Parameters) error {
func (gc *GarbageCollector) init(ctx job.Context, params job.Parameters) error {
regCtlInit()
gc.logger = ctx.GetLogger()
opCmd, flag := ctx.OPCommand()
if flag && opCmd.IsStop() {
gc.logger.Info("received the stop signal, quit GC job.")
return nil
}
// UT will use the mock client, ctl and mgr
if os.Getenv("UTTEST") != "true" {
gc.registryCtlClient = registryctl.RegistryCtlClient

View File

@ -4,6 +4,7 @@ import (
"github.com/goharbor/harbor/src/common/config"
"github.com/goharbor/harbor/src/common/models"
commom_regctl "github.com/goharbor/harbor/src/common/registryctl"
"github.com/goharbor/harbor/src/jobservice/job"
"github.com/goharbor/harbor/src/pkg/artifact"
"github.com/goharbor/harbor/src/pkg/artifactrash/model"
artifacttesting "github.com/goharbor/harbor/src/testing/controller/artifact"
@ -117,6 +118,7 @@ func (suite *gcTestSuite) TestInit() {
logger := &mockjobservice.MockJobLogger{}
mock.OnAnything(ctx, "Get").Return("core url", true)
ctx.On("GetLogger").Return(logger)
ctx.On("OPCommand").Return(job.NilCommand, true)
gc := &GarbageCollector{
registryCtlClient: suite.registryCtlClient,
@ -149,10 +151,39 @@ func (suite *gcTestSuite) TestInit() {
suite.True(gc.deleteUntagged)
}
func (suite *gcTestSuite) TestStop() {
ctx := &mockjobservice.MockJobContext{}
logger := &mockjobservice.MockJobLogger{}
mock.OnAnything(ctx, "Get").Return("core url", true)
ctx.On("GetLogger").Return(logger)
ctx.On("OPCommand").Return(job.StopCommand, true)
gc := &GarbageCollector{
registryCtlClient: suite.registryCtlClient,
}
params := map[string]interface{}{
"delete_untagged": true,
"redis_url_reg": "redis url",
}
suite.Nil(gc.init(ctx, params))
ctx = &mockjobservice.MockJobContext{}
mock.OnAnything(ctx, "Get").Return("core url", true)
ctx.On("OPCommand").Return(job.StopCommand, false)
suite.Nil(gc.init(ctx, params))
ctx = &mockjobservice.MockJobContext{}
mock.OnAnything(ctx, "Get").Return("core url", true)
ctx.On("OPCommand").Return(job.NilCommand, true)
suite.Nil(gc.init(ctx, params))
}
func (suite *gcTestSuite) TestRun() {
ctx := &mockjobservice.MockJobContext{}
logger := &mockjobservice.MockJobLogger{}
ctx.On("GetLogger").Return(logger)
ctx.On("OPCommand").Return(job.NilCommand, true)
mock.OnAnything(ctx, "Get").Return("core url", true)
suite.artrashMgr.On("Flush").Return(nil)

View File

@ -44,7 +44,7 @@ func (mjc *MockJobContext) Checkin(status string) error {
// OPCommand ...
func (mjc *MockJobContext) OPCommand() (job.OPCommand, bool) {
args := mjc.Called()
return (job.OPCommand)(args.String(0)), args.Bool(1)
return args.Get(0).(job.OPCommand), args.Bool(1)
}
// GetLogger ...