Skip to content

Commit

Permalink
feat: Konnector worker honors skip-maintenance-for
Browse files Browse the repository at this point in the history
  The `harvest.skip-maintenance-for` flag can be set to a list of
  konnector slugs to unlock these in Harvest and allow requesting an
  execution even if it's maintenance mode has been activated either in
  the registry or cozy-stack.

  However, the flag was not honored in the konnector worker thus
  preventing its execution.
  We'll now check if the konnector to be executed is part of the list
  (if set) and skip the maintenance mode when appropriate.
  • Loading branch information
taratatach committed Feb 19, 2024
1 parent 5b51b33 commit 4c01187
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 8 deletions.
41 changes: 33 additions & 8 deletions worker/exec/konnector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -136,22 +152,31 @@ 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)
} else if app.MaintenanceActivated {
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 {
Expand Down
37 changes: 37 additions & 0 deletions worker/exec/konnector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4c01187

Please sign in to comment.