-
Notifications
You must be signed in to change notification settings - Fork 1
Worker - actionsToRun[] - determine actions needing run #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,53 @@ | ||||||||||||||||||||||||||||||||||||||||||
| import { getScheduledActions } from "../lib/supabase/scheduled_actions/getScheduledActions"; | ||||||||||||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||||||||||||
| getScheduledActions, | ||||||||||||||||||||||||||||||||||||||||||
| ScheduledAction, | ||||||||||||||||||||||||||||||||||||||||||
| } from "../lib/supabase/scheduled_actions/getScheduledActions"; | ||||||||||||||||||||||||||||||||||||||||||
| import parser from "cron-parser"; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| function shouldRunAction(action: ScheduledAction, now: Date): boolean { | ||||||||||||||||||||||||||||||||||||||||||
| if (!action.enabled) return false; | ||||||||||||||||||||||||||||||||||||||||||
| if (!action.schedule) return false; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // If never run, and next_run is now or earlier, run it | ||||||||||||||||||||||||||||||||||||||||||
| if (!action.last_run) { | ||||||||||||||||||||||||||||||||||||||||||
| if (!action.next_run) return true; // If next_run and last_run are null, run it | ||||||||||||||||||||||||||||||||||||||||||
| return new Date(action.next_run) <= now; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // If next_run is now or earlier, and last_run is before next_run, run it | ||||||||||||||||||||||||||||||||||||||||||
| if (action.next_run && new Date(action.next_run) <= now) { | ||||||||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||||||||
| !action.last_run || | ||||||||||||||||||||||||||||||||||||||||||
| new Date(action.last_run) < new Date(action.next_run) | ||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // Fallback: use cron-parser to check if a run is due | ||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||
| const interval = parser.parseExpression(action.schedule, { | ||||||||||||||||||||||||||||||||||||||||||
| currentDate: now, | ||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||
| const prev = interval.prev().toDate(); | ||||||||||||||||||||||||||||||||||||||||||
| // If last_run is before the previous scheduled time, it's due | ||||||||||||||||||||||||||||||||||||||||||
| if (!action.last_run || new Date(action.last_run) < prev) { | ||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+27
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Either use -const prev = interval.prev().toDate();
-if (!action.last_run || new Date(action.last_run) < prev) {
+const next = interval.next().toDate();
+if (!action.last_run || new Date(action.last_run) < next) {
return true;
}This guarantees an execution whenever 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||
| } catch (e) { | ||||||||||||||||||||||||||||||||||||||||||
| console.error( | ||||||||||||||||||||||||||||||||||||||||||
| `[CronController] Invalid cron schedule for action ${action.id}:`, | ||||||||||||||||||||||||||||||||||||||||||
| action.schedule | ||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| export async function handleScheduledActions(): Promise<void> { | ||||||||||||||||||||||||||||||||||||||||||
| const scheduledActions = await getScheduledActions(); | ||||||||||||||||||||||||||||||||||||||||||
| console.log("[CronController] Scheduled Actions:", scheduledActions); | ||||||||||||||||||||||||||||||||||||||||||
| const now = new Date(); | ||||||||||||||||||||||||||||||||||||||||||
| const actionsToRun = scheduledActions.filter((action) => | ||||||||||||||||||||||||||||||||||||||||||
| shouldRunAction(action, now) | ||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||
| console.log("[CronController] actionsToRun:", actionsToRun); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Repeated
Dateconstruction is wasteful & can cause subtle TZ mis-parsing.new Date(action.next_run)is invoked multiple times and assumes the DB string includes a timezone.If Supabase returns
"2025-06-21 12:00:00"(no TZ), Node treats it as local time, whereascron-parserdefaults to system TZ, leading to silent drift on servers not in UTC.Cache the parsed dates once per action (
const nextRun = …; const lastRun = …) to avoid repeated parsing and make the TZ handling explicit.📝 Committable suggestion
🤖 Prompt for AI Agents