1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// # Returns when your scheduled val should run
// This is a hacky way to get cron-like scheduled vals
// until we build them properly into Val Town.
// Returns `true` when your function should run
// Example: https://val.town/v/stevekrouse.sendDailyJoke
// Test: https://val.town/v/stevekrouse.exampleCronDay
export async function cron({
expression,
lastRunAt,
timezone,
_debugNow,
}: {
expression: string;
lastRunAt: Date;
timezone?: string;
_debugNow?: Date;
}) {
const { default: parser } = await import("npm:cron-parser");
const interval = parser.parseExpression(expression, {
tz: timezone,
currentDate: _debugNow,
});
// If the last time it was supposed to run is *after*
// the last time it ran, return `true` (meaning: run it now!)
return interval.prev().toDate() >= lastRunAt;
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023