diff --git a/controller/pkg/job_runner.go b/controller/pkg/job_runner.go index f6926c3..35c175a 100644 --- a/controller/pkg/job_runner.go +++ b/controller/pkg/job_runner.go @@ -18,6 +18,7 @@ func JobScheduler(server *socketio.Server, cronTime string, jobFunc func()) { if err != nil { fmt.Printf("There was an error getting location: %v", err) server.BroadcastToRoom("/", "logs", "logs", fmt.Sprintf("There was an error getting location: %v", err)) + loc = time.Local } crontJob := cron.New(cron.WithLocation(loc)) // cron.WithSeconds() @@ -25,7 +26,21 @@ func JobScheduler(server *socketio.Server, cronTime string, jobFunc func()) { // reference for the cron time format https://pkg.go.dev/github.com/robfig/cron@v1.2.0#hdr-CRON_Expression_Format // we can use 6 feilds but we need to add this in with cron.WithSeconds into our cron job other wise we can use use the standard 5 fields https://crontab.guru/ - crontJob.AddFunc(cronTime, jobFunc) + _, err = crontJob.AddFunc(cronTime, func() { + now := time.Now().In(loc) + msg := fmt.Sprintf("⏰ Cron job triggered at %s (spec: %s)", now.Format(time.RFC3339), cronTime) + fmt.Println(msg) + server.BroadcastToRoom("/", "logs", "logs", msg) + + jobFunc() + }) + if err != nil { + fmt.Printf("Failed to add cron job (%s): %v\n", cronTime, err) + server.BroadcastToRoom("/", "logs", "logs", fmt.Sprintf("Failed to add cron job (%s): %v\n", cronTime, err)) + return + } crontJob.Start() + fmt.Printf("✅ Cron job scheduled: %s (TZ: %s)\n", cronTime, loc.String()) + server.BroadcastToRoom("/", "logs", "logs", fmt.Sprintf("✅ Cron job scheduled: %s (TZ: %s)\n", cronTime, loc.String())) }