Review request

Food bank stock tracker on Replit, the nightly recount job never fires

Open · 13 views · asked by amara_v · edited

Repo or live app

github.com/amara-okoye/foodbank-stock

Unsure about: Deployment

Volunteers log crates in and out during the day. At 02:00 a job should roll the day up into stock_daily so the morning report is ready. Replit wrote it with node-cron inside the same process as the web server. Some mornings the report is there, most mornings it is not, and I cannot see a pattern.

Comment

2 answers

jonas_k · edited

There is no pattern because it depends on whether your process happened to be awake at 02:00. node-cron is a timer inside a running process. On a deployment that sleeps when idle, and your app is idle at two in the morning, the timer simply does not exist at that moment.

Move the job out of the web process. Two options that both work on Replit:

  • A Scheduled Deployment, which is a separate run on a cron expression. Point it at a scripts/rollup.js that does the work and exits.
  • Supabase pg_cron if the rollup is pure SQL, which from your code it almost is.

Either way, make the job idempotent: insert ... on conflict (day) do update. Then a double run on a retry is harmless.

Comment
ines_data · edited

Adding to that: your rollup reads crate_events with where created_at::date = current_date - 1, which is your local date, not the database's. If the scheduler runs in UTC and you are in a different offset, some mornings you roll up a window that is one hour short.

Store a day column computed once with an explicit zone:

(created_at at time zone 'Europe/Amsterdam')::date

and group on that. It also lets you index the column, which the cast in your where clause currently prevents.

Comment