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:
Cian Johnston
2022-04-18 11:04:48 -05:00
committed by GitHub
parent 3311c2f65d
commit af672803a2
11 changed files with 171 additions and 151 deletions
@@ -2,7 +2,7 @@ import Box from "@material-ui/core/Box"
import Typography from "@material-ui/core/Typography"
import cronstrue from "cronstrue"
import React from "react"
import { expandScheduleCronString, extractTimezone } from "../../util/schedule"
import { extractTimezone, stripTimezone } from "../../util/schedule"
import { WorkspaceSection } from "./WorkspaceSection"
const Language = {
@@ -26,7 +26,7 @@ const Language = {
},
cronHumanDisplay: (schedule: string): string => {
if (schedule) {
return cronstrue.toString(expandScheduleCronString(schedule), { throwExceptionOnParseError: false })
return cronstrue.toString(stripTimezone(schedule), { throwExceptionOnParseError: false })
}
return "Manual"
},
+2 -2
View File
@@ -68,7 +68,7 @@ export const MockWorkspaceAutostartDisabled: WorkspaceAutostartRequest = {
export const MockWorkspaceAutostartEnabled: WorkspaceAutostartRequest = {
// Runs at 9:30am Monday through Friday using Canada/Eastern
// (America/Toronto) time
schedule: "CRON_TZ=Canada/Eastern 30 9 1-5",
schedule: "CRON_TZ=Canada/Eastern 30 9 * * 1-5",
}
export const MockWorkspaceAutostopDisabled: WorkspaceAutostartRequest = {
@@ -77,7 +77,7 @@ export const MockWorkspaceAutostopDisabled: WorkspaceAutostartRequest = {
export const MockWorkspaceAutostopEnabled: WorkspaceAutostartRequest = {
// Runs at 9:30pm Monday through Friday using America/Toronto
schedule: "CRON_TZ=America/Toronto 30 21 1-5",
schedule: "CRON_TZ=America/Toronto 30 21 * * 1-5",
}
export const MockWorkspace: Workspace = {
+1 -11
View File
@@ -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)
})
})
})
-22
View File
@@ -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(" ")
}