Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
refactor: cron based job scheduler (#1501)
Kyle Mendella307534fb53b
diff --git a/backend/go.mod b/backend/go.modindex 362e40b89..09371b03a 100644--- a/backend/go.mod+++ b/backend/go.mod@@ -17,7 +17,6 @@ require (github.com/gin-contrib/cors v1.7.6github.com/gin-gonic/gin v1.11.0github.com/glebarez/sqlite v1.11.0- github.com/go-co-op/gocron/v2 v2.19.0github.com/go-git/go-git/v5 v5.16.4github.com/goccy/go-yaml v1.19.2github.com/gofrs/flock v0.13.0@@ -31,6 +30,7 @@ require (github.com/lmittmann/tint v1.1.2github.com/nicholas-fedor/shoutrrr v0.13.1github.com/orandin/slog-gorm v1.4.0+ github.com/robfig/cron/v3 v3.0.1github.com/samber/slog-gin v1.19.1github.com/shirou/gopsutil/v4 v4.25.12github.com/spf13/cobra v1.10.2@@ -172,7 +172,6 @@ require (github.com/quic-go/qpack v0.6.0 // indirectgithub.com/quic-go/quic-go v0.57.0 // indirectgithub.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect- github.com/robfig/cron/v3 v3.0.1 // indirectgithub.com/santhosh-tekuri/jsonschema/v6 v6.0.2 // indirectgithub.com/secure-systems-lab/go-securesystemslib v0.9.1 // indirectgithub.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirectdiff --git a/backend/go.sum b/backend/go.sumindex 676d457a4..3408124ad 100644--- a/backend/go.sum+++ b/backend/go.sum@@ -184,8 +184,6 @@ github.com/glebarez/sqlite v1.11.0 h1:wSG0irqzP6VurnMEpFGer5Li19RpIRi2qvQz++w0GMgithub.com/glebarez/sqlite v1.11.0/go.mod h1:h8/o8j5wiAsqSPoWELDUdJXhjAhsVliSn7bWZjOhrgQ=github.com/gliderlabs/ssh v0.3.8 h1:a4YXD1V7xMF9g5nTkdfnja3Sxy1PVDCj1Zg4Wb8vY6c=github.com/gliderlabs/ssh v0.3.8/go.mod h1:xYoytBv1sV0aL3CavoDuJIQNURXkkfPA/wxQ1pL1fAU=-github.com/go-co-op/gocron/v2 v2.19.0 h1:OKf2y6LXPs/BgBI2fl8PxUpNAI1DA9Mg+hSeGOS38OU=-github.com/go-co-op/gocron/v2 v2.19.0/go.mod h1:5lEiCKk1oVJV39Zg7/YG10OnaVrDAV5GGR6O0663k6U=github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI=github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic=github.com/go-git/go-billy/v5 v5.6.2 h1:6Q86EsPXMa7c3YZ3aLAQsMA0VlWmy43r6FHqa/UNbRM=diff --git a/backend/internal/bootstrap/bootstrap.go b/backend/internal/bootstrap/bootstrap.goindex e0f5cd288..02906149f 100644--- a/backend/internal/bootstrap/bootstrap.go+++ b/backend/internal/bootstrap/bootstrap.go@@ -20,6 +20,7 @@ import ("github.com/getarcaneapp/arcane/backend/internal/utils/crypto""github.com/getarcaneapp/arcane/backend/internal/utils/edge"httputils "github.com/getarcaneapp/arcane/backend/internal/utils/http"+ "github.com/getarcaneapp/arcane/backend/pkg/scheduler")func Bootstrap(ctx context.Context) error {@@ -60,6 +61,19 @@ func Bootstrap(ctx context.Context) error {utils.EnsureEncryptionKey(appCtx, cfg, appServices.Settings.EnsureEncryptionKey)crypto.InitEncryption(cfg)utils.InitializeDefaultSettings(appCtx, cfg, appServices.Settings)+ utils.MigrateSchedulerCronValues(+ appCtx,+ appServices.Settings.GetStringSetting,+ appServices.Settings.UpdateSetting,+ appServices.Settings.LoadDatabaseSettings,+ )+ if appServices.GitOpsSync != nil {+ utils.MigrateGitOpsSyncIntervals(+ appCtx,+ appServices.GitOpsSync.ListSyncIntervalsRaw,+ appServices.GitOpsSync.UpdateSyncIntervalMinutes,+ )+ }if err := appServices.Settings.NormalizeProjectsDirectory(appCtx, cfg.ProjectsDirectory); err != nil {slog.WarnContext(appCtx, "Failed to normalize projects directory", "error", err)@@ -91,10 +105,7 @@ func Bootstrap(ctx context.Context) error {}}- scheduler, err := initializeScheduler()- if err != nil {- return fmt.Errorf("failed to create job scheduler: %w", err)- }+ scheduler := scheduler.NewJobScheduler(appCtx)registerJobs(appCtx, scheduler, appServices, cfg)router, tunnelServer := setupRouter(appCtx, cfg, appServices)@@ -115,7 +126,7 @@ func Bootstrap(ctx context.Context) error {}}- err = runServices(appCtx, cfg, router, scheduler, tunnelServer)+ err = runServices(appCtx, cfg, router, tunnelServer, scheduler)if err != nil {return fmt.Errorf("failed to run services: %w", err)}@@ -168,16 +179,19 @@ func handleAgentBootstrapPairing(ctx context.Context, cfg *config.Config, httpCl}}-func runServices(appCtx context.Context, cfg *config.Config, router http.Handler, scheduler interface{ Run(context.Context) error }, tunnelServer *edge.TunnelServer) error {- go func() {- slog.InfoContext(appCtx, "Starting scheduler")- if err := scheduler.Run(appCtx); err != nil {- if !errors.Is(err, context.Canceled) {- slog.ErrorContext(appCtx, "Job scheduler exited with error", "error", err)+func runServices(appCtx context.Context, cfg *config.Config, router http.Handler, tunnelServer *edge.TunnelServer, schedulers ...interface{ Run(context.Context) error }) error {+ for _, s := range schedulers {+ scheduler := s+ go func() {+ slog.InfoContext(appCtx, "Starting scheduler")+ if err := scheduler.Run(appCtx); err != nil {+ if !errors.Is(err, context.Canceled) {+ slog.ErrorContext(appCtx, "Job scheduler exited with error", "error", err)+ }}- }- slog.InfoContext(appCtx, "Scheduler stopped")- }()+ slog.InfoContext(appCtx, "Scheduler stopped")+ }()+ }srv := &http.Server{Addr: ":" + cfg.Port,diff --git a/backend/internal/bootstrap/jobs_bootstrap.go b/backend/internal/bootstrap/jobs_bootstrap.goindex 2fc56dd98..1c74cdf7d 100644--- a/backend/internal/bootstrap/jobs_bootstrap.go+++ b/backend/internal/bootstrap/jobs_bootstrap.go@@ -2,69 +2,46 @@ package bootstrapimport ("context"- "fmt""log/slog""github.com/getarcaneapp/arcane/backend/internal/config"- "github.com/getarcaneapp/arcane/backend/internal/job"+ pkg_scheduler "github.com/getarcaneapp/arcane/backend/pkg/scheduler")-func initializeScheduler() (*job.Scheduler, error) {- scheduler, err := job.NewScheduler()- if err != nil {- return nil, fmt.Errorf("failed to create job scheduler: %w", err)- }- return scheduler, nil-}+func registerJobs(appCtx context.Context, newScheduler *pkg_scheduler.JobScheduler, appServices *Services, appConfig *config.Config) {+ autoUpdateJob := pkg_scheduler.NewAutoUpdateJob(appServices.Updater, appServices.Settings)+ newScheduler.RegisterJob(autoUpdateJob)-func registerJobs(appCtx context.Context, scheduler *job.Scheduler, appServices *Services, appConfig *config.Config) {- autoUpdateJob := job.NewAutoUpdateJob(scheduler, appServices.Updater, appServices.Settings)- if err := autoUpdateJob.Register(appCtx); err != nil {- slog.ErrorContext(appCtx, "Failed to register auto-update job", "error", err)- }-- imagePollingJob := job.NewImagePollingJob(scheduler, appServices.ImageUpdate, appServices.Settings, appServices.Environment)- if err := imagePollingJob.Register(appCtx); err != nil {- slog.ErrorContext(appCtx, "Failed to register image polling job", "error", err)- }+ imagePollingJob := pkg_scheduler.NewImagePollingJob(appServices.ImageUpdate, appServices.Settings, appServices.Environment)+ newScheduler.RegisterJob(imagePollingJob)- environmentHealthJob := job.NewEnvironmentHealthJob(scheduler, appServices.Environment, appServices.Settings)+ environmentHealthJob := pkg_scheduler.NewEnvironmentHealthJob(appServices.Environment, appServices.Settings)if !appConfig.AgentMode {- if err := environmentHealthJob.Register(appCtx); err != nil {- slog.ErrorContext(appCtx, "Failed to register environment health check job", "error", err)- }+ newScheduler.RegisterJob(environmentHealthJob)}- analyticsJob := job.NewAnalyticsJob(scheduler, appServices.Settings, nil, appConfig)- if err := analyticsJob.Register(appCtx); err != nil {- slog.ErrorContext(appCtx, "Failed to register analytics heartbeat job", "error", err)- }+ analyticsJob := pkg_scheduler.NewAnalyticsJob(appServices.Settings, nil, appConfig)+ newScheduler.RegisterJob(analyticsJob)- eventCleanupJob := job.NewEventCleanupJob(scheduler, appServices.Event, appServices.Settings)- if err := eventCleanupJob.Register(appCtx); err != nil {- slog.ErrorContext(appCtx, "Failed to register event cleanup job", "error", err)- }+ eventCleanupJob := pkg_scheduler.NewEventCleanupJob(appServices.Event, appServices.Settings)+ newScheduler.RegisterJob(eventCleanupJob)- scheduledPruneJob := job.NewScheduledPruneJob(scheduler, appServices.System, appServices.Settings)- if err := scheduledPruneJob.Register(appCtx); err != nil {- slog.ErrorContext(appCtx, "Failed to register scheduled prune job", "error", err)- }+ scheduledPruneJob := pkg_scheduler.NewScheduledPruneJob(appServices.System, appServices.Settings)+ newScheduler.RegisterJob(scheduledPruneJob)- fsWatcherJob, err := job.RegisterFilesystemWatcherJob(appCtx, scheduler, appServices.Project, appServices.Template, appServices.Settings)+ fsWatcherJob, err := pkg_scheduler.RegisterFilesystemWatcherJob(appCtx, appServices.Project, appServices.Template, appServices.Settings)if err != nil {slog.ErrorContext(appCtx, "Failed to register filesystem watcher job", "error", err)}- gitOpsSyncJob := job.NewGitOpsSyncJob(scheduler, appServices.GitOpsSync, appServices.Settings)- if err := gitOpsSyncJob.Register(appCtx); err != nil {- slog.ErrorContext(appCtx, "Failed to register GitOps sync job", slog.Any("error", err))- }+ gitOpsSyncJob := pkg_scheduler.NewGitOpsSyncJob(appServices.GitOpsSync, appServices.Settings)+ newScheduler.RegisterJob(gitOpsSyncJob)setupJobScheduleCallbacks(appServices, appConfig, environmentHealthJob, analyticsJob, eventCleanupJob)setupSettingsCallbacks(appServices, appConfig, imagePollingJob, autoUpdateJob, environmentHealthJob, fsWatcherJob, scheduledPruneJob)}-func setupJobScheduleCallbacks(appServices *Services, appConfig *config.Config, environmentHealthJob *job.EnvironmentHealthJob, analyticsJob *job.AnalyticsJob, eventCleanupJob *job.EventCleanupJob) {+func setupJobScheduleCallbacks(appServices *Services, appConfig *config.Config, environmentHealthJob *pkg_scheduler.EnvironmentHealthJob, analyticsJob *pkg_scheduler.AnalyticsJob, eventCleanupJob *pkg_scheduler.EventCleanupJob) {if appServices.JobSchedule != nil {appServices.JobSchedule.OnJobSchedulesChanged = func(ctx context.Context) {if !appConfig.AgentMode {@@ -82,7 +59,7 @@ func setupJobScheduleCallbacks(appServices *Services, appConfig *config.Config,}}-func setupSettingsCallbacks(appServices *Services, appConfig *config.Config, imagePollingJob *job.ImagePollingJob, autoUpdateJob *job.AutoUpdateJob, environmentHealthJob *job.EnvironmentHealthJob, fsWatcherJob *job.FilesystemWatcherJob, scheduledPruneJob *job.ScheduledPruneJob) {+func setupSettingsCallbacks(appServices *Services, appConfig *config.Config, imagePollingJob *pkg_scheduler.ImagePollingJob, autoUpdateJob *pkg_scheduler.AutoUpdateJob, environmentHealthJob *pkg_scheduler.EnvironmentHealthJob, fsWatcherJob *pkg_scheduler.FilesystemWatcherJob, scheduledPruneJob *pkg_scheduler.ScheduledPruneJob) {appServices.Settings.OnImagePollingSettingsChanged = func(ctx context.Context) {if err := imagePollingJob.Reschedule(ctx); err != nil {slog.WarnContext(ctx, "Failed to reschedule image-polling job", "error", err)diff --git a/backend/internal/huma/handlers/job_schedules.go b/backend/internal/huma/handlers/job_schedules.goindex c2a227b31..35f11495f 100644--- a/backend/internal/huma/handlers/job_schedules.go+++ b/backend/internal/huma/handlers/job_schedules.go@@ -30,7 +30,7 @@ func RegisterJobSchedules(api huma.API, jobSvc *services.JobService) {Method: http.MethodGet,Path: "/job-schedules",Summary: "Get job schedules",- Description: "Get configured intervals (in minutes) for background jobs",+ Description: "Get configured cron schedules for background jobs",Tags: []string{"JobSchedules"},Security: []map[string][]string{{"BearerAuth": {}},@@ -43,7 +43,7 @@ func RegisterJobSchedules(api huma.API, jobSvc *services.JobService) {Method: http.MethodPut,Path: "/job-schedules",Summary: "Update job schedules",- Description: "Update background job intervals and reschedule running jobs",+ Description: "Update background job cron schedules and reschedule running jobs",Tags: []string{"JobSchedules"},Security: []map[string][]string{{"BearerAuth": {}},diff --git a/backend/internal/job/analytics_job.go b/backend/internal/job/analytics_job.godeleted file mode 100644index e62a654bc..000000000--- a/backend/internal/job/analytics_job.go+++ /dev/null@@ -1,160 +0,0 @@-package job--import (- "bytes"- "context"- "encoding/json"- "fmt"- "log/slog"- "net/http"- "strings"- "time"-- backoff "github.com/cenkalti/backoff/v5"- "github.com/getarcaneapp/arcane/backend/internal/config"- "github.com/getarcaneapp/arcane/backend/internal/services"- "github.com/go-co-op/gocron/v2"-)--const (- AnalyticsJobName = "analytics-heartbeat"- defaultHeartbeatEndpoint = "https://checkin.getarcane.app/heartbeat"- defaultAnalyticsInterval = 24 * time.Hour-)--type AnalyticsJob struct {- scheduler *Scheduler- settingsService *services.SettingsService- httpClient *http.Client- heartbeatURL string- cfg *config.Config-}--func NewAnalyticsJob(- scheduler *Scheduler,- settingsService *services.SettingsService,- httpClient *http.Client,- cfg *config.Config,-) *AnalyticsJob {- if httpClient == nil {- httpClient = &http.Client{Timeout: 30 * time.Second}- }- return &AnalyticsJob{- scheduler: scheduler,- settingsService: settingsService,- httpClient: httpClient,- heartbeatURL: defaultHeartbeatEndpoint,- cfg: cfg,- }-}--func (j *AnalyticsJob) Register(ctx context.Context) error {- if j.cfg.AnalyticsDisabled || !j.cfg.Environment.IsProdEnvironment() {- slog.InfoContext(ctx, "analytics disabled or not in production; heartbeat job not registered", "analyticsDisabled", j.cfg.AnalyticsDisabled, "env", j.cfg.Environment)- j.scheduler.RemoveJobByName(AnalyticsJobName)- return nil- }-- intervalMinutes := j.settingsService.GetIntSetting(ctx, "analyticsHeartbeatInterval", int(defaultAnalyticsInterval/time.Minute))- interval := time.Duration(intervalMinutes) * time.Minute- if interval < 60*time.Minute {- interval = defaultAnalyticsInterval- }-- j.scheduler.RemoveJobByName(AnalyticsJobName)-- jobDefinition := gocron.DurationJob(interval)- slog.InfoContext(ctx, "registering analytics heartbeat job", "jobName", AnalyticsJobName, "interval", interval.String(), "endpoint", j.heartbeatURL)-- return j.scheduler.RegisterJob(- ctx,- AnalyticsJobName,- jobDefinition,- j.Execute,- true, // run immediately on startup- )-}--func (j *AnalyticsJob) Reschedule(ctx context.Context) error {- if j.cfg.AnalyticsDisabled || !j.cfg.Environment.IsProdEnvironment() {- j.scheduler.RemoveJobByName(AnalyticsJobName)- return nil- }-- intervalMinutes := j.settingsService.GetIntSetting(ctx, "analyticsHeartbeatInterval", int(defaultAnalyticsInterval/time.Minute))- interval := time.Duration(intervalMinutes) * time.Minute- if interval < 60*time.Minute {- interval = defaultAnalyticsInterval- }-- slog.InfoContext(ctx, "analytics heartbeat settings changed; rescheduling",- "jobName", AnalyticsJobName, "interval", interval.String())-- return j.scheduler.RescheduleDurationJobByName(ctx, AnalyticsJobName, interval, j.Execute, false)-}--func (j *AnalyticsJob) Execute(parentCtx context.Context) error {- if j.cfg.AnalyticsDisabled || !j.cfg.Environment.IsProdEnvironment() {- slog.InfoContext(parentCtx, "analytics disabled or not in production; skipping heartbeat", "analyticsDisabled", j.cfg.AnalyticsDisabled, "env", j.cfg.Environment)- return nil- }-- instanceID := j.settingsService.GetStringSetting(parentCtx, "instanceId", "")-- payload := struct {- Version string `json:"version"`- InstanceID string `json:"instance_id"`- }{- Version: getVersion(),- InstanceID: instanceID,- }-- body, err := json.Marshal(payload)- if err != nil {- return fmt.Errorf("failed to marshal analytics heartbeat body: %w", err)- }-- slog.InfoContext(parentCtx, "sending analytics heartbeat", "jobName", AnalyticsJobName)-- _, err = backoff.Retry(- parentCtx,- func() (struct{}, error) {- ctx, cancel := context.WithTimeout(parentCtx, 20*time.Second)- defer cancel()-- req, err := http.NewRequestWithContext(ctx, http.MethodPost, j.heartbeatURL, bytes.NewReader(body))- if err != nil {- return struct{}{}, fmt.Errorf("failed to create request: %w", err)- }- req.Header.Set("Content-Type", "application/json")-- resp, err := j.httpClient.Do(req)- if err != nil {- return struct{}{}, fmt.Errorf("failed to send request: %w", err)- }- defer resp.Body.Close()-- if resp.StatusCode < 200 || resp.StatusCode >= 300 {- return struct{}{}, fmt.Errorf("request failed with status: %d", resp.StatusCode)- }- return struct{}{}, nil- },- backoff.WithBackOff(backoff.NewExponentialBackOff()),- backoff.WithMaxTries(3),- )-- if err != nil {- slog.ErrorContext(parentCtx, "analytics heartbeat failed", "error", err)- return fmt.Errorf("analytics heartbeat request failed: %w", err)- }-- slog.InfoContext(parentCtx, "analytics heartbeat sent successfully", "jobName", AnalyticsJobName)- return nil-}--func getVersion() string {- if v := strings.TrimSpace(config.Version); v != "" && v != "dev" {- return v- }- return "unknown"-}diff --git a/backend/internal/job/auto_update_job.go b/backend/internal/job/auto_update_job.godeleted file mode 100644index 506f0911d..000000000--- a/backend/internal/job/auto_update_job.go+++ /dev/null@@ -1,106 +0,0 @@-package job--import (- "context"- "log/slog"- "time"-- "github.com/getarcaneapp/arcane/backend/internal/services"- "github.com/go-co-op/gocron/v2"-)--type AutoUpdateJob struct {- updaterService *services.UpdaterService- settingsService *services.SettingsService- scheduler *Scheduler-}--func NewAutoUpdateJob(scheduler *Scheduler, updaterService *services.UpdaterService, settingsService *services.SettingsService) *AutoUpdateJob {- return &AutoUpdateJob{- updaterService: updaterService,- settingsService: settingsService,- scheduler: scheduler,- }-}--func (j *AutoUpdateJob) Register(ctx context.Context) error {- autoUpdateEnabled := j.settingsService.GetBoolSetting(ctx, "autoUpdate", false)- pollingEnabled := j.settingsService.GetBoolSetting(ctx, "pollingEnabled", true)- autoUpdateInterval := j.settingsService.GetIntSetting(ctx, "autoUpdateInterval", 1440)-- if !autoUpdateEnabled || !pollingEnabled {- slog.InfoContext(ctx, "auto-update disabled or polling disabled; job not registered",- "autoUpdate", autoUpdateEnabled, "pollingEnabled", pollingEnabled)- return nil- }-- interval := time.Duration(autoUpdateInterval) * time.Minute- if interval < 5*time.Minute {- slog.WarnContext(ctx, "auto-update interval too low; using default",- "requested_minutes", autoUpdateInterval,- "effective_interval", "60m")- interval = 60 * time.Minute- }-- slog.InfoContext(ctx, "registering auto-update job", "interval", interval.String())-- // ensure single instance- j.scheduler.RemoveJobByName("auto-update")-- jobDefinition := gocron.DurationJob(interval)- return j.scheduler.RegisterJob(- ctx,- "auto-update",- jobDefinition,- j.Execute,- false,- )-}--func (j *AutoUpdateJob) Execute(ctx context.Context) error {- slog.InfoContext(ctx, "auto-update run started")-- enabled := j.settingsService.GetBoolSetting(ctx, "autoUpdate", false)- pollingEnabled := j.settingsService.GetBoolSetting(ctx, "pollingEnabled", true)- if !enabled || !pollingEnabled {- slog.InfoContext(ctx, "auto-update disabled or polling disabled; skipping run",- "autoUpdate", enabled, "pollingEnabled", pollingEnabled)- return nil- }-- result, err := j.updaterService.ApplyPending(ctx, false)- if err != nil {- slog.ErrorContext(ctx, "auto-update run failed", "err", err)- return err- }-- slog.InfoContext(ctx, "auto-update run completed",- "checked", result.Checked,- "updated", result.Updated,- "skipped", result.Skipped,- "failed", result.Failed,- )-- return nil-}--func (j *AutoUpdateJob) Reschedule(ctx context.Context) error {- autoUpdateEnabled := j.settingsService.GetBoolSetting(ctx, "autoUpdate", false)- pollingEnabled := j.settingsService.GetBoolSetting(ctx, "pollingEnabled", true)- autoUpdateInterval := j.settingsService.GetIntSetting(ctx, "autoUpdateInterval", 1440)-- if !autoUpdateEnabled || !pollingEnabled {- j.scheduler.RemoveJobByName("auto-update")- slog.InfoContext(ctx, "auto-update disabled or polling disabled; removed job if present",- "autoUpdate", autoUpdateEnabled, "pollingEnabled", pollingEnabled)- return nil- }-- interval := time.Duration(autoUpdateInterval) * time.Minute- if interval < 5*time.Minute {- interval = 60 * time.Minute- }- slog.InfoContext(ctx, "auto-update settings changed; rescheduling", "interval", interval.String())-- return j.scheduler.RescheduleDurationJobByName(ctx, "auto-update", interval, j.Execute, false)-}diff --git a/backend/internal/job/event_cleanup_job.go b/backend/internal/job/event_cleanup_job.godeleted file mode 100644index b01763871..000000000--- a/backend/internal/job/event_cleanup_job.go+++ /dev/null@@ -1,81 +0,0 @@-package job--import (- "context"- "fmt"- "log/slog"- "time"-- "github.com/getarcaneapp/arcane/backend/internal/services"- "github.com/go-co-op/gocron/v2"-)--const EventCleanupJobName = "EventCleanup"--type EventCleanupJob struct {- scheduler *Scheduler- eventService *services.EventService- settingsService *services.SettingsService-}--func NewEventCleanupJob(scheduler *Scheduler, eventService *services.EventService, settingsService *services.SettingsService) *EventCleanupJob {- return &EventCleanupJob{- scheduler: scheduler,- eventService: eventService,- settingsService: settingsService,- }-}--func (j *EventCleanupJob) Register(ctx context.Context) error {- interval := j.getInterval(ctx)-- slog.InfoContext(ctx, "Registering event cleanup job", "jobName", EventCleanupJobName, "interval", interval.String(), "retention", "36h")-- // ensure single instance- j.scheduler.RemoveJobByName(EventCleanupJobName)-- jobDefinition := gocron.DurationJob(interval)- err := j.scheduler.RegisterJob(- ctx,- EventCleanupJobName,- jobDefinition,- j.Execute,- false, // Don't run immediately on startup- )- if err != nil {- return fmt.Errorf("failed to register event cleanup job %q: %w", EventCleanupJobName, err)- }-- return nil-}--func (j *EventCleanupJob) Reschedule(ctx context.Context) error {- interval := j.getInterval(ctx)- slog.InfoContext(ctx, "event cleanup settings changed; rescheduling", "jobName", EventCleanupJobName, "interval", interval.String())- return j.scheduler.RescheduleDurationJobByName(ctx, EventCleanupJobName, interval, j.Execute, false)-}--func (j *EventCleanupJob) getInterval(ctx context.Context) time.Duration {- minutes := j.settingsService.GetIntSetting(ctx, "eventCleanupInterval", 360)- interval := time.Duration(minutes) * time.Minute- if interval < 5*time.Minute {- interval = 6 * time.Hour- }- return interval-}--func (j *EventCleanupJob) Execute(ctx context.Context) error {- slog.InfoContext(ctx, "Running event cleanup job", "jobName", EventCleanupJobName)-- // Delete events older than 36 hours- olderThan := 36 * time.Hour- if err := j.eventService.DeleteOldEvents(ctx, olderThan); err != nil {- slog.ErrorContext(ctx, "Failed to delete old events", "jobName", EventCleanupJobName, "olderThan", olderThan.String(), "error", err)- return err- }-- slog.InfoContext(ctx, "Event cleanup job completed successfully",- "jobName", EventCleanupJobName,- "olderThan", olderThan.String())- return nil-}diff --git a/backend/internal/job/gitops_sync_job.go b/backend/internal/job/gitops_sync_job.godeleted file mode 100644index b750b69b5..000000000--- a/backend/internal/job/gitops_sync_job.go+++ /dev/null@@ -1,69 +0,0 @@-package job--import (- "context"- "log/slog"- "time"-- "github.com/getarcaneapp/arcane/backend/internal/services"- "github.com/go-co-op/gocron/v2"-)--type GitOpsSyncJob struct {- syncService *services.GitOpsSyncService- settingsService *services.SettingsService- scheduler *Scheduler-}--func NewGitOpsSyncJob(scheduler *Scheduler, syncService *services.GitOpsSyncService, settingsService *services.SettingsService) *GitOpsSyncJob {- return &GitOpsSyncJob{- syncService: syncService,- settingsService: settingsService,- scheduler: scheduler,- }-}--func (j *GitOpsSyncJob) Register(ctx context.Context) error {- // Check if GitOps sync is enabled via settings- gitopsSyncEnabled := j.settingsService.GetBoolSetting(ctx, "gitopsSyncEnabled", true)-- if !gitopsSyncEnabled {- slog.InfoContext(ctx, "GitOps sync disabled; job not registered")- return nil- }-- // Default interval: 1 minute to check for due syncs- interval := 1 * time.Minute-- slog.InfoContext(ctx, "registering GitOps sync job", "interval", interval.String())-- // Ensure single instance- j.scheduler.RemoveJobByName("gitops-sync")-- jobDefinition := gocron.DurationJob(interval)- return j.scheduler.RegisterJob(- ctx,- "gitops-sync",- jobDefinition,- j.Execute,- false,- )-}--func (j *GitOpsSyncJob) Execute(ctx context.Context) error {- slog.InfoContext(ctx, "GitOps sync run started")-- enabled := j.settingsService.GetBoolSetting(ctx, "gitopsSyncEnabled", true)- if !enabled {- slog.InfoContext(ctx, "GitOps sync disabled; skipping run")- return nil- }-- if err := j.syncService.SyncAllEnabled(ctx); err != nil {- slog.ErrorContext(ctx, "GitOps sync run failed", "err", err)- return err- }-- slog.InfoContext(ctx, "GitOps sync run completed")- return nil-}diff --git a/backend/internal/job/image_polling_job.go b/backend/internal/job/image_polling_job.godeleted file mode 100644index 9b0b6e9e7..000000000--- a/backend/internal/job/image_polling_job.go+++ /dev/null@@ -1,115 +0,0 @@-package job--import (- "context"- "log/slog"- "time"-- "github.com/getarcaneapp/arcane/backend/internal/services"- "github.com/getarcaneapp/arcane/types/containerregistry"- "github.com/go-co-op/gocron/v2"-)--type ImagePollingJob struct {- imageUpdateService *services.ImageUpdateService- settingsService *services.SettingsService- environmentService *services.EnvironmentService- scheduler *Scheduler-}--func NewImagePollingJob(scheduler *Scheduler, imageUpdateService *services.ImageUpdateService, settingsService *services.SettingsService, environmentService *services.EnvironmentService) *ImagePollingJob {- return &ImagePollingJob{- imageUpdateService: imageUpdateService,- settingsService: settingsService,- environmentService: environmentService,- scheduler: scheduler,- }-}--func (j *ImagePollingJob) Register(ctx context.Context) error {- pollingEnabled := j.settingsService.GetBoolSetting(ctx, "pollingEnabled", true)- pollingInterval := j.settingsService.GetIntSetting(ctx, "pollingInterval", 60)-- if !pollingEnabled {- slog.InfoContext(ctx, "polling disabled; job not registered")- return nil- }-- interval := time.Duration(pollingInterval) * time.Minute- if interval < 5*time.Minute {- slog.WarnContext(ctx, "polling interval too low; using default", "requested_minutes", pollingInterval, "effective_interval", "60m")- interval = 60 * time.Minute- }-- slog.InfoContext(ctx, "registering image polling job", "interval", interval.String())-- j.scheduler.RemoveJobByName("image-polling")-- jobDefinition := gocron.DurationJob(interval)- return j.scheduler.RegisterJob(- ctx,- "image-polling",- jobDefinition,- j.Execute,- false,- )-}--func (j *ImagePollingJob) Execute(ctx context.Context) error {- slog.InfoContext(ctx, "image scan run started")-- creds, err := j.loadRegistryCredentials(ctx)- if err != nil {- slog.WarnContext(ctx, "failed to load registry credentials for polling", "error", err.Error())- creds = nil- }-- results, err := j.imageUpdateService.CheckAllImages(ctx, 0, creds)- if err != nil {- slog.ErrorContext(ctx, "image scan failed", "err", err)- return err- }-- total := len(results)- updates := 0- errors := 0- for _, r := range results {- if r == nil {- continue- }- if r.Error != "" {- errors++- continue- }- if r.HasUpdate {- updates++- }- }-- slog.InfoContext(ctx, "image scan run completed", "checked", total, "updates", updates, "errors", errors)-- return nil-}--func (j *ImagePollingJob) loadRegistryCredentials(ctx context.Context) ([]containerregistry.Credential, error) {- return j.environmentService.GetEnabledRegistryCredentials(ctx)-}--func (j *ImagePollingJob) Reschedule(ctx context.Context) error {- pollingEnabled := j.settingsService.GetBoolSetting(ctx, "pollingEnabled", true)- pollingInterval := j.settingsService.GetIntSetting(ctx, "pollingInterval", 60)-- if !pollingEnabled {- j.scheduler.RemoveJobByName("image-polling")- slog.InfoContext(ctx, "polling disabled; removed image-polling job if present")- return nil- }-- interval := time.Duration(pollingInterval) * time.Minute- if interval < 5*time.Minute {- interval = 60 * time.Minute- }- slog.InfoContext(ctx, "polling settings changed; rescheduling", "interval", interval.String())-- return j.scheduler.RescheduleDurationJobByName(ctx, "image-polling", interval, j.Execute, false)-}diff --git a/backend/internal/job/job.go b/backend/internal/job/job.godeleted file mode 100644index aaa151b37..000000000--- a/backend/internal/job/job.go+++ /dev/null@@ -1,122 +0,0 @@-package job--import (- "context"- "fmt"- "log/slog"- "time"-- "github.com/go-co-op/gocron/v2"- "github.com/google/uuid"-)--type Scheduler struct {- scheduler gocron.Scheduler-}--func NewScheduler() (*Scheduler, error) {- s, err := gocron.NewScheduler()- if err != nil {- return nil, fmt.Errorf("failed to create new gocron scheduler: %w", err)- }- return &Scheduler{scheduler: s}, nil-}--func (s *Scheduler) Run(ctx context.Context) error {- slog.InfoContext(ctx, "Starting job scheduler")- s.scheduler.Start() // Start the scheduler, non-blocking-- time.Sleep(100 * time.Millisecond)- s.LogJobStatus(ctx)-- // Wait for the context to be done (e.g., application shutdown)- <-ctx.Done()-- slog.InfoContext(ctx, "Shutting down job scheduler...")- err := s.scheduler.Shutdown()- if err != nil {- slog.ErrorContext(ctx, "Error shutting down job scheduler", "error", err)- return fmt.Errorf("error during scheduler shutdown: %w", err)- }-- slog.InfoContext(ctx, "Job scheduler shut down successfully")- return nil-}--func (s *Scheduler) RegisterJob(- ctx context.Context,- name string,- definition gocron.JobDefinition,- taskFunc func(ctx context.Context) error,- runImmediately bool,-) error {- jobOptions := []gocron.JobOption{- gocron.WithName(name),- gocron.WithEventListeners(- gocron.BeforeJobRuns(func(jobID uuid.UUID, jobName string) {- slog.Info("Job starting", "name", name, "id", jobID.String())- }),- gocron.AfterJobRuns(func(jobID uuid.UUID, jobName string) {- slog.Info("Job finished successfully", "name", name, "id", jobID.String())- }),- gocron.AfterJobRunsWithError(func(jobID uuid.UUID, jobName string, err error) {- slog.Error("Job failed", "name", name, "id", jobID.String(), "error", err)- }),- ),- }-- task := gocron.NewTask(func() {- if err := taskFunc(ctx); err != nil {- slog.Error("Error executing task function", "name", name, "error", err)- }- })-- var job gocron.Job- var err error- if runImmediately {- job, err = s.scheduler.NewJob(definition, task, append(jobOptions, gocron.WithStartAt(gocron.WithStartImmediately()))...)- } else {- job, err = s.scheduler.NewJob(definition, task, jobOptions...)- }- if err != nil {- return fmt.Errorf("failed to register job %q: %w", name, err)- }-- nextRun, _ := job.NextRun()- slog.InfoContext(ctx, "Job registered successfully", "name", name, "nextRun", nextRun.Format(time.RFC3339), "runImmediately", runImmediately)- return nil-}--func (s *Scheduler) RemoveJobByName(name string) {- for _, j := range s.scheduler.Jobs() {- if j.Name() == name {- _ = s.scheduler.RemoveJob(j.ID())- }- }-}--func (s *Scheduler) RescheduleDurationJobByName(- ctx context.Context,- name string,- interval time.Duration,- taskFunc func(ctx context.Context) error,- runImmediately bool,-) error {- slog.DebugContext(ctx, "Rescheduling job", "name", name, "interval", interval.String())- s.RemoveJobByName(name)- definition := gocron.DurationJob(interval)- return s.RegisterJob(ctx, name, definition, taskFunc, runImmediately)-}--func (s *Scheduler) LogJobStatus(ctx context.Context) {- jobs := s.scheduler.Jobs()- slog.InfoContext(ctx, "Active jobs in scheduler", "count", len(jobs))- for _, j := range jobs {- nextRun, err := j.NextRun()- if err != nil {- slog.WarnContext(ctx, "Could not get next run for job", "name", j.Name(), "error", err)- continue- }- slog.InfoContext(ctx, "Job status", "name", j.Name(), "id", j.ID().String(), "nextRun", nextRun.Format(time.RFC3339))- }-}diff --git a/backend/internal/models/settings.go b/backend/internal/models/settings.goindex 69c6bc548..4cdc17ad5 100644--- a/backend/internal/models/settings.go+++ b/backend/internal/models/settings.go@@ -49,20 +49,20 @@ type Settings struct {BaseServerURL SettingVariable `key:"baseServerUrl" meta:"label=Base Server URL;type=text;keywords=base,url,server,domain,host,endpoint,address,link;category=general;description=Set the base URL for the application"`EnableGravatar SettingVariable `key:"enableGravatar" meta:"label=Enable Gravatar;type=boolean;keywords=gravatar,avatar,profile,picture,image,user,photo;category=general;description=Enable Gravatar profile pictures for users"`DefaultShell SettingVariable `key:"defaultShell" meta:"label=Default Shell;type=text;keywords=shell,default,shellpath,path,login;category=general;description=Default shell to use for commands"`- EnvironmentHealthInterval SettingVariable `key:"environmentHealthInterval" meta:"label=Environment Health Check Interval;type=number;keywords=environment,health,check,interval,frequency,heartbeat,status,monitoring,uptime,jobs,schedule;category=jobschedule;description=How often to check environment connectivity in minutes (default: 2)" catmeta:"id=jobschedule;title=Job Schedule;icon=jobs;url=/settings/jobs;description=Configure how often Arcane background jobs run"`+ EnvironmentHealthInterval SettingVariable `key:"environmentHealthInterval" meta:"label=Environment Health Check Interval;type=cron;keywords=environment,health,check,interval,frequency,heartbeat,status,monitoring,uptime,jobs,schedule;category=jobschedule;description=How often to check environment connectivity (cron expression)" catmeta:"id=jobschedule;title=Job Schedule;icon=jobs;url=/settings/jobs;description=Configure how often Arcane background jobs run"`AccentColor SettingVariable `key:"accentColor,public,local" meta:"label=Accent Color;type=text;keywords=color,accent,theme,css,appearance,ui;category=general;description=Primary accent color for UI"`// Docker categoryAutoUpdate SettingVariable `key:"autoUpdate" meta:"label=Auto Update;type=boolean;keywords=auto,update,automatic,upgrade,refresh,restart,deploy;category=internal;description=Automatically update containers when new images are available"`- AutoUpdateInterval SettingVariable `key:"autoUpdateInterval" meta:"label=Auto Update Interval;type=number;keywords=auto,update,interval,frequency,schedule,automatic,timing;category=internal;description=Interval between automatic updates"`+ AutoUpdateInterval SettingVariable `key:"autoUpdateInterval" meta:"label=Auto Update Interval;type=cron;keywords=auto,update,interval,frequency,schedule,automatic,timing;category=internal;description=How often to check for automatic updates (cron expression)"`PollingEnabled SettingVariable `key:"pollingEnabled" meta:"label=Enable Polling;type=boolean;keywords=polling,check,monitor,watch,scan,detection,automatic;category=internal;description=Enable automatic checking for image updates"`- PollingInterval SettingVariable `key:"pollingInterval" meta:"label=Polling Interval;type=number;keywords=interval,frequency,schedule,time,minutes,period,delay;category=internal;description=How often to check for image updates"`- EventCleanupInterval SettingVariable `key:"eventCleanupInterval" meta:"label=Event Cleanup Interval;type=number;keywords=events,cleanup,retention,interval,frequency,schedule,history,logs,jobs;category=jobschedule;description=How often to delete old events in minutes"`- AnalyticsHeartbeatInterval SettingVariable `key:"analyticsHeartbeatInterval" meta:"label=Analytics Heartbeat Interval;type=number;keywords=analytics,heartbeat,interval,frequency,schedule,telemetry,jobs;category=jobschedule;description=How often to send the anonymous analytics heartbeat in minutes"`+ PollingInterval SettingVariable `key:"pollingInterval" meta:"label=Polling Interval;type=cron;keywords=interval,frequency,schedule,time,minutes,period,delay;category=internal;description=How often to check for image updates (cron expression)"`+ EventCleanupInterval SettingVariable `key:"eventCleanupInterval" meta:"label=Event Cleanup Interval;type=cron;keywords=events,cleanup,retention,interval,frequency,schedule,history,logs,jobs;category=jobschedule;description=How often to delete old events (cron expression)"`+ AnalyticsHeartbeatInterval SettingVariable `key:"analyticsHeartbeatInterval" meta:"label=Analytics Heartbeat Interval;type=cron;keywords=analytics,heartbeat,interval,frequency,schedule,telemetry,jobs;category=jobschedule;description=How often to send the anonymous analytics heartbeat (cron expression)"`AutoInjectEnv SettingVariable `key:"autoInjectEnv" meta:"label=Auto Inject Env Variables;type=boolean;keywords=auto,inject,env,environment,variables,interpolation;category=internal;description=Automatically inject project .env variables into all containers (default: false)"`PruneMode SettingVariable `key:"dockerPruneMode" meta:"label=Docker Prune Action;type=select;keywords=prune,cleanup,clean,remove,delete,unused,dangling,space,disk;category=internal;description=Configure how unused Docker images are cleaned up"`ScheduledPruneEnabled SettingVariable `key:"scheduledPruneEnabled" meta:"label=Scheduled Prune Enabled;type=boolean;keywords=prune,cleanup,maintenance,schedule,automatic;category=internal;description=Enable scheduled pruning of unused Docker resources"`- ScheduledPruneInterval SettingVariable `key:"scheduledPruneInterval" meta:"label=Scheduled Prune Interval;type=number;keywords=prune,cleanup,interval,minutes,schedule;category=internal;description=Minutes between scheduled prune runs"`+ ScheduledPruneInterval SettingVariable `key:"scheduledPruneInterval" meta:"label=Scheduled Prune Interval;type=cron;keywords=prune,cleanup,interval,minutes,schedule;category=internal;description=How often to run scheduled prunes (cron expression)"`ScheduledPruneContainers SettingVariable `key:"scheduledPruneContainers" meta:"label=Scheduled Prune Containers;type=boolean;keywords=prune,containers,cleanup,maintenance;category=internal;description=Remove stopped containers during scheduled prune"`ScheduledPruneImages SettingVariable `key:"scheduledPruneImages" meta:"label=Scheduled Prune Images;type=boolean;keywords=prune,images,cleanup,maintenance;category=internal;description=Remove unused images during scheduled prune"`ScheduledPruneVolumes SettingVariable `key:"scheduledPruneVolumes" meta:"label=Scheduled Prune Volumes;type=boolean;keywords=prune,volumes,cleanup,maintenance;category=internal;description=Remove unused volumes during scheduled prune"`diff --git a/backend/internal/services/gitops_sync_service.go b/backend/internal/services/gitops_sync_service.goindex 7d03d466e..75d8973b1 100644--- a/backend/internal/services/gitops_sync_service.go+++ b/backend/internal/services/gitops_sync_service.go@@ -10,6 +10,7 @@ import ("github.com/getarcaneapp/arcane/backend/internal/database""github.com/getarcaneapp/arcane/backend/internal/models"+ bootstraputils "github.com/getarcaneapp/arcane/backend/internal/utils""github.com/getarcaneapp/arcane/backend/internal/utils/mapper""github.com/getarcaneapp/arcane/backend/internal/utils/pagination""github.com/getarcaneapp/arcane/types/gitops"@@ -34,6 +35,42 @@ func NewGitOpsSyncService(db *database.DB, repoService *GitRepositoryService, pr}}+func (s *GitOpsSyncService) ListSyncIntervalsRaw(ctx context.Context) ([]bootstraputils.IntervalMigrationItem, error) {+ rows, err := s.db.WithContext(ctx).Raw("SELECT id, sync_interval FROM gitops_syncs").Rows()+ if err != nil {+ return nil, fmt.Errorf("failed to load git sync intervals: %w", err)+ }+ defer rows.Close()++ items := make([]bootstraputils.IntervalMigrationItem, 0)+ for rows.Next() {+ var id string+ var raw any+ if err := rows.Scan(&id, &raw); err != nil {+ return nil, fmt.Errorf("failed to scan git sync interval: %w", err)+ }+ items = append(items, bootstraputils.IntervalMigrationItem{+ ID: id,+ RawValue: strings.TrimSpace(fmt.Sprint(raw)),+ })+ }+ if err := rows.Err(); err != nil {+ return nil, fmt.Errorf("failed to read git sync intervals: %w", err)+ }++ return items, nil+}++func (s *GitOpsSyncService) UpdateSyncIntervalMinutes(ctx context.Context, id string, minutes int) error {+ if minutes <= 0 {+ return fmt.Errorf("sync interval must be positive")+ }+ return s.db.WithContext(ctx).+ Model(&models.GitOpsSync{}).+ Where("id = ?", id).+ Update("sync_interval", minutes).Error+}+func (s *GitOpsSyncService) GetSyncsPaginated(ctx context.Context, environmentID string, params pagination.QueryParams) ([]gitops.GitOpsSync, pagination.Response, error) {var syncs []models.GitOpsSyncq := s.db.WithContext(ctx).Model(&models.GitOpsSync{}).Preload("Repository").Preload("Project").diff --git a/backend/internal/services/job_service.go b/backend/internal/services/job_service.goindex ccc2fc58e..3783c2fb9 100644--- a/backend/internal/services/job_service.go+++ b/backend/internal/services/job_service.go@@ -3,12 +3,12 @@ package servicesimport ("context""fmt"- "strconv""github.com/getarcaneapp/arcane/backend/internal/config""github.com/getarcaneapp/arcane/backend/internal/database""github.com/getarcaneapp/arcane/backend/internal/models""github.com/getarcaneapp/arcane/types/jobschedule"+ "github.com/robfig/cron/v3""gorm.io/gorm")@@ -35,9 +35,9 @@ func NewJobService(db *database.DB, settings *SettingsService, cfg *config.Confifunc (s *JobService) GetJobSchedules(ctx context.Context) jobschedule.Config {// Use SettingsService cache for fast reads.return jobschedule.Config{- EnvironmentHealthInterval: s.settings.GetIntSetting(ctx, "environmentHealthInterval", 2),- EventCleanupInterval: s.settings.GetIntSetting(ctx, "eventCleanupInterval", 360),- AnalyticsHeartbeatInterval: s.settings.GetIntSetting(ctx, "analyticsHeartbeatInterval", 1440),+ EnvironmentHealthInterval: s.settings.GetStringSetting(ctx, "environmentHealthInterval", "0 */2 * * * *"),+ EventCleanupInterval: s.settings.GetStringSetting(ctx, "eventCleanupInterval", "0 0 */6 * * *"),+ AnalyticsHeartbeatInterval: s.settings.GetStringSetting(ctx, "analyticsHeartbeatInterval", "0 0 0 * * *"),}}@@ -51,30 +51,29 @@ func (s *JobService) UpdateJobSchedules(ctx context.Context, updates jobschedulecurrent := s.GetJobSchedules(ctx)- // Validate inputs (minutes)- validate := func(name string, v *int, min, max int) error {- if v == nil {+ // Validate inputs (cron expressions)+ validate := func(name string, v *string) error {+ if v == nil || *v == "" {return nil}- if *v < min || *v > max {- return fmt.Errorf("%s must be between %d and %d minutes", name, min, max)+ if _, err := cron.NewParser(cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow).Parse(*v); err != nil {+ return fmt.Errorf("invalid cron expression for %s: %w", name, err)}return nil}- // Keep bounds aligned with job-level guardrails.- if err := validate("environmentHealthInterval", updates.EnvironmentHealthInterval, 1, 60); err != nil {+ if err := validate("environmentHealthInterval", updates.EnvironmentHealthInterval); err != nil {return jobschedule.Config{}, err}- if err := validate("eventCleanupInterval", updates.EventCleanupInterval, 5, 10080); err != nil {+ if err := validate("eventCleanupInterval", updates.EventCleanupInterval); err != nil {return jobschedule.Config{}, err}- if err := validate("analyticsHeartbeatInterval", updates.AnalyticsHeartbeatInterval, 60, 43200); err != nil {+ if err := validate("analyticsHeartbeatInterval", updates.AnalyticsHeartbeatInterval); err != nil {return jobschedule.Config{}, err}changed := false- upsert := func(tx *gorm.DB, key string, v *int, currentVal int) error {+ upsert := func(tx *gorm.DB, key string, v *string, currentVal string) error {if v == nil {return nil}@@ -82,7 +81,7 @@ func (s *JobService) UpdateJobSchedules(ctx context.Context, updates jobschedulereturn nil}changed = true- return tx.Save(&models.SettingVariable{Key: key, Value: strconv.Itoa(*v)}).Error+ return tx.Save(&models.SettingVariable{Key: key, Value: *v}).Error}err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {diff --git a/backend/internal/services/settings_service.go b/backend/internal/services/settings_service.goindex 527a79071..f4ca21c62 100644--- a/backend/internal/services/settings_service.go+++ b/backend/internal/services/settings_service.go@@ -18,6 +18,7 @@ import ("time""github.com/hashicorp/go-uuid"+ "github.com/robfig/cron/v3""gorm.io/gorm""github.com/getarcaneapp/arcane/backend/internal/config"@@ -85,15 +86,15 @@ func (s *SettingsService) getDefaultSettings() *models.Settings {ProjectsDirectory: models.SettingVariable{Value: "/app/data/projects"},DiskUsagePath: models.SettingVariable{Value: "/app/data/projects"},AutoUpdate: models.SettingVariable{Value: "false"},- AutoUpdateInterval: models.SettingVariable{Value: "1440"},+ AutoUpdateInterval: models.SettingVariable{Value: "0 0 0 * * *"},PollingEnabled: models.SettingVariable{Value: "true"},- PollingInterval: models.SettingVariable{Value: "60"},- EventCleanupInterval: models.SettingVariable{Value: "360"},- AnalyticsHeartbeatInterval: models.SettingVariable{Value: "1440"},+ PollingInterval: models.SettingVariable{Value: "0 0 * * * *"},+ EventCleanupInterval: models.SettingVariable{Value: "0 0 */6 * * *"},+ AnalyticsHeartbeatInterval: models.SettingVariable{Value: "0 0 0 * * *"},AutoInjectEnv: models.SettingVariable{Value: "false"},PruneMode: models.SettingVariable{Value: "dangling"},ScheduledPruneEnabled: models.SettingVariable{Value: "false"},- ScheduledPruneInterval: models.SettingVariable{Value: "1440"},+ ScheduledPruneInterval: models.SettingVariable{Value: "0 0 0 * * *"},ScheduledPruneContainers: models.SettingVariable{Value: "true"},ScheduledPruneImages: models.SettingVariable{Value: "true"},ScheduledPruneVolumes: models.SettingVariable{Value: "false"},@@ -127,7 +128,7 @@ func (s *SettingsService) getDefaultSettings() *models.Settings {SidebarHoverExpansion: models.SettingVariable{Value: "true"},AccentColor: models.SettingVariable{Value: "oklch(0.606 0.25 292.717)"},MaxImageUploadSize: models.SettingVariable{Value: "500"},- EnvironmentHealthInterval: models.SettingVariable{Value: "2"},+ EnvironmentHealthInterval: models.SettingVariable{Value: "0 */2 * * * *"},DockerAPITimeout: models.SettingVariable{Value: "30"},DockerImagePullTimeout: models.SettingVariable{Value: "600"},@@ -487,12 +488,11 @@ func (s *SettingsService) prepareUpdateValues(updates settings.Update, cfg, defavalue = fieldValue.Elem().String()}- // Validate scheduled prune interval bounds (60-10080 minutes)- if key == "scheduledPruneInterval" && value != "" {- if minutes, err := strconv.Atoi(value); err != nil {- return nil, false, false, false, fmt.Errorf("invalid scheduledPruneInterval: %w", err)- } else if minutes < 60 || minutes > 10080 {- return nil, false, false, false, fmt.Errorf("scheduledPruneInterval must be between 60 and 10080 minutes")+ // Validate cron settings+ cronFields := []string{"scheduledPruneInterval", "autoUpdateInterval", "pollingInterval", "environmentHealthInterval", "eventCleanupInterval", "analyticsHeartbeatInterval"}+ if slices.Contains(cronFields, key) && value != "" {+ if _, err := cron.NewParser(cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow).Parse(value); err != nil {+ return nil, false, false, false, fmt.Errorf("invalid cron expression for %s: %w", key, err)}}diff --git a/backend/internal/utils/bootstrap_util.go b/backend/internal/utils/bootstrap_util.goindex 753191bc3..ac3232f69 100644--- a/backend/internal/utils/bootstrap_util.go+++ b/backend/internal/utils/bootstrap_util.go@@ -2,7 +2,11 @@ package utilsimport ("context"+ "fmt""log/slog"+ "strconv"+ "strings"+ "time""github.com/getarcaneapp/arcane/backend/internal/config")@@ -37,6 +41,11 @@ type SettingsPruner interface {PruneUnknownSettings(ctx context.Context) error}+type IntervalMigrationItem struct {+ ID string+ RawValue string+}+func InitializeDefaultSettings(ctx context.Context, cfg *config.Config, settingsMgr SettingsManager) {slog.InfoContext(ctx, "Ensuring default settings are initialized")@@ -88,3 +97,334 @@ func InitializeNonAgentFeatures(ctx context.Context, cfg *config.Config, createA}}}++func MigrateSchedulerCronValues(+ ctx context.Context,+ getSettingFunc func(context.Context, string, string) string,+ updateSettingFunc func(context.Context, string, string) error,+ reloadFunc func(context.Context) error,+) {+ migrations := []struct {+ key string+ defaultUnit time.Duration+ }{+ {key: "pollingInterval", defaultUnit: time.Minute},+ {key: "autoUpdateInterval", defaultUnit: time.Minute},+ {key: "scheduledPruneInterval", defaultUnit: time.Minute},+ {key: "environmentHealthInterval", defaultUnit: time.Minute},+ {key: "eventCleanupInterval", defaultUnit: time.Minute},+ {key: "analyticsHeartbeatInterval", defaultUnit: time.Minute},+ }++ changed := false+ for _, item := range migrations {+ raw := strings.TrimSpace(getSettingFunc(ctx, item.key, ""))+ if raw == "" {+ continue+ }++ cronValue, shouldUpdate, warn := normalizeSchedulerValueToCron(raw, item.defaultUnit)+ if warn != "" {+ slog.WarnContext(ctx, warn, "key", item.key, "value", raw)+ }+ if !shouldUpdate {+ continue+ }+ if cronValue == "" || cronValue == raw {+ continue+ }+ if err := updateSettingFunc(ctx, item.key, cronValue); err != nil {+ slog.WarnContext(ctx, "Failed to migrate scheduler setting", "key", item.key, "error", err.Error())+ continue+ }+ slog.InfoContext(ctx, "Migrated scheduler setting to cron", "key", item.key, "value", cronValue)+ changed = true+ }++ if changed && reloadFunc != nil {+ if err := reloadFunc(ctx); err != nil {+ slog.WarnContext(ctx, "Failed to reload settings after scheduler migration", "error", err.Error())+ }+ }+}++func MigrateGitOpsSyncIntervals(+ ctx context.Context,+ listFunc func(context.Context) ([]IntervalMigrationItem, error),+ updateFunc func(context.Context, string, int) error,+) {+ if listFunc == nil || updateFunc == nil {+ return+ }++ items, err := listFunc(ctx)+ if err != nil {+ slog.WarnContext(ctx, "Failed to load git sync intervals for migration", "error", err.Error())+ return+ }++ for _, item := range items {+ raw := strings.TrimSpace(item.RawValue)+ if raw == "" {+ continue+ }+ minutes, shouldUpdate, warn := normalizeSchedulerValueToMinutes(raw, time.Minute)+ if warn != "" {+ slog.WarnContext(ctx, warn, "syncId", item.ID, "value", raw)+ }+ if !shouldUpdate || minutes <= 0 {+ continue+ }+ if err := updateFunc(ctx, item.ID, minutes); err != nil {+ slog.WarnContext(ctx, "Failed to migrate git sync interval", "syncId", item.ID, "error", err.Error())+ continue+ }+ slog.InfoContext(ctx, "Migrated git sync interval", "syncId", item.ID, "minutes", minutes)+ }+}++func normalizeSchedulerValueToCron(raw string, defaultUnit time.Duration) (string, bool, string) {+ trimmed := strings.TrimSpace(raw)+ if trimmed == "" {+ return "", false, ""+ }++ if strings.HasPrefix(trimmed, "@") {+ return trimmed, false, ""+ }++ if strings.HasPrefix(trimmed, "CRON_TZ=") || strings.HasPrefix(trimmed, "TZ=") {+ parts := strings.Fields(trimmed)+ if len(parts) == 6 {+ return parts[0] + " 0 " + strings.Join(parts[1:], " "), true, ""+ }+ return trimmed, false, ""+ }++ fields := strings.Fields(trimmed)+ if len(fields) == 5 {+ return "0 " + trimmed, true, ""+ }+ if len(fields) >= 6 {+ return trimmed, false, ""+ }++ duration, ok := parseSchedulerDuration(trimmed, defaultUnit)+ if !ok {+ return "", false, ""+ }++ cronValue, warn := durationToCron(duration)+ if cronValue == "" {+ return "", false, warn+ }++ return cronValue, true, warn+}++func parseSchedulerDuration(raw string, defaultUnit time.Duration) (time.Duration, bool) {+ if raw == "" {+ return 0, false+ }+ if d, err := time.ParseDuration(raw); err == nil {+ return d, true+ }+ value, err := strconv.Atoi(raw)+ if err != nil {+ return 0, false+ }+ if defaultUnit <= 0 {+ defaultUnit = time.Minute+ }+ return time.Duration(value) * defaultUnit, true+}++func durationToCron(d time.Duration) (string, string) {+ if d <= 0 {+ return "", ""+ }++ if d < time.Minute {+ seconds := int(d.Seconds())+ if d%time.Second != 0 {+ seconds+++ }+ if seconds < 1 {+ return "", ""+ }+ if seconds >= 60 {+ minutes := (seconds + 59) / 60+ return minutesToCron(minutes), "scheduler interval rounded up to minutes"+ }+ return fmt.Sprintf("*/%d * * * * *", seconds), ""+ }++ if d%time.Minute != 0 {+ seconds := int(d.Seconds())+ if d%time.Second != 0 {+ seconds+++ }+ minutes := (seconds + 59) / 60+ return minutesToCron(minutes), "scheduler interval rounded up to minutes"+ }++ minutes := int(d.Minutes())+ return minutesToCron(minutes), ""+}++func minutesToCron(minutes int) string {+ if minutes <= 0 {+ return ""+ }+ if minutes < 60 {+ return fmt.Sprintf("0 */%d * * * *", minutes)+ }+ if minutes%60 == 0 {+ hours := minutes / 60+ if hours < 24 {+ return fmt.Sprintf("0 0 */%d * * *", hours)+ }+ if hours%24 == 0 {+ days := hours / 24+ return fmt.Sprintf("0 0 0 */%d * *", days)+ }+ }+ return fmt.Sprintf("0 */%d * * * *", minutes)+}++func normalizeSchedulerValueToMinutes(raw string, defaultUnit time.Duration) (int, bool, string) {+ trimmed := strings.TrimSpace(raw)+ if trimmed == "" {+ return 0, false, ""+ }++ if strings.HasPrefix(trimmed, "@") {+ return 0, false, ""+ }++ if strings.HasPrefix(trimmed, "CRON_TZ=") || strings.HasPrefix(trimmed, "TZ=") {+ parts := strings.Fields(trimmed)+ if len(parts) >= 6 {+ minutes, ok, warn := cronToMinutes(parts[1:])+ if ok {+ return minutes, true, warn+ }+ }+ return 0, false, ""+ }++ fields := strings.Fields(trimmed)+ if len(fields) >= 5 {+ if len(fields) == 5 {+ fields = append([]string{"0"}, fields...)+ }+ minutes, ok, warn := cronToMinutes(fields)+ if ok {+ return minutes, true, warn+ }+ return 0, false, ""+ }++ duration, ok := parseSchedulerDuration(trimmed, defaultUnit)+ if !ok {+ return 0, false, ""+ }++ minutes := int(duration / time.Minute)+ warn := ""+ if duration%time.Minute != 0 {+ minutes+++ warn = "scheduler interval rounded up to minutes"+ }+ if minutes < 1 {+ minutes = 1+ warn = "scheduler interval rounded up to minutes"+ }+ if strconv.Itoa(minutes) == trimmed {+ return minutes, false, warn+ }++ return minutes, true, warn+}++func cronToMinutes(fields []string) (int, bool, string) {+ if len(fields) < 6 {+ return 0, false, ""+ }++ sec, min, hour, day, month, weekday := fields[0], fields[1], fields[2], fields[3], fields[4], fields[5]++ if mins, ok, warn := tryConvertSecondStep(sec, min, hour, day, month, weekday); ok {+ return mins, ok, warn+ }+ if mins, ok, warn := tryConvertMinuteOrHour(sec, min, hour, day, month, weekday); ok {+ return mins, ok, warn+ }+ if mins, ok, warn := tryConvertDayStep(sec, min, hour, day, month, weekday); ok {+ return mins, ok, warn+ }++ return 0, false, ""+}++func tryConvertSecondStep(sec, min, hour, day, month, weekday string) (int, bool, string) {+ step, ok := parseCronStep(sec)+ if !ok {+ return 0, false, ""+ }++ if min == "*" && hour == "*" && day == "*" && month == "*" && weekday == "*" {+ minutes := (step + 59) / 60+ if minutes < 1 {+ minutes = 1+ }+ warn := ""+ if step%60 != 0 {+ warn = "scheduler interval rounded up to minutes"+ }+ return minutes, true, warn+ }+ return 0, false, ""+}++func tryConvertMinuteOrHour(sec, min, hour, day, month, weekday string) (int, bool, string) {+ if (sec != "0" && sec != "*") || day != "*" || month != "*" || weekday != "*" {+ return 0, false, ""+ }++ if step, ok := parseCronStep(min); ok && hour == "*" {+ return step, true, ""+ }+ if min == "*" && hour == "*" {+ return 1, true, ""+ }+ if min == "0" {+ if step, ok := parseCronStep(hour); ok && day == "*" {+ return step * 60, true, ""+ }+ if hour == "*" {+ return 60, true, ""+ }+ }+ return 0, false, ""+}++func tryConvertDayStep(sec, min, hour, day, month, weekday string) (int, bool, string) {+ if (sec == "0" || sec == "*") && min == "0" && hour == "0" && month == "*" && weekday == "*" {+ if step, ok := parseCronStep(day); ok {+ return step * 1440, true, ""+ }+ }+ return 0, false, ""+}++func parseCronStep(field string) (int, bool) {+ if !strings.HasPrefix(field, "*/") {+ return 0, false+ }+ step, err := strconv.Atoi(strings.TrimPrefix(field, "*/"))+ if err != nil || step <= 0 {+ return 0, false+ }+ return step, true+}diff --git a/backend/internal/utils/bootstrap_util_test.go b/backend/internal/utils/bootstrap_util_test.gonew file mode 100644index 000000000..aa30f14c8--- /dev/null+++ b/backend/internal/utils/bootstrap_util_test.go@@ -0,0 +1,726 @@+package utils++import (+ "context"+ "errors"+ "testing"+ "time"++ "github.com/getarcaneapp/arcane/backend/internal/config"+ "github.com/stretchr/testify/assert"+)++type mockSettingsManager struct {+ persistCalled bool+ setBoolCalled bool+ ensureDefaultsCalled bool+ persistErr error+ setBoolErr error+ ensureDefaultsErr error+}++func (m *mockSettingsManager) PersistEnvSettingsIfMissing(ctx context.Context) error {+ m.persistCalled = true+ return m.persistErr+}++func (m *mockSettingsManager) SetBoolSetting(ctx context.Context, key string, value bool) error {+ m.setBoolCalled = true+ return m.setBoolErr+}++func (m *mockSettingsManager) EnsureDefaultSettings(ctx context.Context) error {+ m.ensureDefaultsCalled = true+ return m.ensureDefaultsErr+}++type mockSettingsPruner struct {+ pruneCalled bool+ pruneErr error+}++func (m *mockSettingsPruner) PruneUnknownSettings(ctx context.Context) error {+ m.pruneCalled = true+ return m.pruneErr+}++func TestLoadAgentToken(t *testing.T) {+ ctx := context.Background()++ t.Run("loads token when agent mode and token empty", func(t *testing.T) {+ cfg := &config.Config{+ AgentMode: true,+ AgentToken: "",+ }+ getSettingFunc := func(ctx context.Context, key string, def string) string {+ if key == "agentToken" {+ return "test-token-123"+ }+ return def+ }++ LoadAgentToken(ctx, cfg, getSettingFunc)++ assert.Equal(t, "test-token-123", cfg.AgentToken)+ })++ t.Run("does not load when not in agent mode", func(t *testing.T) {+ cfg := &config.Config{+ AgentMode: false,+ AgentToken: "",+ }+ getSettingFunc := func(ctx context.Context, key string, def string) string {+ return "test-token-123"+ }++ LoadAgentToken(ctx, cfg, getSettingFunc)++ assert.Empty(t, cfg.AgentToken)+ })++ t.Run("does not override existing token", func(t *testing.T) {+ cfg := &config.Config{+ AgentMode: true,+ AgentToken: "existing-token",+ }+ getSettingFunc := func(ctx context.Context, key string, def string) string {+ return "new-token"+ }++ LoadAgentToken(ctx, cfg, getSettingFunc)++ assert.Equal(t, "existing-token", cfg.AgentToken)+ })++ t.Run("handles empty token from database", func(t *testing.T) {+ cfg := &config.Config{+ AgentMode: true,+ AgentToken: "",+ }+ getSettingFunc := func(ctx context.Context, key string, def string) string {+ return ""+ }++ LoadAgentToken(ctx, cfg, getSettingFunc)++ assert.Empty(t, cfg.AgentToken)+ })+}++func TestEnsureEncryptionKey(t *testing.T) {+ ctx := context.Background()++ t.Run("sets key in agent mode", func(t *testing.T) {+ cfg := &config.Config{+ AgentMode: true,+ EncryptionKey: "",+ }+ ensureKeyFunc := func(ctx context.Context) (string, error) {+ return "generated-key", nil+ }++ EnsureEncryptionKey(ctx, cfg, ensureKeyFunc)++ assert.Equal(t, "generated-key", cfg.EncryptionKey)+ })++ t.Run("sets key in non-production", func(t *testing.T) {+ cfg := &config.Config{+ AgentMode: false,+ Environment: "development",+ EncryptionKey: "",+ }+ ensureKeyFunc := func(ctx context.Context) (string, error) {+ return "dev-key", nil+ }++ EnsureEncryptionKey(ctx, cfg, ensureKeyFunc)++ assert.Equal(t, "dev-key", cfg.EncryptionKey)+ })++ t.Run("does not set key in production when not agent", func(t *testing.T) {+ cfg := &config.Config{+ AgentMode: false,+ Environment: "production",+ EncryptionKey: "",+ }+ ensureKeyFunc := func(ctx context.Context) (string, error) {+ return "should-not-set", nil+ }++ EnsureEncryptionKey(ctx, cfg, ensureKeyFunc)++ assert.Empty(t, cfg.EncryptionKey)+ })++ t.Run("handles error gracefully", func(t *testing.T) {+ cfg := &config.Config{+ AgentMode: true,+ EncryptionKey: "",+ }+ ensureKeyFunc := func(ctx context.Context) (string, error) {+ return "", errors.New("key generation failed")+ }++ EnsureEncryptionKey(ctx, cfg, ensureKeyFunc)++ assert.Empty(t, cfg.EncryptionKey)+ })+}++func TestInitializeDefaultSettings(t *testing.T) {+ ctx := context.Background()+ cfg := &config.Config{}++ t.Run("calls all initialization methods", func(t *testing.T) {+ mgr := &mockSettingsManager{}++ InitializeDefaultSettings(ctx, cfg, mgr)++ assert.True(t, mgr.ensureDefaultsCalled)+ assert.True(t, mgr.persistCalled)+ })++ t.Run("handles ensure defaults error", func(t *testing.T) {+ mgr := &mockSettingsManager{+ ensureDefaultsErr: errors.New("defaults failed"),+ }++ InitializeDefaultSettings(ctx, cfg, mgr)++ assert.True(t, mgr.ensureDefaultsCalled)+ assert.True(t, mgr.persistCalled)+ })++ t.Run("handles persist error", func(t *testing.T) {+ mgr := &mockSettingsManager{+ persistErr: errors.New("persist failed"),+ }++ InitializeDefaultSettings(ctx, cfg, mgr)++ assert.True(t, mgr.ensureDefaultsCalled)+ assert.True(t, mgr.persistCalled)+ })+}++func TestCleanupUnknownSettings(t *testing.T) {+ ctx := context.Background()++ t.Run("calls prune method", func(t *testing.T) {+ pruner := &mockSettingsPruner{}++ CleanupUnknownSettings(ctx, pruner)++ assert.True(t, pruner.pruneCalled)+ })++ t.Run("handles error gracefully", func(t *testing.T) {+ pruner := &mockSettingsPruner{+ pruneErr: errors.New("prune failed"),+ }++ CleanupUnknownSettings(ctx, pruner)++ assert.True(t, pruner.pruneCalled)+ })+}++func TestTestDockerConnection(t *testing.T) {+ ctx := context.Background()++ t.Run("executes test function", func(t *testing.T) {+ called := false+ testFunc := func(ctx context.Context) error {+ called = true+ return nil+ }++ TestDockerConnection(ctx, testFunc)++ assert.True(t, called)+ })++ t.Run("handles error gracefully", func(t *testing.T) {+ testFunc := func(ctx context.Context) error {+ return errors.New("docker connection failed")+ }++ TestDockerConnection(ctx, testFunc)+ })+}++func TestInitializeNonAgentFeatures(t *testing.T) {+ ctx := context.Background()++ t.Run("skips in agent mode", func(t *testing.T) {+ cfg := &config.Config{AgentMode: true}+ createAdminCalled := false+ createAdminFunc := func(ctx context.Context) error {+ createAdminCalled = true+ return nil+ }++ InitializeNonAgentFeatures(ctx, cfg, createAdminFunc, nil, nil)++ assert.False(t, createAdminCalled)+ })++ t.Run("calls all functions in non-agent mode", func(t *testing.T) {+ cfg := &config.Config{AgentMode: false}+ createAdminCalled := false+ migrateOidcCalled := false+ migrateDiscordCalled := false++ createAdminFunc := func(ctx context.Context) error {+ createAdminCalled = true+ return nil+ }+ migrateOidcFunc := func(ctx context.Context) error {+ migrateOidcCalled = true+ return nil+ }+ migrateDiscordFunc := func(ctx context.Context) error {+ migrateDiscordCalled = true+ return nil+ }++ InitializeNonAgentFeatures(ctx, cfg, createAdminFunc, migrateOidcFunc, migrateDiscordFunc)++ assert.True(t, createAdminCalled)+ assert.True(t, migrateOidcCalled)+ assert.True(t, migrateDiscordCalled)+ })++ t.Run("handles errors gracefully", func(t *testing.T) {+ cfg := &config.Config{AgentMode: false}+ createAdminFunc := func(ctx context.Context) error {+ return errors.New("admin creation failed")+ }+ migrateOidcFunc := func(ctx context.Context) error {+ return errors.New("oidc migration failed")+ }+ migrateDiscordFunc := func(ctx context.Context) error {+ return errors.New("discord migration failed")+ }++ InitializeNonAgentFeatures(ctx, cfg, createAdminFunc, migrateOidcFunc, migrateDiscordFunc)+ })+}++func TestMigrateSchedulerCronValues(t *testing.T) {+ ctx := context.Background()++ t.Run("migrates minute-based intervals to cron", func(t *testing.T) {+ settings := map[string]string{+ "pollingInterval": "15",+ }+ updateCalled := false++ getSettingFunc := func(ctx context.Context, key string, def string) string {+ if val, ok := settings[key]; ok {+ return val+ }+ return def+ }+ updateSettingFunc := func(ctx context.Context, key string, value string) error {+ updateCalled = true+ assert.Equal(t, "pollingInterval", key)+ assert.Equal(t, "0 */15 * * * *", value)+ return nil+ }+ reloadFunc := func(ctx context.Context) error {+ return nil+ }++ MigrateSchedulerCronValues(ctx, getSettingFunc, updateSettingFunc, reloadFunc)++ assert.True(t, updateCalled)+ })++ t.Run("migrates duration strings", func(t *testing.T) {+ settings := map[string]string{+ "autoUpdateInterval": "30m",+ }+ updateCalled := false++ getSettingFunc := func(ctx context.Context, key string, def string) string {+ if val, ok := settings[key]; ok {+ return val+ }+ return def+ }+ updateSettingFunc := func(ctx context.Context, key string, value string) error {+ updateCalled = true+ assert.Equal(t, "autoUpdateInterval", key)+ assert.Equal(t, "0 */30 * * * *", value)+ return nil+ }+ reloadFunc := func(ctx context.Context) error {+ return nil+ }++ MigrateSchedulerCronValues(ctx, getSettingFunc, updateSettingFunc, reloadFunc)++ assert.True(t, updateCalled)+ })++ t.Run("skips already valid cron expressions", func(t *testing.T) {+ settings := map[string]string{+ "pollingInterval": "0 */15 * * * *",+ }+ updateCalled := false++ getSettingFunc := func(ctx context.Context, key string, def string) string {+ if val, ok := settings[key]; ok {+ return val+ }+ return def+ }+ updateSettingFunc := func(ctx context.Context, key string, value string) error {+ updateCalled = true+ return nil+ }+ reloadFunc := func(ctx context.Context) error {+ return nil+ }++ MigrateSchedulerCronValues(ctx, getSettingFunc, updateSettingFunc, reloadFunc)++ assert.False(t, updateCalled)+ })++ t.Run("handles update errors", func(t *testing.T) {+ settings := map[string]string{+ "pollingInterval": "15",+ }++ getSettingFunc := func(ctx context.Context, key string, def string) string {+ if val, ok := settings[key]; ok {+ return val+ }+ return def+ }+ updateSettingFunc := func(ctx context.Context, key string, value string) error {+ return errors.New("update failed")+ }+ reloadFunc := func(ctx context.Context) error {+ return nil+ }++ MigrateSchedulerCronValues(ctx, getSettingFunc, updateSettingFunc, reloadFunc)+ })+}++func TestMigrateGitOpsSyncIntervals(t *testing.T) {+ ctx := context.Background()++ t.Run("migrates intervals to minutes", func(t *testing.T) {+ items := []IntervalMigrationItem{+ {ID: "sync-1", RawValue: "30"},+ {ID: "sync-2", RawValue: "1h"},+ }+ updates := make(map[string]int)++ listFunc := func(ctx context.Context) ([]IntervalMigrationItem, error) {+ return items, nil+ }+ updateFunc := func(ctx context.Context, id string, minutes int) error {+ updates[id] = minutes+ return nil+ }++ MigrateGitOpsSyncIntervals(ctx, listFunc, updateFunc)++ assert.Len(t, updates, 1)+ assert.Equal(t, 60, updates["sync-2"])+ })++ t.Run("handles list error", func(t *testing.T) {+ listFunc := func(ctx context.Context) ([]IntervalMigrationItem, error) {+ return nil, errors.New("list failed")+ }+ updateFunc := func(ctx context.Context, id string, minutes int) error {+ return nil+ }++ MigrateGitOpsSyncIntervals(ctx, listFunc, updateFunc)+ })++ t.Run("handles nil functions", func(t *testing.T) {+ MigrateGitOpsSyncIntervals(ctx, nil, nil)+ })+}++func TestParseSchedulerDuration(t *testing.T) {+ tests := []struct {+ name string+ raw string+ defaultUnit time.Duration+ wantDuration time.Duration+ wantOk bool+ }{+ {"empty string", "", time.Minute, 0, false},+ {"valid duration string", "30m", time.Minute, 30 * time.Minute, true},+ {"integer with default minute", "15", time.Minute, 15 * time.Minute, true},+ {"integer with default hour", "2", time.Hour, 2 * time.Hour, true},+ {"seconds duration", "45s", time.Minute, 45 * time.Second, true},+ {"hours duration", "2h", time.Minute, 2 * time.Hour, true},+ {"invalid format", "abc", time.Minute, 0, false},+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ gotDuration, gotOk := parseSchedulerDuration(tt.raw, tt.defaultUnit)+ assert.Equal(t, tt.wantOk, gotOk)+ if gotOk {+ assert.Equal(t, tt.wantDuration, gotDuration)+ }+ })+ }+}++func TestDurationToCron(t *testing.T) {+ tests := []struct {+ name string+ duration time.Duration+ wantCron string+ wantWarn string+ }{+ {"zero duration", 0, "", ""},+ {"negative duration", -1 * time.Minute, "", ""},+ {"30 seconds", 30 * time.Second, "*/30 * * * * *", ""},+ {"1 minute", time.Minute, "0 */1 * * * *", ""},+ {"15 minutes", 15 * time.Minute, "0 */15 * * * *", ""},+ {"1 hour", time.Hour, "0 0 */1 * * *", ""},+ {"24 hours", 24 * time.Hour, "0 0 0 */1 * *", ""},+ {"45 seconds rounds up", 45 * time.Second, "*/45 * * * * *", ""},+ {"90 seconds rounds to 2 min", 90 * time.Second, "0 */2 * * * *", "scheduler interval rounded up to minutes"},+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ gotCron, gotWarn := durationToCron(tt.duration)+ assert.Equal(t, tt.wantCron, gotCron)+ if tt.wantWarn != "" {+ assert.Contains(t, gotWarn, tt.wantWarn)+ }+ })+ }+}++func TestMinutesToCron(t *testing.T) {+ tests := []struct {+ name string+ minutes int+ wantCron string+ }{+ {"zero minutes", 0, ""},+ {"negative minutes", -1, ""},+ {"15 minutes", 15, "0 */15 * * * *"},+ {"30 minutes", 30, "0 */30 * * * *"},+ {"60 minutes", 60, "0 0 */1 * * *"},+ {"120 minutes", 120, "0 0 */2 * * *"},+ {"1440 minutes (1 day)", 1440, "0 0 0 */1 * *"},+ {"2880 minutes (2 days)", 2880, "0 0 0 */2 * *"},+ {"90 minutes", 90, "0 */90 * * * *"},+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ gotCron := minutesToCron(tt.minutes)+ assert.Equal(t, tt.wantCron, gotCron)+ })+ }+}++func TestNormalizeSchedulerValueToCron(t *testing.T) {+ tests := []struct {+ name string+ raw string+ defaultUnit time.Duration+ wantCron string+ wantShouldUpdate bool+ }{+ {"empty string", "", time.Minute, "", false},+ {"@hourly special", "@hourly", time.Minute, "@hourly", false},+ {"valid 6-field cron", "0 */15 * * * *", time.Minute, "0 */15 * * * *", false},+ {"5-field cron adds second", "*/15 * * * *", time.Minute, "0 */15 * * * *", true},+ {"integer minutes", "30", time.Minute, "0 */30 * * * *", true},+ {"duration string", "1h", time.Minute, "0 0 */1 * * *", true},+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ gotCron, gotShouldUpdate, _ := normalizeSchedulerValueToCron(tt.raw, tt.defaultUnit)+ assert.Equal(t, tt.wantCron, gotCron)+ assert.Equal(t, tt.wantShouldUpdate, gotShouldUpdate)+ })+ }+}++func TestNormalizeSchedulerValueToMinutes(t *testing.T) {+ tests := []struct {+ name string+ raw string+ defaultUnit time.Duration+ wantMinutes int+ wantShouldUpdate bool+ }{+ {"empty string", "", time.Minute, 0, false},+ {"@hourly special", "@hourly", time.Minute, 0, false},+ {"integer already minutes", "30", time.Minute, 30, false},+ {"duration string", "1h", time.Minute, 60, true},+ {"cron every hour", "0 0 * * * *", time.Minute, 60, true},+ {"cron every 15 min", "0 */15 * * * *", time.Minute, 15, true},+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ gotMinutes, gotShouldUpdate, _ := normalizeSchedulerValueToMinutes(tt.raw, tt.defaultUnit)+ assert.Equal(t, tt.wantMinutes, gotMinutes)+ assert.Equal(t, tt.wantShouldUpdate, gotShouldUpdate)+ })+ }+}++func TestCronToMinutes(t *testing.T) {+ tests := []struct {+ name string+ fields []string+ wantMinutes int+ wantOk bool+ }{+ {"every 30 seconds", []string{"*/30", "*", "*", "*", "*", "*"}, 1, true},+ {"every 15 minutes", []string{"0", "*/15", "*", "*", "*", "*"}, 15, true},+ {"every hour", []string{"0", "0", "*", "*", "*", "*"}, 60, true},+ {"every 2 hours", []string{"0", "0", "*/2", "*", "*", "*"}, 120, true},+ {"every day", []string{"0", "0", "0", "*/1", "*", "*"}, 1440, true},+ {"too few fields", []string{"0", "*/15", "*", "*", "*"}, 0, false},+ {"complex pattern", []string{"0", "15", "*/2", "*", "*", "*"}, 0, false},+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ gotMinutes, gotOk, _ := cronToMinutes(tt.fields)+ assert.Equal(t, tt.wantOk, gotOk)+ if gotOk {+ assert.Equal(t, tt.wantMinutes, gotMinutes)+ }+ })+ }+}++func TestParseCronStep(t *testing.T) {+ tests := []struct {+ name string+ field string+ wantStep int+ wantOk bool+ }{+ {"valid step", "*/15", 15, true},+ {"no prefix", "15", 0, false},+ {"zero step", "*/0", 0, false},+ {"negative step", "*/-5", 0, false},+ {"invalid number", "*/abc", 0, false},+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ gotStep, gotOk := parseCronStep(tt.field)+ assert.Equal(t, tt.wantOk, gotOk)+ if gotOk {+ assert.Equal(t, tt.wantStep, gotStep)+ }+ })+ }+}++func TestTryConvertSecondStep(t *testing.T) {+ tests := []struct {+ name string+ sec string+ min string+ hour string+ day string+ month string+ weekday string+ wantMinutes int+ wantOk bool+ }{+ {"every 30 seconds", "*/30", "*", "*", "*", "*", "*", 1, true},+ {"every 60 seconds", "*/60", "*", "*", "*", "*", "*", 1, true},+ {"every 90 seconds", "*/90", "*", "*", "*", "*", "*", 2, true},+ {"not wildcard minutes", "*/30", "0", "*", "*", "*", "*", 0, false},+ {"not step pattern", "0", "*", "*", "*", "*", "*", 0, false},+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ gotMinutes, gotOk, _ := tryConvertSecondStep(tt.sec, tt.min, tt.hour, tt.day, tt.month, tt.weekday)+ assert.Equal(t, tt.wantOk, gotOk)+ if gotOk {+ assert.Equal(t, tt.wantMinutes, gotMinutes)+ }+ })+ }+}++func TestTryConvertMinuteOrHour(t *testing.T) {+ tests := []struct {+ name string+ sec string+ min string+ hour string+ day string+ month string+ weekday string+ wantMinutes int+ wantOk bool+ }{+ {"every 15 minutes", "0", "*/15", "*", "*", "*", "*", 15, true},+ {"every hour", "0", "0", "*", "*", "*", "*", 60, true},+ {"every 2 hours", "0", "0", "*/2", "*", "*", "*", 120, true},+ {"complex pattern", "0", "30", "*/2", "*", "*", "*", 0, false},+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ gotMinutes, gotOk, _ := tryConvertMinuteOrHour(tt.sec, tt.min, tt.hour, tt.day, tt.month, tt.weekday)+ assert.Equal(t, tt.wantOk, gotOk)+ if gotOk {+ assert.Equal(t, tt.wantMinutes, gotMinutes)+ }+ })+ }+}++func TestTryConvertDayStep(t *testing.T) {+ tests := []struct {+ name string+ sec string+ min string+ hour string+ day string+ month string+ weekday string+ wantMinutes int+ wantOk bool+ }{+ {"every day", "0", "0", "0", "*/1", "*", "*", 1440, true},+ {"every 2 days", "0", "0", "0", "*/2", "*", "*", 2880, true},+ {"not midnight", "0", "0", "1", "*/1", "*", "*", 0, false},+ {"not step pattern", "0", "0", "0", "1", "*", "*", 0, false},+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ gotMinutes, gotOk, _ := tryConvertDayStep(tt.sec, tt.min, tt.hour, tt.day, tt.month, tt.weekday)+ assert.Equal(t, tt.wantOk, gotOk)+ if gotOk {+ assert.Equal(t, tt.wantMinutes, gotMinutes)+ }+ })+ }+}diff --git a/backend/pkg/scheduler/analytics_job.go b/backend/pkg/scheduler/analytics_job.gonew file mode 100644index 000000000..94a385e20--- /dev/null+++ b/backend/pkg/scheduler/analytics_job.go@@ -0,0 +1,144 @@+package scheduler++import (+ "bytes"+ "context"+ "encoding/json"+ "fmt"+ "log/slog"+ "net/http"+ "strconv"+ "strings"+ "time"++ backoff "github.com/cenkalti/backoff/v5"+ "github.com/getarcaneapp/arcane/backend/internal/config"+ "github.com/getarcaneapp/arcane/backend/internal/services"+)++const (+ AnalyticsJobName = "analytics-heartbeat"+ defaultHeartbeatEndpoint = "https://checkin.getarcane.app/heartbeat"+ defaultAnalyticsInterval = 24 * time.Hour+)++type AnalyticsJob struct {+ settingsService *services.SettingsService+ httpClient *http.Client+ heartbeatURL string+ cfg *config.Config+}++func NewAnalyticsJob(+ settingsService *services.SettingsService,+ httpClient *http.Client,+ cfg *config.Config,+) *AnalyticsJob {+ if httpClient == nil {+ httpClient = &http.Client{Timeout: 30 * time.Second}+ }+ return &AnalyticsJob{+ settingsService: settingsService,+ httpClient: httpClient,+ heartbeatURL: defaultHeartbeatEndpoint,+ cfg: cfg,+ }+}++func (j *AnalyticsJob) Name() string {+ return AnalyticsJobName+}++func (j *AnalyticsJob) Schedule(ctx context.Context) string {+ s := j.settingsService.GetStringSetting(ctx, "analyticsHeartbeatInterval", "0 0 0 * * *")+ if s == "" {+ return "0 0 0 * * *"+ }++ // Handle legacy straight int if it somehow didn't get migrated+ if i, err := strconv.Atoi(s); err == nil {+ if i <= 0 {+ i = 1440+ }+ if i%1440 == 0 {+ return fmt.Sprintf("0 0 0 */%d * *", i/1440)+ }+ if i%60 == 0 {+ return fmt.Sprintf("0 0 */%d * * *", i/60)+ }+ return fmt.Sprintf("0 */%d * * * *", i)+ }++ return s+}++func (j *AnalyticsJob) Run(ctx context.Context) {+ if j.cfg.AnalyticsDisabled || !j.cfg.Environment.IsProdEnvironment() {+ slog.DebugContext(ctx, "analytics disabled or not in production; skipping heartbeat", "analyticsDisabled", j.cfg.AnalyticsDisabled, "env", j.cfg.Environment)+ return+ }++ instanceID := j.settingsService.GetStringSetting(ctx, "instanceId", "")++ payload := struct {+ Version string `json:"version"`+ InstanceID string `json:"instance_id"`+ }{+ Version: getAnalyticsVersion(),+ InstanceID: instanceID,+ }++ body, err := json.Marshal(payload)+ if err != nil {+ slog.ErrorContext(ctx, "failed to marshal analytics heartbeat body", "error", err)+ return+ }++ slog.InfoContext(ctx, "sending analytics heartbeat", "jobName", AnalyticsJobName)++ _, err = backoff.Retry(+ ctx,+ func() (struct{}, error) {+ reqCtx, cancel := context.WithTimeout(ctx, 20*time.Second)+ defer cancel()++ req, err := http.NewRequestWithContext(reqCtx, http.MethodPost, j.heartbeatURL, bytes.NewReader(body))+ if err != nil {+ return struct{}{}, fmt.Errorf("failed to create request: %w", err)+ }+ req.Header.Set("Content-Type", "application/json")++ resp, err := j.httpClient.Do(req)+ if err != nil {+ return struct{}{}, fmt.Errorf("failed to send request: %w", err)+ }+ defer resp.Body.Close()++ if resp.StatusCode < 200 || resp.StatusCode >= 300 {+ return struct{}{}, fmt.Errorf("request failed with status: %d", resp.StatusCode)+ }+ return struct{}{}, nil+ },+ backoff.WithBackOff(backoff.NewExponentialBackOff()),+ backoff.WithMaxTries(3),+ )++ if err != nil {+ slog.ErrorContext(ctx, "analytics heartbeat failed", "error", err)+ return+ }++ slog.InfoContext(ctx, "analytics heartbeat sent successfully", "jobName", AnalyticsJobName)+}++func (j *AnalyticsJob) Reschedule(ctx context.Context) error {+ slog.InfoContext(ctx, "rescheduling analytics heartbeat job in new scheduler; currently requires restart")+ return nil+}++func getAnalyticsVersion() string {+ if v := strings.TrimSpace(config.Version); v != "" && v != "dev" {+ return v+ }+ return "unknown"+}diff --git a/backend/pkg/scheduler/auto_update_job.go b/backend/pkg/scheduler/auto_update_job.gonew file mode 100644index 000000000..da164330b--- /dev/null+++ b/backend/pkg/scheduler/auto_update_job.go@@ -0,0 +1,79 @@+package scheduler++import (+ "context"+ "fmt"+ "log/slog"+ "strconv"++ "github.com/getarcaneapp/arcane/backend/internal/services"+)++type AutoUpdateJob struct {+ updaterService *services.UpdaterService+ settingsService *services.SettingsService+}++func NewAutoUpdateJob(updaterService *services.UpdaterService, settingsService *services.SettingsService) *AutoUpdateJob {+ return &AutoUpdateJob{+ updaterService: updaterService,+ settingsService: settingsService,+ }+}++func (j *AutoUpdateJob) Name() string {+ return "auto-update"+}++func (j *AutoUpdateJob) Schedule(ctx context.Context) string {+ s := j.settingsService.GetStringSetting(ctx, "autoUpdateInterval", "0 0 0 * * *")+ if s == "" {+ return "0 0 0 * * *"+ }++ // Handle legacy straight int if it somehow didn't get migrated+ if i, err := strconv.Atoi(s); err == nil {+ if i <= 0 {+ i = 1440+ }+ if i%1440 == 0 {+ return fmt.Sprintf("0 0 0 */%d * *", i/1440)+ }+ if i%60 == 0 {+ return fmt.Sprintf("0 0 */%d * * *", i/60)+ }+ return fmt.Sprintf("0 */%d * * * *", i)+ }++ return s+}++func (j *AutoUpdateJob) Run(ctx context.Context) {+ enabled := j.settingsService.GetBoolSetting(ctx, "autoUpdate", false)+ pollingEnabled := j.settingsService.GetBoolSetting(ctx, "pollingEnabled", true)+ if !enabled || !pollingEnabled {+ slog.DebugContext(ctx, "auto-update disabled or polling disabled; skipping run",+ "autoUpdate", enabled, "pollingEnabled", pollingEnabled)+ return+ }++ slog.InfoContext(ctx, "auto-update run started")++ result, err := j.updaterService.ApplyPending(ctx, false)+ if err != nil {+ slog.ErrorContext(ctx, "auto-update run failed", "err", err)+ return+ }++ slog.InfoContext(ctx, "auto-update run completed",+ "checked", result.Checked,+ "updated", result.Updated,+ "skipped", result.Skipped,+ "failed", result.Failed,+ )+}++func (j *AutoUpdateJob) Reschedule(ctx context.Context) error {+ slog.InfoContext(ctx, "rescheduling auto-update job in new scheduler; currently requires restart")+ return nil+}diff --git a/backend/internal/job/environment_health_job.go b/backend/pkg/scheduler/environment_health_job.gosimilarity index 64%rename from backend/internal/job/environment_health_job.gorename to backend/pkg/scheduler/environment_health_job.goindex 799e54fcd..06cd0046d 100644--- a/backend/internal/job/environment_health_job.go+++ b/backend/pkg/scheduler/environment_health_job.go@@ -1,66 +1,51 @@-package job+package schedulerimport ("context"+ "fmt""log/slog"- "time"+ "strconv""github.com/getarcaneapp/arcane/backend/internal/services"- "github.com/go-co-op/gocron/v2")type EnvironmentHealthJob struct {environmentService *services.EnvironmentServicesettingsService *services.SettingsService- scheduler *Scheduler}-func NewEnvironmentHealthJob(scheduler *Scheduler, environmentService *services.EnvironmentService, settingsService *services.SettingsService) *EnvironmentHealthJob {+func NewEnvironmentHealthJob(environmentService *services.EnvironmentService, settingsService *services.SettingsService) *EnvironmentHealthJob {return &EnvironmentHealthJob{environmentService: environmentService,settingsService: settingsService,- scheduler: scheduler,}}-func (j *EnvironmentHealthJob) Register(ctx context.Context) error {- healthCheckInterval := j.settingsService.GetIntSetting(ctx, "environmentHealthInterval", 2)- interval := time.Duration(healthCheckInterval) * time.Minute-- // Ensure minimum interval of 1 minute- if interval < 1*time.Minute {- slog.WarnContext(ctx, "environment health check interval too low; using minimum", "requested_minutes", healthCheckInterval, "effective_interval", "1m")- interval = 1 * time.Minute- }-- slog.InfoContext(ctx, "registering environment health check job", "interval", interval.String())-- j.scheduler.RemoveJobByName("environment-health")-- jobDefinition := gocron.DurationJob(interval)- return j.scheduler.RegisterJob(- ctx,- "environment-health",- jobDefinition,- j.Execute,- true, // Run immediately on startup- )+func (j *EnvironmentHealthJob) Name() string {+ return "environment-health"}-func (j *EnvironmentHealthJob) Reschedule(ctx context.Context) error {- healthCheckInterval := j.settingsService.GetIntSetting(ctx, "environmentHealthInterval", 2)- interval := time.Duration(healthCheckInterval) * time.Minute-- if interval < 1*time.Minute {- interval = 1 * time.Minute+func (j *EnvironmentHealthJob) Schedule(ctx context.Context) string {+ s := j.settingsService.GetStringSetting(ctx, "environmentHealthInterval", "0 */2 * * * *")+ if s == "" {+ return "0 */2 * * * *"}- slog.InfoContext(ctx, "environment health check settings changed; rescheduling", "interval", interval.String())+ // Handle legacy straight int if it somehow didn't get migrated+ if i, err := strconv.Atoi(s); err == nil {+ if i <= 0 {+ i = 2+ }+ if i%60 == 0 {+ return fmt.Sprintf("0 0 */%d * * *", i/60)+ }+ return fmt.Sprintf("0 */%d * * * *", i)+ }- return j.scheduler.RescheduleDurationJobByName(ctx, "environment-health", interval, j.Execute, false)+ return s}-func (j *EnvironmentHealthJob) Execute(ctx context.Context) error {+func (j *EnvironmentHealthJob) Run(ctx context.Context) {slog.InfoContext(ctx, "environment health check started")// Get all environments using the DB directly@@ -81,7 +66,7 @@ func (j *EnvironmentHealthJob) Execute(ctx context.Context) error {Where("enabled = ?", true).Find(&environments).Error; err != nil {slog.ErrorContext(ctx, "failed to list environments for health check", "error", err)- return err+ return}checkedCount := 0@@ -134,6 +119,9 @@ func (j *EnvironmentHealthJob) Execute(ctx context.Context) error {}slog.InfoContext(ctx, "environment health check completed", "checked", checkedCount, "online", onlineCount, "offline", offlineCount)+}+func (j *EnvironmentHealthJob) Reschedule(ctx context.Context) error {+ slog.InfoContext(ctx, "rescheduling environment health job in new scheduler; currently requires restart")return nil}diff --git a/backend/pkg/scheduler/event_cleanup_job.go b/backend/pkg/scheduler/event_cleanup_job.gonew file mode 100644index 000000000..00a0b69d1--- /dev/null+++ b/backend/pkg/scheduler/event_cleanup_job.go@@ -0,0 +1,69 @@+package scheduler++import (+ "context"+ "fmt"+ "log/slog"+ "strconv"+ "time"++ "github.com/getarcaneapp/arcane/backend/internal/services"+)++const EventCleanupJobName = "EventCleanup"++type EventCleanupJob struct {+ eventService *services.EventService+ settingsService *services.SettingsService+}++func NewEventCleanupJob(eventService *services.EventService, settingsService *services.SettingsService) *EventCleanupJob {+ return &EventCleanupJob{+ eventService: eventService,+ settingsService: settingsService,+ }+}++func (j *EventCleanupJob) Name() string {+ return EventCleanupJobName+}++func (j *EventCleanupJob) Schedule(ctx context.Context) string {+ s := j.settingsService.GetStringSetting(ctx, "eventCleanupInterval", "0 0 */6 * * *")+ if s == "" {+ return "0 0 */6 * * *"+ }++ // Handle legacy straight int if it somehow didn't get migrated+ if i, err := strconv.Atoi(s); err == nil {+ if i <= 0 {+ i = 360+ }+ if i%60 == 0 {+ return fmt.Sprintf("0 0 */%d * * *", i/60)+ }+ return fmt.Sprintf("0 */%d * * * *", i)+ }++ return s+}++func (j *EventCleanupJob) Run(ctx context.Context) {+ slog.InfoContext(ctx, "Running event cleanup job", "jobName", EventCleanupJobName)++ // Delete events older than 36 hours+ olderThan := 36 * time.Hour+ if err := j.eventService.DeleteOldEvents(ctx, olderThan); err != nil {+ slog.ErrorContext(ctx, "Failed to delete old events", "jobName", EventCleanupJobName, "olderThan", olderThan.String(), "error", err)+ return+ }++ slog.InfoContext(ctx, "Event cleanup job completed successfully",+ "jobName", EventCleanupJobName,+ "olderThan", olderThan.String())+}++func (j *EventCleanupJob) Reschedule(ctx context.Context) error {+ slog.InfoContext(ctx, "rescheduling event cleanup job in new scheduler; currently requires restart")+ return nil+}diff --git a/backend/internal/job/filesystem_watcher_job.go b/backend/pkg/scheduler/filesystem_watcher_job.gosimilarity index 95%rename from backend/internal/job/filesystem_watcher_job.gorename to backend/pkg/scheduler/filesystem_watcher_job.goindex 283cecbd0..a6900c336 100644--- a/backend/internal/job/filesystem_watcher_job.go+++ b/backend/pkg/scheduler/filesystem_watcher_job.go@@ -1,4 +1,4 @@-package job+package schedulerimport ("context"@@ -29,7 +29,7 @@ func NewFilesystemWatcherJob(}}-func RegisterFilesystemWatcherJob(ctx context.Context, scheduler *Scheduler, projectService *services.ProjectService, templateService *services.TemplateService, settingsService *services.SettingsService) (*FilesystemWatcherJob, error) {+func RegisterFilesystemWatcherJob(ctx context.Context, projectService *services.ProjectService, templateService *services.TemplateService, settingsService *services.SettingsService) (*FilesystemWatcherJob, error) {job := NewFilesystemWatcherJob(projectService, templateService, settingsService)go func() {@@ -43,7 +43,6 @@ func RegisterFilesystemWatcherJob(ctx context.Context, scheduler *Scheduler, pro}func (j *FilesystemWatcherJob) Start(ctx context.Context) error {-settings, err := j.settingsService.GetSettings(ctx)if err != nil {return errdiff --git a/backend/pkg/scheduler/gitops_sync_job.go b/backend/pkg/scheduler/gitops_sync_job.gonew file mode 100644index 000000000..b8c242908--- /dev/null+++ b/backend/pkg/scheduler/gitops_sync_job.go@@ -0,0 +1,46 @@+package scheduler++import (+ "context"+ "log/slog"++ "github.com/getarcaneapp/arcane/backend/internal/services"+)++type GitOpsSyncJob struct {+ syncService *services.GitOpsSyncService+ settingsService *services.SettingsService+}++func NewGitOpsSyncJob(syncService *services.GitOpsSyncService, settingsService *services.SettingsService) *GitOpsSyncJob {+ return &GitOpsSyncJob{+ syncService: syncService,+ settingsService: settingsService,+ }+}++func (j *GitOpsSyncJob) Name() string {+ return "gitops-sync"+}++func (j *GitOpsSyncJob) Schedule(ctx context.Context) string {+ // Default interval: 1 minute to check for due syncs+ return "0 */1 * * * *"+}++func (j *GitOpsSyncJob) Run(ctx context.Context) {+ enabled := j.settingsService.GetBoolSetting(ctx, "gitopsSyncEnabled", true)+ if !enabled {+ slog.DebugContext(ctx, "GitOps sync disabled; skipping run")+ return+ }++ slog.InfoContext(ctx, "GitOps sync run started")++ if err := j.syncService.SyncAllEnabled(ctx); err != nil {+ slog.ErrorContext(ctx, "GitOps sync run failed", "err", err)+ return+ }++ slog.InfoContext(ctx, "GitOps sync run completed")+}diff --git a/backend/pkg/scheduler/image_polling_job.go b/backend/pkg/scheduler/image_polling_job.gonew file mode 100644index 000000000..6e0f0285f--- /dev/null+++ b/backend/pkg/scheduler/image_polling_job.go@@ -0,0 +1,93 @@+package scheduler++import (+ "context"+ "fmt"+ "log/slog"+ "strconv"++ "github.com/getarcaneapp/arcane/backend/internal/services"+)++type ImagePollingJob struct {+ imageUpdateService *services.ImageUpdateService+ settingsService *services.SettingsService+ environmentService *services.EnvironmentService+}++func NewImagePollingJob(imageUpdateService *services.ImageUpdateService, settingsService *services.SettingsService, environmentService *services.EnvironmentService) *ImagePollingJob {+ return &ImagePollingJob{+ imageUpdateService: imageUpdateService,+ settingsService: settingsService,+ environmentService: environmentService,+ }+}++func (j *ImagePollingJob) Name() string {+ return "image-polling"+}++func (j *ImagePollingJob) Schedule(ctx context.Context) string {+ s := j.settingsService.GetStringSetting(ctx, "pollingInterval", "0 0 * * * *")+ if s == "" {+ return "0 0 * * * *"+ }++ // Handle legacy straight int if it somehow didn't get migrated+ if i, err := strconv.Atoi(s); err == nil {+ if i <= 0 {+ i = 60+ }+ if i%60 == 0 {+ return fmt.Sprintf("0 0 */%d * * *", i/60)+ }+ return fmt.Sprintf("0 */%d * * * *", i)+ }++ return s+}++func (j *ImagePollingJob) Run(ctx context.Context) {+ pollingEnabled := j.settingsService.GetBoolSetting(ctx, "pollingEnabled", true)+ if !pollingEnabled {+ slog.DebugContext(ctx, "polling disabled; skipping image scan")+ return+ }++ slog.InfoContext(ctx, "image scan run started")++ creds, err := j.environmentService.GetEnabledRegistryCredentials(ctx)+ if err != nil {+ slog.WarnContext(ctx, "failed to load registry credentials for polling", "error", err.Error())+ creds = nil+ }++ results, err := j.imageUpdateService.CheckAllImages(ctx, 0, creds)+ if err != nil {+ slog.ErrorContext(ctx, "image scan failed", "err", err)+ return+ }++ total := len(results)+ updates := 0+ errors := 0+ for _, r := range results {+ if r == nil {+ continue+ }+ if r.Error != "" {+ errors+++ continue+ }+ if r.HasUpdate {+ updates+++ }+ }++ slog.InfoContext(ctx, "image scan run completed", "checked", total, "updates", updates, "errors", errors)+}++func (j *ImagePollingJob) Reschedule(ctx context.Context) error {+ slog.InfoContext(ctx, "rescheduling image polling job in new scheduler; currently requires restart")+ return nil+}diff --git a/backend/internal/job/scheduled_prune_job.go b/backend/pkg/scheduler/scheduled_prune_job.gosimilarity index 52%rename from backend/internal/job/scheduled_prune_job.gorename to backend/pkg/scheduler/scheduled_prune_job.goindex 8900476ad..18c223168 100644--- a/backend/internal/job/scheduled_prune_job.go+++ b/backend/pkg/scheduler/scheduled_prune_job.go@@ -1,13 +1,13 @@-package job+package schedulerimport ("context"+ "fmt""log/slog"- "time"+ "strconv""github.com/getarcaneapp/arcane/backend/internal/services""github.com/getarcaneapp/arcane/types/system"- "github.com/go-co-op/gocron/v2")const ScheduledPruneJobName = "scheduled-prune"@@ -15,50 +15,47 @@ const ScheduledPruneJobName = "scheduled-prune"type ScheduledPruneJob struct {systemService *services.SystemServicesettingsService *services.SettingsService- scheduler *Scheduler}-func NewScheduledPruneJob(scheduler *Scheduler, systemService *services.SystemService, settingsService *services.SettingsService) *ScheduledPruneJob {+func NewScheduledPruneJob(systemService *services.SystemService, settingsService *services.SettingsService) *ScheduledPruneJob {return &ScheduledPruneJob{systemService: systemService,settingsService: settingsService,- scheduler: scheduler,}}-func (j *ScheduledPruneJob) Register(ctx context.Context) error {- enabled := j.settingsService.GetBoolSetting(ctx, "scheduledPruneEnabled", false)- intervalMinutes := j.settingsService.GetIntSetting(ctx, "scheduledPruneInterval", 1440)+func (j *ScheduledPruneJob) Name() string {+ return ScheduledPruneJobName+}- if !enabled {- j.scheduler.RemoveJobByName(ScheduledPruneJobName)- slog.InfoContext(ctx, "scheduled prune disabled; job not registered", "enabled", enabled)- return nil+func (j *ScheduledPruneJob) Schedule(ctx context.Context) string {+ s := j.settingsService.GetStringSetting(ctx, "scheduledPruneInterval", "0 0 0 * * *")+ if s == "" {+ return "0 0 0 * * *"}- interval := time.Duration(intervalMinutes) * time.Minute- if interval < 60*time.Minute {- slog.WarnContext(ctx, "scheduled prune interval too low; using minimum", "requested_minutes", intervalMinutes, "effective_interval", "60m")- interval = 60 * time.Minute+ // Handle legacy straight int if it somehow didn't get migrated+ if i, err := strconv.Atoi(s); err == nil {+ if i <= 0 {+ i = 1440+ }+ if i%1440 == 0 {+ return fmt.Sprintf("0 0 0 */%d * *", i/1440)+ }+ if i%60 == 0 {+ return fmt.Sprintf("0 0 */%d * * *", i/60)+ }+ return fmt.Sprintf("0 */%d * * * *", i)}- j.scheduler.RemoveJobByName(ScheduledPruneJobName)-- jobDefinition := gocron.DurationJob(interval)- return j.scheduler.RegisterJob(- ctx,- ScheduledPruneJobName,- jobDefinition,- j.Execute,- false,- )+ return s}-func (j *ScheduledPruneJob) Execute(ctx context.Context) error {+func (j *ScheduledPruneJob) Run(ctx context.Context) {enabled := j.settingsService.GetBoolSetting(ctx, "scheduledPruneEnabled", false)if !enabled {- slog.InfoContext(ctx, "scheduled prune disabled; skipping run")- return nil+ slog.DebugContext(ctx, "scheduled prune disabled; skipping run")+ return}pruneMode := j.settingsService.GetStringSetting(ctx, "dockerPruneMode", "dangling")@@ -75,7 +72,7 @@ func (j *ScheduledPruneJob) Execute(ctx context.Context) error {if !req.Containers && !req.Images && !req.Volumes && !req.Networks && !req.BuildCache {slog.InfoContext(ctx, "scheduled prune run skipped; no resource types selected")- return nil+ return}slog.InfoContext(ctx, "scheduled prune run started",@@ -90,7 +87,7 @@ func (j *ScheduledPruneJob) Execute(ctx context.Context) error {result, err := j.systemService.PruneAll(ctx, req)if err != nil {slog.ErrorContext(ctx, "scheduled prune run failed", "error", err)- return err+ return}slog.InfoContext(ctx, "scheduled prune run completed",@@ -102,26 +99,9 @@ func (j *ScheduledPruneJob) Execute(ctx context.Context) error {"networks_deleted", len(result.NetworksDeleted),"errors", len(result.Errors),)-- return nil}func (j *ScheduledPruneJob) Reschedule(ctx context.Context) error {- enabled := j.settingsService.GetBoolSetting(ctx, "scheduledPruneEnabled", false)- intervalMinutes := j.settingsService.GetIntSetting(ctx, "scheduledPruneInterval", 1440)-- if !enabled {- j.scheduler.RemoveJobByName(ScheduledPruneJobName)- slog.InfoContext(ctx, "scheduled prune disabled; removed job if present")- return nil- }-- interval := time.Duration(intervalMinutes) * time.Minute- if interval < 60*time.Minute {- interval = 60 * time.Minute- }-- slog.InfoContext(ctx, "scheduled prune settings changed; rescheduling", "interval", interval.String())-- return j.scheduler.RescheduleDurationJobByName(ctx, ScheduledPruneJobName, interval, j.Execute, false)+ slog.InfoContext(ctx, "rescheduling scheduled prune job in new scheduler; currently requires restart")+ return nil}diff --git a/backend/pkg/scheduler/scheduler.go b/backend/pkg/scheduler/scheduler.gonew file mode 100644index 000000000..5969ee2e0--- /dev/null+++ b/backend/pkg/scheduler/scheduler.go@@ -0,0 +1,52 @@+package scheduler++import (+ "context"+ "log/slog"++ schedulertypes "github.com/getarcaneapp/arcane/types/scheduler"+ "github.com/robfig/cron/v3"+)++type JobScheduler struct {+ cron *cron.Cron+ jobs []schedulertypes.Job+ context context.Context+}++func NewJobScheduler(ctx context.Context) *JobScheduler {+ return &JobScheduler{+ cron: cron.New(cron.WithSeconds()),+ jobs: []schedulertypes.Job{},+ context: ctx,+ }+}++func (js *JobScheduler) RegisterJob(job schedulertypes.Job) {+ js.jobs = append(js.jobs, job)+}++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)++ if _, err := js.cron.AddFunc(schedule, func() {+ slog.InfoContext(js.context, "Job starting", "name", currentJob.Name())+ currentJob.Run(js.context)+ slog.InfoContext(js.context, "Job finished", "name", currentJob.Name())+ }); err != nil {+ slog.ErrorContext(js.context, "Failed to schedule job", "name", currentJob.Name(), "error", err)+ }+ }+ js.cron.Start()+}++func (js *JobScheduler) Run(ctx context.Context) error {+ js.StartScheduler()+ <-ctx.Done()+ js.cron.Stop()+ return nil+}diff --git a/cli/pkg/jobschedules/cmd.go b/cli/pkg/jobschedules/cmd.goindex b5148d056..5f6cad96b 100644--- a/cli/pkg/jobschedules/cmd.go+++ b/cli/pkg/jobschedules/cmd.go@@ -52,17 +52,17 @@ var getCmd = &cobra.Command{}output.Header("Job Schedules")- output.KeyValue("Environment health interval (min)", cfg.EnvironmentHealthInterval)- output.KeyValue("Event cleanup interval (min)", cfg.EventCleanupInterval)- output.KeyValue("Analytics heartbeat interval (min)", cfg.AnalyticsHeartbeatInterval)+ output.KeyValue("Environment health interval", cfg.EnvironmentHealthInterval)+ output.KeyValue("Event cleanup interval", cfg.EventCleanupInterval)+ output.KeyValue("Analytics heartbeat interval", cfg.AnalyticsHeartbeatInterval)return nil},}var (- environmentHealthInterval int- eventCleanupInterval int- analyticsHeartbeatInterval int+ environmentHealthInterval string+ eventCleanupInterval string+ analyticsHeartbeatInterval string)var updateCmd = &cobra.Command{@@ -111,9 +111,9 @@ var updateCmd = &cobra.Command{}output.Success("Job schedules updated")- output.KeyValue("Environment health interval (min)", result.Data.EnvironmentHealthInterval)- output.KeyValue("Event cleanup interval (min)", result.Data.EventCleanupInterval)- output.KeyValue("Analytics heartbeat interval (min)", result.Data.AnalyticsHeartbeatInterval)+ output.KeyValue("Environment health interval", result.Data.EnvironmentHealthInterval)+ output.KeyValue("Event cleanup interval", result.Data.EventCleanupInterval)+ output.KeyValue("Analytics heartbeat interval", result.Data.AnalyticsHeartbeatInterval)return nil},}@@ -125,7 +125,7 @@ func init() {getCmd.Flags().BoolVar(&jsonOutput, "json", false, "Output in JSON format")updateCmd.Flags().BoolVar(&jsonOutput, "json", false, "Output in JSON format")- updateCmd.Flags().IntVar(&environmentHealthInterval, "environment-health-interval", 0, "Environment health job interval in minutes (1-60)")- updateCmd.Flags().IntVar(&eventCleanupInterval, "event-cleanup-interval", 0, "Event cleanup job interval in minutes (5-10080)")- updateCmd.Flags().IntVar(&analyticsHeartbeatInterval, "analytics-heartbeat-interval", 0, "Analytics heartbeat job interval in minutes (60-43200)")+ updateCmd.Flags().StringVar(&environmentHealthInterval, "environment-health-interval", "", "Environment health job interval (cron expression)")+ updateCmd.Flags().StringVar(&eventCleanupInterval, "event-cleanup-interval", "", "Event cleanup job interval (cron expression)")+ updateCmd.Flags().StringVar(&analyticsHeartbeatInterval, "analytics-heartbeat-interval", "", "Analytics heartbeat job interval (cron expression)")}diff --git a/frontend/src/lib/types/job-schedule.type.ts b/frontend/src/lib/types/job-schedule.type.tsindex 7619168d7..d115ec053 100644--- a/frontend/src/lib/types/job-schedule.type.ts+++ b/frontend/src/lib/types/job-schedule.type.ts@@ -1,7 +1,7 @@export type JobSchedules = {- environmentHealthInterval: number;- eventCleanupInterval: number;- analyticsHeartbeatInterval: number;+ environmentHealthInterval: string;+ eventCleanupInterval: string;+ analyticsHeartbeatInterval: string;};export type JobSchedulesUpdate = Partial<JobSchedules>;diff --git a/frontend/src/routes/(app)/settings/jobs/+page.svelte b/frontend/src/routes/(app)/settings/jobs/+page.svelteindex b0bb4b76e..9f690b821 100644--- a/frontend/src/routes/(app)/settings/jobs/+page.svelte+++ b/frontend/src/routes/(app)/settings/jobs/+page.svelte@@ -23,9 +23,9 @@let savedSchedules = $state<JobSchedules>(untrack(() => data.jobSchedules));const formSchema = z.object({- environmentHealthInterval: z.coerce.number().int().min(1).max(60),- eventCleanupInterval: z.coerce.number().int().min(5).max(10080),- analyticsHeartbeatInterval: z.coerce.number().int().min(60).max(43200)+ environmentHealthInterval: z.string().min(1),+ eventCleanupInterval: z.string().min(1),+ analyticsHeartbeatInterval: z.string().min(1)});const form = createForm(@@ -128,9 +128,9 @@bind:value={$formInputs.environmentHealthInterval.value}error={$formInputs.environmentHealthInterval.error}label={m.environments_health_check_interval_label()}- placeholder="2"+ placeholder="0 */2 * * * *"helpText={m.environments_health_check_interval_description()}- type="number"+ type="text"/></div></div>@@ -153,9 +153,9 @@bind:value={$formInputs.eventCleanupInterval.value}error={$formInputs.eventCleanupInterval.error}label={m.jobs_event_cleanup_interval_label()}- placeholder="360"+ placeholder="0 0 */6 * * *"helpText={m.jobs_event_cleanup_interval_help()}- type="number"+ type="text"/></div></div>@@ -178,9 +178,9 @@bind:value={$formInputs.analyticsHeartbeatInterval.value}error={$formInputs.analyticsHeartbeatInterval.error}label={m.jobs_analytics_interval_label()}- placeholder="1440"+ placeholder="0 0 0 * * *"helpText={m.jobs_analytics_interval_help()}- type="number"+ type="text"/></div></div>diff --git a/types/jobschedule/jobschedule.go b/types/jobschedule/jobschedule.goindex 0451f9c34..25f5f3e74 100644--- a/types/jobschedule/jobschedule.go+++ b/types/jobschedule/jobschedule.go@@ -5,16 +5,16 @@ package jobschedule// All fields are in minutes.// This makes conversion to time.Duration straightforward in the backend.type Config struct {- EnvironmentHealthInterval int `json:"environmentHealthInterval"`- EventCleanupInterval int `json:"eventCleanupInterval"`- AnalyticsHeartbeatInterval int `json:"analyticsHeartbeatInterval"`+ EnvironmentHealthInterval string `json:"environmentHealthInterval"`+ EventCleanupInterval string `json:"eventCleanupInterval"`+ AnalyticsHeartbeatInterval string `json:"analyticsHeartbeatInterval"`}// Update is used to update job schedule intervals (in minutes).//// Any nil field is ignored.type Update struct {- EnvironmentHealthInterval *int `json:"environmentHealthInterval,omitempty"`- EventCleanupInterval *int `json:"eventCleanupInterval,omitempty"`- AnalyticsHeartbeatInterval *int `json:"analyticsHeartbeatInterval,omitempty"`+ EnvironmentHealthInterval *string `json:"environmentHealthInterval,omitempty"`+ EventCleanupInterval *string `json:"eventCleanupInterval,omitempty"`+ AnalyticsHeartbeatInterval *string `json:"analyticsHeartbeatInterval,omitempty"`}diff --git a/types/scheduler/job.go b/types/scheduler/job.gonew file mode 100644index 000000000..485b1f5e7--- /dev/null+++ b/types/scheduler/job.go@@ -0,0 +1,9 @@+package scheduler++import "context"++type Job interface {+ Name() string+ Schedule(ctx context.Context) string+ Run(ctx context.Context)+}