Skip to content

Commit e805dae

Browse files
tanviet12claude
andcommitted
fix: fix cron timezone — jobs run in tenant local time not UTC
- scheduler.go: prefix cron expression with TZ=<tenant_timezone> so each job runs at the correct local time regardless of system TZ - docker-compose.yml, docker-compose.hub.yml: add TZ=Asia/Ho_Chi_Minh as system default for container timezone and log timestamps Fixes jobs scheduled at 07:00 running at 14:00 (UTC+7 offset) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent d73a7f3 commit e805dae

3 files changed

Lines changed: 16 additions & 1 deletion

File tree

backend/engine/scheduler.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package engine
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"log"
78
"time"
89

@@ -145,15 +146,27 @@ func (s *Scheduler) syncAllChannelsTask() {
145146
}
146147
}
147148

149+
// tenantTimezone returns the timezone configured for a tenant, defaulting to Asia/Ho_Chi_Minh.
150+
func tenantTimezone(tenantID string) string {
151+
var setting models.AppSetting
152+
db.DB.Where("tenant_id = ? AND setting_key = 'timezone'", tenantID).First(&setting)
153+
if setting.ValuePlain != "" {
154+
return setting.ValuePlain
155+
}
156+
return "Asia/Ho_Chi_Minh"
157+
}
158+
148159
// loadCronJobs loads active jobs with cron schedules and registers them.
149160
func (s *Scheduler) loadCronJobs() {
150161
var jobs []models.Job
151162
db.DB.Where("is_active = true AND schedule_type = 'cron' AND schedule_cron != ''").Find(&jobs)
152163

153164
for _, job := range jobs {
154165
j := job // capture
166+
tz := tenantTimezone(j.TenantID)
167+
cronExpr := fmt.Sprintf("TZ=%s %s", tz, j.ScheduleCron)
155168
_, err := s.scheduler.NewJob(
156-
gocron.CronJob(j.ScheduleCron, false),
169+
gocron.CronJob(cronExpr, false),
157170
gocron.NewTask(func() {
158171
log.Printf("[scheduler] running analysis job %s (%s)", j.Name, j.ID)
159172
analyzer := NewAnalyzer(s.cfg)

docker-compose.hub.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ services:
3939
- APP_ENV=production
4040
- DB_HOST=db
4141
- DB_PORT=3306
42+
- TZ=Asia/Ho_Chi_Minh
4243
depends_on:
4344
db:
4445
condition: service_healthy

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ services:
3131
- APP_ENV=production
3232
- DB_HOST=db
3333
- DB_PORT=3306
34+
- TZ=Asia/Ho_Chi_Minh
3435
depends_on:
3536
db:
3637
condition: service_healthy

0 commit comments

Comments
 (0)