diff --git a/worker/exec/konnector.go b/worker/exec/konnector.go index b3a66126f09..1753a97214b 100644 --- a/worker/exec/konnector.go +++ b/worker/exec/konnector.go @@ -15,6 +15,7 @@ import ( "github.com/cozy/cozy-stack/model/account" "github.com/cozy/cozy-stack/model/app" + "github.com/cozy/cozy-stack/model/feature" "github.com/cozy/cozy-stack/model/instance" "github.com/cozy/cozy-stack/model/instance/lifecycle" "github.com/cozy/cozy-stack/model/job" @@ -126,6 +127,21 @@ func beforeHookKonnector(j *job.Job) (bool, error) { if err := json.Unmarshal(j.Message, &msg); err == nil { slug = msg.Konnector + + inst, err := lifecycle.GetInstance(j.DomainName()) + if err != nil { + return false, err + } + + flags, err := feature.GetFlags(inst) + if err != nil { + return false, err + } + skipMaintenance, err := flags.HasListItem("harvest.skip-maintenance-for", slug) + if err != nil { + return false, err + } + doc, err := app.GetMaintenanceOptions(slug) if err != nil { j.Logger().Warnf("konnector %q could not get local maintenance status", slug) @@ -136,13 +152,16 @@ func beforeHookKonnector(j *job.Job) (bool, error) { return true, nil } } - j.Logger().Infof("konnector %q has not been triggered because of its maintenance status", slug) - return false, nil - } - inst, err := lifecycle.GetInstance(j.DomainName()) - if err != nil { - return false, err + + if skipMaintenance { + j.Logger().Infof("skipping konnector %q's maintenance", slug) + return true, nil + } else { + j.Logger().Infof("konnector %q has not been triggered because of its maintenance status", slug) + return false, nil + } } + app, err := registry.GetApplication(slug, inst.Registries()) if err != nil { j.Logger().Warnf("konnector %q could not get application to fetch maintenance status", slug) @@ -150,8 +169,14 @@ func beforeHookKonnector(j *job.Job) (bool, error) { if j.Manual && !app.MaintenanceOptions.FlagDisallowManualExec { return true, nil } - j.Logger().Infof("konnector %q has not been triggered because of its maintenance status", slug) - return false, nil + + if skipMaintenance { + j.Logger().Infof("skipping konnector %q's maintenance", slug) + return true, nil + } else { + j.Logger().Infof("konnector %q has not been triggered because of its maintenance status", slug) + return false, nil + } } if msg.BIWebhook { diff --git a/worker/exec/konnector_test.go b/worker/exec/konnector_test.go index 07e55698c4f..9f672ddefe7 100644 --- a/worker/exec/konnector_test.go +++ b/worker/exec/konnector_test.go @@ -442,6 +442,43 @@ echo "{\"type\": \"toto\", \"message\": \"COZY_URL=${COZY_URL}\"}" }) } +func TestBeforeHookKonnector(t *testing.T) { + if testing.Short() { + t.Skip("a couchdb is required for this test: test skipped due to the use of --short flag") + } + + config.UseTestFile(t) + require.NoError(t, loadLocale(), "Could not load default locale translations") + + setup := testutils.NewSetup(t, t.Name()) + slug, err := setup.InstallMiniKonnector() + require.NoError(t, err) + + inst := setup.GetTestInstance() + + t.Run("stack maintenance", func(t *testing.T) { + err := app.ActivateMaintenance(slug, nil) + require.NoError(t, err) + + msg, err := job.NewMessage(map[string]interface{}{ + "konnector": slug, + }) + require.NoError(t, err) + + j := job.NewJob(inst, &job.JobRequest{ + Message: msg, + WorkerType: "konnector", + }) + + shouldExec, err := beforeHookKonnector(j) + assert.False(t, shouldExec) + + testutils.WithFlag(t, inst, "harvest.skip-maintenance-for", map[string]interface{}{"list": []string{slug}}) + shouldExec, err = beforeHookKonnector(j) + assert.True(t, shouldExec) + }) +} + func loadLocale() error { locale := consts.DefaultLocale assetsPath := config.GetConfig().Assets