Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix: dont schedule disabled jobs (#2122)
Kyle Mendell41c3bc7f916d
diff --git a/backend/internal/services/job_service_test.go b/backend/internal/services/job_service_test.goindex 11bc5bd88..fabb7cfc3 100644--- a/backend/internal/services/job_service_test.go+++ b/backend/internal/services/job_service_test.go@@ -41,6 +41,23 @@ func TestJobService_ListJobs_AnalyticsHeartbeatIsManagedInternally(t *testing.T)require.False(t, analyticsJob.IsContinuous)}+func TestJobService_ListJobs_IncludesDisabledAutoHealJob(t *testing.T) {+ ctx := context.Background()+ db := setupSettingsTestDB(t)++ settingsSvc, err := NewSettingsService(ctx, db)+ require.NoError(t, err)+ require.NoError(t, settingsSvc.SetBoolSetting(ctx, "autoHealEnabled", false))++ jobSvc := NewJobService(db, settingsSvc, &config.Config{})+ jobs, err := jobSvc.ListJobs(ctx)+ require.NoError(t, err)++ autoHealJob := findJobStatusByIDInternal(t, jobs.Jobs, "auto-heal")+ require.False(t, autoHealJob.Enabled)+ require.Equal(t, "autoHealInterval", autoHealJob.SettingsKey)+}+func findJobStatusByIDInternal(t *testing.T, jobs []jobschedule.JobStatus, id string) jobschedule.JobStatus {t.Helper()diff --git a/backend/pkg/scheduler/auto_heal_job.go b/backend/pkg/scheduler/auto_heal_job.goindex aa1ed2429..129e59197 100644--- a/backend/pkg/scheduler/auto_heal_job.go+++ b/backend/pkg/scheduler/auto_heal_job.go@@ -58,6 +58,10 @@ func (j *AutoHealJob) Name() string {return AutoHealJobName}+func (j *AutoHealJob) ShouldSchedule(ctx context.Context) bool {+ return j.settingsService.GetBoolSetting(ctx, "autoHealEnabled", false)+}+func (j *AutoHealJob) Schedule(ctx context.Context) string {schedule := j.settingsService.GetStringSetting(ctx, "autoHealInterval", "*/30 * * * * *")if schedule == "" {diff --git a/backend/pkg/scheduler/auto_heal_job_test.go b/backend/pkg/scheduler/auto_heal_job_test.goindex 16afdffe9..720bc3fa1 100644--- a/backend/pkg/scheduler/auto_heal_job_test.go+++ b/backend/pkg/scheduler/auto_heal_job_test.go@@ -106,6 +106,17 @@ func TestAutoHeal_Schedule_Default(t *testing.T) {require.Equal(t, "auto-heal", job.Name())}+func TestAutoHeal_ShouldSchedule(t *testing.T) {+ ctx := context.Background()+ _, settingsSvc, _ := setupAnalyticsStateServicesInternal(t)+ job := NewAutoHealJob(nil, settingsSvc, nil, nil)++ require.False(t, job.ShouldSchedule(ctx))++ require.NoError(t, settingsSvc.SetBoolSetting(ctx, "autoHealEnabled", true))+ require.True(t, job.ShouldSchedule(ctx))+}+func TestAutoHeal_ResetRestartTracking(t *testing.T) {job := newTestAutoHealJob()diff --git a/backend/pkg/scheduler/auto_update_job.go b/backend/pkg/scheduler/auto_update_job.goindex da164330b..7f6ddefad 100644--- a/backend/pkg/scheduler/auto_update_job.go+++ b/backend/pkg/scheduler/auto_update_job.go@@ -25,6 +25,12 @@ func (j *AutoUpdateJob) Name() string {return "auto-update"}+func (j *AutoUpdateJob) ShouldSchedule(ctx context.Context) bool {+ enabled := j.settingsService.GetBoolSetting(ctx, "autoUpdate", false)+ pollingEnabled := j.settingsService.GetBoolSetting(ctx, "pollingEnabled", true)+ return enabled && pollingEnabled+}+func (j *AutoUpdateJob) Schedule(ctx context.Context) string {s := j.settingsService.GetStringSetting(ctx, "autoUpdateInterval", "0 0 0 * * *")if s == "" {diff --git a/backend/pkg/scheduler/auto_update_job_test.go b/backend/pkg/scheduler/auto_update_job_test.gonew file mode 100644index 000000000..06a2c78c5--- /dev/null+++ b/backend/pkg/scheduler/auto_update_job_test.go@@ -0,0 +1,25 @@+package scheduler++import (+ "context"+ "testing"++ "github.com/stretchr/testify/require"+)++func TestAutoUpdateJob_ShouldSchedule_RequiresAutoUpdateAndPolling(t *testing.T) {+ ctx := context.Background()+ _, settingsSvc, _ := setupAnalyticsStateServicesInternal(t)+ job := NewAutoUpdateJob(nil, settingsSvc)++ require.False(t, job.ShouldSchedule(ctx))++ require.NoError(t, settingsSvc.SetBoolSetting(ctx, "autoUpdate", true))+ require.True(t, job.ShouldSchedule(ctx))++ require.NoError(t, settingsSvc.SetBoolSetting(ctx, "pollingEnabled", false))+ require.False(t, job.ShouldSchedule(ctx))++ require.NoError(t, settingsSvc.SetBoolSetting(ctx, "autoUpdate", false))+ require.False(t, job.ShouldSchedule(ctx))+}diff --git a/backend/pkg/scheduler/gitops_sync_job.go b/backend/pkg/scheduler/gitops_sync_job.goindex 7de1cb41b..a90ccab60 100644--- a/backend/pkg/scheduler/gitops_sync_job.go+++ b/backend/pkg/scheduler/gitops_sync_job.go@@ -26,6 +26,10 @@ func (j *GitOpsSyncJob) Name() string {return "gitops-sync"}+func (j *GitOpsSyncJob) ShouldSchedule(ctx context.Context) bool {+ return j.settingsService.GetBoolSetting(ctx, "gitopsSyncEnabled", true)+}+func (j *GitOpsSyncJob) Schedule(ctx context.Context) string {schedule := j.settingsService.GetStringSetting(ctx, "gitopsSyncInterval", "0 */1 * * * *")if schedule == "" {diff --git a/backend/pkg/scheduler/image_polling_job.go b/backend/pkg/scheduler/image_polling_job.goindex 6e0f0285f..fab417f3f 100644--- a/backend/pkg/scheduler/image_polling_job.go+++ b/backend/pkg/scheduler/image_polling_job.go@@ -27,6 +27,10 @@ func (j *ImagePollingJob) Name() string {return "image-polling"}+func (j *ImagePollingJob) ShouldSchedule(ctx context.Context) bool {+ return j.settingsService.GetBoolSetting(ctx, "pollingEnabled", true)+}+func (j *ImagePollingJob) Schedule(ctx context.Context) string {s := j.settingsService.GetStringSetting(ctx, "pollingInterval", "0 0 * * * *")if s == "" {diff --git a/backend/pkg/scheduler/scheduled_prune_job.go b/backend/pkg/scheduler/scheduled_prune_job.goindex a8bc5b6af..51986bfb0 100644--- a/backend/pkg/scheduler/scheduled_prune_job.go+++ b/backend/pkg/scheduler/scheduled_prune_job.go@@ -31,6 +31,10 @@ func (j *ScheduledPruneJob) Name() string {return ScheduledPruneJobName}+func (j *ScheduledPruneJob) ShouldSchedule(ctx context.Context) bool {+ return j.settingsService.GetBoolSetting(ctx, "scheduledPruneEnabled", false)+}+func (j *ScheduledPruneJob) Schedule(ctx context.Context) string {schedule := j.settingsService.GetStringSetting(ctx, "scheduledPruneInterval", "0 0 0 * * *")if schedule == "" {diff --git a/backend/pkg/scheduler/scheduler.go b/backend/pkg/scheduler/scheduler.goindex 9e105a843..996df263b 100644--- a/backend/pkg/scheduler/scheduler.go+++ b/backend/pkg/scheduler/scheduler.go@@ -48,44 +48,24 @@ func (js *JobScheduler) GetJob(jobID string) (schedulertypes.Job, bool) {func (js *JobScheduler) StartScheduler() {for _, job := range js.jobs {- currentJob := job- schedule := currentJob.Schedule(js.context)-- slog.InfoContext(js.context, "Starting Job", "name", currentJob.Name(), "schedule", schedule)-- entryID, err := js.cron.AddFunc(schedule, func() {- slog.InfoContext(js.context, "Job starting", "name", currentJob.Name(), "schedule", schedule)- currentJob.Run(js.context)- slog.InfoContext(js.context, "Job finished", "name", currentJob.Name())- })- if err != nil {- slog.ErrorContext(js.context, "Failed to schedule job", "name", currentJob.Name(), "schedule", schedule, "error", err)- continue+ if err := js.scheduleJobInternal(js.context, job); err != nil {+ slog.ErrorContext(js.context, "Failed to schedule job", "name", job.Name(), "error", err)}-- js.entryIDs[currentJob.Name()] = entryID}js.cron.Start()}func (js *JobScheduler) RescheduleJob(ctx context.Context, job schedulertypes.Job) error {- schedule := job.Schedule(ctx)-if entryID, ok := js.entryIDs[job.Name()]; ok {js.cron.Remove(entryID)+ delete(js.entryIDs, job.Name())}- entryID, err := js.cron.AddFunc(schedule, func() {- slog.InfoContext(ctx, "Job starting", "name", job.Name(), "schedule", schedule)- job.Run(ctx)- slog.InfoContext(ctx, "Job finished", "name", job.Name())- })- if err != nil {+ if err := js.scheduleJobInternal(ctx, job); err != nil {return err}- js.entryIDs[job.Name()] = entryID- slog.DebugContext(ctx, "Job rescheduled", "name", job.Name(), "schedule", schedule, "contextCanceled", ctx.Err() != nil)+ slog.DebugContext(ctx, "Job rescheduled", "name", job.Name(), "scheduled", js.isJobScheduledInternal(job.Name()), "contextCanceled", ctx.Err() != nil)return nil}@@ -100,3 +80,30 @@ func (js *JobScheduler) Run(ctx context.Context) error {js.cron.Stop()return nil}++func (js *JobScheduler) scheduleJobInternal(ctx context.Context, job schedulertypes.Job) error {+ if conditionalJob, ok := job.(schedulertypes.ConditionalJob); ok && !conditionalJob.ShouldSchedule(ctx) {+ slog.DebugContext(ctx, "Job disabled; not scheduling", "name", job.Name())+ return nil+ }++ schedule := job.Schedule(ctx)+ slog.InfoContext(ctx, "Starting Job", "name", job.Name(), "schedule", schedule)++ entryID, err := js.cron.AddFunc(schedule, func() {+ slog.InfoContext(ctx, "Job starting", "name", job.Name(), "schedule", schedule)+ job.Run(ctx)+ slog.InfoContext(ctx, "Job finished", "name", job.Name())+ })+ if err != nil {+ return err+ }++ js.entryIDs[job.Name()] = entryID+ return nil+}++func (js *JobScheduler) isJobScheduledInternal(jobName string) bool {+ _, ok := js.entryIDs[jobName]+ return ok+}diff --git a/backend/pkg/scheduler/scheduler_test.go b/backend/pkg/scheduler/scheduler_test.goindex 1b40232dd..5e1c6cda5 100644--- a/backend/pkg/scheduler/scheduler_test.go+++ b/backend/pkg/scheduler/scheduler_test.go@@ -25,6 +25,98 @@ func (j *testSchedulerJob) Run(ctx context.Context) {}}+type conditionalTestSchedulerJob struct {+ *testSchedulerJob+ shouldSchedule func(context.Context) bool+}++func (j *conditionalTestSchedulerJob) ShouldSchedule(ctx context.Context) bool {+ if j.shouldSchedule == nil {+ return true+ }++ return j.shouldSchedule(ctx)+}++func TestJobScheduler_StartScheduler_SkipsDisabledConditionalJobs(t *testing.T) {+ js := NewJobScheduler(context.Background(), nil)++ job := &conditionalTestSchedulerJob{+ testSchedulerJob: &testSchedulerJob{+ name: "test-disabled-startup",+ schedule: "*/1 * * * * *",+ },+ shouldSchedule: func(context.Context) bool { return false },+ }++ js.RegisterJob(job)+ js.StartScheduler()+ defer js.cron.Stop()++ require.NotContains(t, js.entryIDs, job.Name())+ require.Empty(t, js.cron.Entries())+}++func TestJobScheduler_RescheduleJob_RemovesEntryWhenDisabled(t *testing.T) {+ js := NewJobScheduler(context.Background(), nil)+ enabled := true++ job := &conditionalTestSchedulerJob{+ testSchedulerJob: &testSchedulerJob{+ name: "test-disabled-reschedule",+ schedule: "*/1 * * * * *",+ },+ shouldSchedule: func(context.Context) bool { return enabled },+ }++ require.NoError(t, js.RescheduleJob(context.Background(), job))+ require.Contains(t, js.entryIDs, job.Name())++ enabled = false++ require.NoError(t, js.RescheduleJob(context.Background(), job))+ require.NotContains(t, js.entryIDs, job.Name())+ require.Empty(t, js.cron.Entries())+}++func TestJobScheduler_RescheduleJob_AddsEntryWhenEnabled(t *testing.T) {+ js := NewJobScheduler(context.Background(), nil)+ enabled := false++ job := &conditionalTestSchedulerJob{+ testSchedulerJob: &testSchedulerJob{+ name: "test-enabled-reschedule",+ schedule: "*/1 * * * * *",+ },+ shouldSchedule: func(context.Context) bool { return enabled },+ }++ require.NoError(t, js.RescheduleJob(context.Background(), job))+ require.NotContains(t, js.entryIDs, job.Name())++ enabled = true++ require.NoError(t, js.RescheduleJob(context.Background(), job))+ require.Contains(t, js.entryIDs, job.Name())+ require.Len(t, js.cron.Entries(), 1)+}++func TestJobScheduler_StartScheduler_SchedulesNonConditionalJobs(t *testing.T) {+ js := NewJobScheduler(context.Background(), nil)++ job := &testSchedulerJob{+ name: "test-non-conditional-startup",+ schedule: "*/1 * * * * *",+ }++ js.RegisterJob(job)+ js.StartScheduler()+ defer js.cron.Stop()++ require.Contains(t, js.entryIDs, job.Name())+ require.Len(t, js.cron.Entries(), 1)+}+func TestJobScheduler_RescheduleJob_UsesProvidedContext(t *testing.T) {js := NewJobScheduler(context.Background(), nil)diff --git a/backend/pkg/scheduler/vulnerability_scan_job.go b/backend/pkg/scheduler/vulnerability_scan_job.goindex 6d33d9c62..126c59d34 100644--- a/backend/pkg/scheduler/vulnerability_scan_job.go+++ b/backend/pkg/scheduler/vulnerability_scan_job.go@@ -38,6 +38,10 @@ func (j *VulnerabilityScanJob) Name() string {return VulnerabilityScanJobName}+func (j *VulnerabilityScanJob) ShouldSchedule(ctx context.Context) bool {+ return j.settingsService.GetBoolSetting(ctx, "vulnerabilityScanEnabled", false)+}+// Schedule returns the cron expression for the job. Defaults to daily at midnight.func (j *VulnerabilityScanJob) Schedule(ctx context.Context) string {schedule := j.settingsService.GetStringSetting(ctx, "vulnerabilityScanInterval", "0 0 0 * * *")diff --git a/types/scheduler/job.go b/types/scheduler/job.goindex 485b1f5e7..372ef3123 100644--- a/types/scheduler/job.go+++ b/types/scheduler/job.go@@ -7,3 +7,9 @@ type Job interface {Schedule(ctx context.Context) stringRun(ctx context.Context)}++// ConditionalJob allows a job to opt out of cron registration when it is disabled.+// Jobs that do not implement this interface are always scheduled.+type ConditionalJob interface {+ ShouldSchedule(ctx context.Context) bool+}