mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
autostart/autostop: move to traditional 5-valued cron string for compatibility (#1049)
This PR modfies the original 3-valued cron strings used in package schedule to be traditional 5-valued cron strings. - schedule.Weekly will validate that the month and dom fields are equal to * - cli autostart/autostop will attempt to detect local timezone using TZ env var, defaulting to UTC - cli autostart/autostop no longer accepts a raw schedule -- instead use the --minute, --hour, --dow, and --tz arguments. - Default schedules are provided that should suffice for most users. Fixes #993
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { expandScheduleCronString, extractTimezone, stripTimezone } from "./schedule"
|
||||
import { extractTimezone, stripTimezone } from "./schedule"
|
||||
|
||||
describe("util/schedule", () => {
|
||||
describe("stripTimezone", () => {
|
||||
@@ -20,14 +20,4 @@ describe("util/schedule", () => {
|
||||
expect(extractTimezone(input)).toBe(expected)
|
||||
})
|
||||
})
|
||||
|
||||
describe("expandScheduleCronString", () => {
|
||||
it.each<[string, string]>([
|
||||
["CRON_TZ=Canada/Eastern 30 9 1-5", "30 9 * * 1-5"],
|
||||
["CRON_TZ=America/Central 0 8 1,2,4,5", "0 8 * * 1,2,4,5"],
|
||||
["30 9 1-5", "30 9 * * 1-5"],
|
||||
])(`expandScheduleCronString(%p) returns %p`, (input, expected) => {
|
||||
expect(expandScheduleCronString(input)).toBe(expected)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -30,25 +30,3 @@ export const extractTimezone = (raw: string): string => {
|
||||
return DEFAULT_TIMEZONE
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* expandScheduleCronString ensures a Schedule is expanded to a valid 5-value
|
||||
* cron string by inserting '*' in month and day positions. If there is a
|
||||
* leading timezone, it is removed.
|
||||
*
|
||||
* @example
|
||||
* expandScheduleCronString("30 9 1-5") // -> "30 9 * * 1-5"
|
||||
*/
|
||||
export const expandScheduleCronString = (schedule: string): string => {
|
||||
const prepared = stripTimezone(schedule).trim()
|
||||
|
||||
const parts = prepared.split(" ")
|
||||
|
||||
while (parts.length < 5) {
|
||||
// insert '*' in the second to last position
|
||||
// ie [a, b, c] --> [a, b, *, c]
|
||||
parts.splice(parts.length - 1, 0, "*")
|
||||
}
|
||||
|
||||
return parts.join(" ")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user