go-co-op / gocron

Easy and fluent Go cron scheduling. This is a fork from https://github.com/jasonlvhit/gocron
MIT License
5.61k stars 308 forks source link

[BUG] - Unable to Use timezone in GoCron #725

Closed niting3c closed 6 months ago

niting3c commented 6 months ago

Describe the bug

I am using a corn expression with a timezone to schedule jobs , but it fails in parsing the location. I have verified that Europe/Isle_of_Man Exists in the TZ information zip attached here: //go:generate env ZONEINFO=$GOROOT/lib/time/zoneinfo.zip go run genzabbrs.go -output zoneinfo_abbrs_windows.go Wierd thing is , it only happens inside the docker container , same inputs etc when provided to a scratch fgolang file it parses exactly the same thing.

Is this a machine specific bug?

{"level":"info","msg":"Scheduling job with cron 30 11 * * * and timezone Europe/Isle_of_Man","time":"2024-05-02T10:19:56Z"}
{"level":"error","msg":"Failed to create new job in scheduler: gocron: CronJob: crontab parse failure\nprovided bad location Europe/Isle_of_Man: unknown time zone Europe/Isle_of_Man","time":"2024-05-02T10:19:56Z"}

To Reproduce

use this code snippet:

type JobRequestDto struct {
Cron             string
TimeZone         string   
}

func scheduleJob(input *dto.JobRequestDto){

cronScheduler,_:=gocron.NewScheduler()
secondsEnabled:=false
if input.TimeZone == "" {
        input.TimeZone = "UTC"
    }
        logrus.Infof("Scheduling job with cron %v and timezone %v", input.Cron, input.TimeZone)
    job, err := cronScheduler.NewJob(
        gocron.CronJob(
            fmt.Sprintf("TZ=%s %s", strings.TrimSpace(input.TimeZone), input.Cron), secondsEnabled),
            gocron.NewTask(func() {
            fmt.Println("Do something")
        }))
    if err != nil {
        logrus.Errorf("Failed to create new job in scheduler: %v", err)
        return
    }
    logrus.Infof("JobID created: %v", job.ID())
}

Steps to reproduce the behavior:

  1. Set time zone : Europe/Isle_of_Man
  2. Send a cron expression to function Please note this SNIPPET WILL WORK WITH VARIOUS TZ , but fails in a docker container somehow

Version

github.com/go-co-op/gocron/v2 v2.2.9

Expected behavior

Job should be scheduled with the provided input and TZ parsing should be consistent

Additional context

niting3c commented 6 months ago

For anyone seeing this issue or similar on your docker: I am using alpine: Add this line: RUN apk add --no-cache tzdata This would add TZ data to your image and this issue can be resolved.

lparam commented 1 month ago

I found same problem on windows 10.