NextAutostart takes the workspace and template schedule and returns the next autostart schedule after "at". The boolean returned is if the autostart should be allowed to start based on the template schedule.
(at time.Time, wsSchedule string, templateSchedule TemplateScheduleOptions)
| 14 | // after "at". The boolean returned is if the autostart should be allowed to start based on the template |
| 15 | // schedule. |
| 16 | func NextAutostart(at time.Time, wsSchedule string, templateSchedule TemplateScheduleOptions) (time.Time, bool) { |
| 17 | sched, err := cron.Weekly(wsSchedule) |
| 18 | if err != nil { |
| 19 | return time.Time{}, false |
| 20 | } |
| 21 | |
| 22 | // Round down to the nearest minute, as this is the finest granularity cron supports. |
| 23 | // Truncate is probably not necessary here, but doing it anyway to be sure. |
| 24 | nextTransition := sched.Next(at).Truncate(time.Minute) |
| 25 | |
| 26 | // The nextTransition is when the auto start should kick off. If it lands on a |
| 27 | // forbidden day, do not allow the auto start. We use the time location of the |
| 28 | // schedule to determine the weekday. So if "Saturday" is disallowed, the |
| 29 | // definition of "Saturday" depends on the location of the schedule. |
| 30 | zonedTransition := nextTransition.In(sched.Location()) |
| 31 | allowed := templateSchedule.AutostartRequirement.DaysMap()[zonedTransition.Weekday()] |
| 32 | |
| 33 | return zonedTransition, allowed |
| 34 | } |
| 35 | |
| 36 | // NextAllowedAutostart returns the next valid autostart time after 'at', based on the workspace's |
| 37 | // cron schedule and the template's allowed days. It searches up to 7 days ahead to find a match. |