From c6860ac35f615c27622b045eff2e349a259f24b5 Mon Sep 17 00:00:00 2001 From: Wang Yan Date: Tue, 14 Apr 2020 18:59:03 +0800 Subject: [PATCH] handle stop signal in GC job (#11612) Stop GC job in the init if receives the stop signal Signed-off-by: wang yan --- .../job/impl/gc/garbage_collection.go | 5 +++ .../job/impl/gc/garbage_collection_test.go | 31 +++++++++++++++++++ src/testing/jobservice/context.go | 2 +- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/jobservice/job/impl/gc/garbage_collection.go b/src/jobservice/job/impl/gc/garbage_collection.go index 374cfe2ae..29388ed48 100644 --- a/src/jobservice/job/impl/gc/garbage_collection.go +++ b/src/jobservice/job/impl/gc/garbage_collection.go @@ -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 diff --git a/src/jobservice/job/impl/gc/garbage_collection_test.go b/src/jobservice/job/impl/gc/garbage_collection_test.go index 0645e55ad..28f1f658d 100644 --- a/src/jobservice/job/impl/gc/garbage_collection_test.go +++ b/src/jobservice/job/impl/gc/garbage_collection_test.go @@ -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) diff --git a/src/testing/jobservice/context.go b/src/testing/jobservice/context.go index 3e6dd3a3a..209a0ea5c 100644 --- a/src/testing/jobservice/context.go +++ b/src/testing/jobservice/context.go @@ -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 ...