Skip to content

Modern Docker Management, Designed for Everyone

fix: convert cron expressions from utc into TZ var timezone (#1781)

Kyle Mendellcaefd110156b
diff --git a/backend/internal/bootstrap/bootstrap.go b/backend/internal/bootstrap/bootstrap.go
index 7b7172ecf..ebeb03b10 100644
--- a/backend/internal/bootstrap/bootstrap.go
+++ b/backend/internal/bootstrap/bootstrap.go
@@ -133,7 +133,8 @@ func Bootstrap(ctx context.Context) error {
}
}
- scheduler := scheduler.NewJobScheduler(appCtx)
+ cronLocation := cfg.GetLocation()
+ scheduler := scheduler.NewJobScheduler(appCtx, cronLocation)
appServices.JobSchedule.SetScheduler(scheduler)
registerJobs(appCtx, scheduler, appServices, cfg)
diff --git a/backend/internal/config/config.go b/backend/internal/config/config.go
index cc6d9150c..5d26714fe 100644
--- a/backend/internal/config/config.go
+++ b/backend/internal/config/config.go
@@ -8,6 +8,7 @@ import (
"reflect"
"strconv"
"strings"
+ "time"
"github.com/getarcaneapp/arcane/backend/internal/common"
)
@@ -73,6 +74,10 @@ type Config struct {
ProxyRequestTimeout int `env:"PROXY_REQUEST_TIMEOUT" default:"0"`
BackupVolumeName string `env:"ARCANE_BACKUP_VOLUME_NAME" default:"arcane-backups"`
+ // Timezone for cron job scheduling. Uses IANA timezone names (e.g., "America/New_York", "Europe/London").
+ // "Local" uses the system's local timezone, "UTC" for Coordinated Universal Time.
+ Timezone string `env:"TZ" default:"Local"`
+
// BuildablesConfig contains feature-specific configuration that can be conditionally compiled
BuildablesConfig
}
@@ -307,6 +312,24 @@ func (c *Config) ListenAddr() string {
return net.JoinHostPort(host, port)
}
+// GetLocation returns the timezone location for cron scheduling.
+// It parses the Timezone config (TZ env var) into a *time.Location.
+// Returns the system's local timezone if Timezone is "Local".
+// Defaults to UTC if not set or if the timezone cannot be loaded.
+func (c *Config) GetLocation() *time.Location {
+ tz := strings.TrimSpace(c.Timezone)
+ if tz == "" {
+ return time.UTC
+ }
+
+ loc, err := time.LoadLocation(tz)
+ if err != nil {
+ slog.Warn("Failed to load timezone, falling back to UTC", "timezone", tz, "error", err)
+ return time.UTC
+ }
+ return loc
+}
+
// GetManagerBaseURL returns the base URL of the manager application.
// It strips any trailing slashes or /api suffix from MANAGER_API_URL.
func (c *Config) GetManagerBaseURL() string {
diff --git a/backend/internal/services/job_service.go b/backend/internal/services/job_service.go
index 32d098502..ccd5ded3f 100644
--- a/backend/internal/services/job_service.go
+++ b/backend/internal/services/job_service.go
@@ -33,12 +33,18 @@ type JobService struct {
settings *SettingsService
cfg *config.Config
scheduler JobRunner
+ location *time.Location // Timezone for cron schedule calculations
OnJobSchedulesChanged func(ctx context.Context, changedKeys []string)
}
func NewJobService(db *database.DB, settings *SettingsService, cfg *config.Config) *JobService {
- return &JobService{db: db, settings: settings, cfg: cfg}
+ return &JobService{
+ db: db,
+ settings: settings,
+ cfg: cfg,
+ location: cfg.GetLocation(),
+ }
}
func (s *JobService) SetScheduler(scheduler JobRunner) {
@@ -266,12 +272,24 @@ func (s *JobService) calculateNextRunInternal(schedule string) *time.Time {
return nil
}
+ // Parse schedule and force it to use the same timezone as the scheduler.
parser := cron.NewParser(cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow)
sched, err := parser.Parse(schedule)
if err != nil {
return nil
}
- nextRun := sched.Next(time.Now())
+ location := time.UTC
+ if s != nil && s.location != nil {
+ location = s.location
+ }
+
+ if specSchedule, ok := sched.(*cron.SpecSchedule); ok {
+ specSchedule.Location = location
+ }
+
+ // Calculate next run using the configured timezone.
+ now := time.Now().In(location)
+ nextRun := sched.Next(now)
return &nextRun
}
diff --git a/backend/pkg/scheduler/scheduler.go b/backend/pkg/scheduler/scheduler.go
index 194a5e8c3..685e43ef8 100644
--- a/backend/pkg/scheduler/scheduler.go
+++ b/backend/pkg/scheduler/scheduler.go
@@ -3,6 +3,7 @@ package scheduler
import (
"context"
"log/slog"
+ "time"
schedulertypes "github.com/getarcaneapp/arcane/types/scheduler"
"github.com/robfig/cron/v3"
@@ -14,15 +15,24 @@ type JobScheduler struct {
jobsByID map[string]schedulertypes.Job
entryIDs map[string]cron.EntryID
context context.Context
+ location *time.Location
}
-func NewJobScheduler(ctx context.Context) *JobScheduler {
+// NewJobScheduler creates a new job scheduler with the specified timezone location.
+// The location is used for interpreting cron expressions.
+// If location is nil, UTC is used.
+func NewJobScheduler(ctx context.Context, location *time.Location) *JobScheduler {
+ if location == nil {
+ location = time.UTC
+ }
+ slog.InfoContext(ctx, "Initializing job scheduler", "timezone", location.String())
return &JobScheduler{
- cron: cron.New(cron.WithSeconds()),
+ cron: cron.New(cron.WithSeconds(), cron.WithLocation(location)),
jobs: []schedulertypes.Job{},
jobsByID: make(map[string]schedulertypes.Job),
entryIDs: make(map[string]cron.EntryID),
context: ctx,
+ location: location,
}
}
@@ -78,6 +88,11 @@ func (js *JobScheduler) RescheduleJob(ctx context.Context, job schedulertypes.Jo
return nil
}
+// GetLocation returns the timezone location used by the scheduler for cron expressions.
+func (js *JobScheduler) GetLocation() *time.Location {
+ return js.location
+}
+
func (js *JobScheduler) Run(ctx context.Context) error {
js.StartScheduler()
<-ctx.Done()
diff --git a/docker/examples/compose.basic.yaml b/docker/examples/compose.basic.yaml
index eac030257..1f0fe6782 100644
--- a/docker/examples/compose.basic.yaml
+++ b/docker/examples/compose.basic.yaml
@@ -11,6 +11,9 @@ services:
environment:
- ENCRYPTION_KEY=xxxxxxxxxxxxxxxxxxxxxx
- JWT_SECRET=xxxxxxxxxxxxxxxxxxxxxxxxxx
+ # Timezone for cron job scheduling (e.g., America/New_York, Europe/London, UTC)
+ # See: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
+ - TZ=UTC
# Use host cgroup namespace for better container self-detection
# This allows Arcane to detect its own container ID more reliably
cgroup: host