2

I am trying to build a service that executes on a cron schedule. I am trying to use gocron (https://github.com/jasonlvhit/gocron). My code looks like below:

func taskWithParams(a int, b string) {
    fmt.Println(a, b)
}

func GetSchedules() {
    s := gocron.NewScheduler()

    //Retrieve the schedules from Dyanmo
    var scheduleArray = dynamodb.GetConfiguration()

    //iterate over all of the schedules
    for _, i := range scheduleArray {

        //Determine the days we need to schedule on
        dayWeekStart, err := strconv.Atoi(i.Cron["day_week_start"])
        dayWeekEnd, err := strconv.Atoi(i.Cron["day_week_end"])
        if err != nil {
            fmt.Println("error getting days:", err)
        }

        //Determine the time to execute on
        var timeToRun string
        buffer := bytes.NewBufferString("")
        buffer.WriteString(i.Cron["hour"])
        buffer.WriteString(":")
        buffer.WriteString(i.Cron["min"])
        timeToRun = buffer.String()

        for x := dayWeekStart; x <= dayWeekEnd; x++ {
            switch {
            case x == 0:
                s.Every(1).Sunday().At(timeToRun).Do(taskWithParams, 0, timeToRun)
            case x == 1:
                s.Every(1).Monday().At(timeToRun).Do(taskWithParams, 1, timeToRun)
            case x == 2:
                s.Every(1).Tuesday().At(timeToRun).Do(taskWithParams, 2, timeToRun)
            case x == 3:
                s.Every(1).Wednesday().At(timeToRun).Do(taskWithParams, 3, timeToRun)
            case x == 4:
                s.Every(1).Thursday().At(timeToRun).Do(taskWithParams, 4, timeToRun)
            case x == 5:
                s.Every(1).Friday().At(timeToRun).Do(taskWithParams, 5, timeToRun)
            case x == 6:
                s.Every(1).Saturday().At(timeToRun).Do(taskWithParams, 6, timeToRun)
            default:
                fmt.Println("Improper cron day of week")
            }
        }
    }
    <-s.Start()
}

Now, say I have a cron schedule to execute at noon Mon-Fri. When noon on Monday comes, the output looks like this:

  • 1 12:00
  • 2 12:00
  • 3 12:00
  • 4 12:00
  • 5 12:00

Why is it executing for Tuesday-Friday on Monday? The examples seem straight-forward. I feel like I am doing something foolish but just can't see it.

user3464189
  • 183
  • 1
  • 1
  • 11

0 Answers0