Live stats without a scheduler
My site records daily Roblox stat history with no cron, no worker, and no queue. Visitors' page loads do the scheduling; a composite primary key does the coordination.
My site shows live player counts for the Pogi Games titles: how many people are in the games right now, and lifetime plays. I wanted history too, per game, per day, so I could chart plays over time. That usually means one thing: a scheduled job. Write a cron line, point it at an endpoint, snapshot at midnight.
remielshirazi.com runs on shared hosting. There is no crontab I control on that box, no worker process, no job queue. I do have a VPS that already runs cron lines for other services, and I still didn't want to use it, because every scheduler you add is a second system that can silently stop. The site would keep rendering while the history quietly flatlined. I have been burned by exactly that shape of failure before: the dashboard looks alive, and the collector died three weeks ago.
So the site schedules itself.
Traffic is a clock
The realization is that I never needed midnight. I need at most one row per game per day, and I already have a stream of events that happens every day: page loads. The project page renders live stats on a five minute revalidation cycle, and a client widget polls the stats API once a minute. Any one of those reads can be the day's snapshot.
The whole collector is about forty lines (src/core/integrations/snapshots.ts). After each successful stats fetch it computes today's date in UTC, compares it to the last day this process recorded, and if they differ, fires an insert and moves on. Fire and forget: a history write must never slow down or break a public page.
const day = new Date(stats.sampledAt).toISOString().slice(0, 10);
if (lastRecordedDay === day) return;
lastRecordedDay = day; // claim first, so concurrent reads don't stack insertsIdempotence does the coordination
Two requests can race. The process can restart and forget it already wrote. The same code runs in dev against a different database. None of that matters if the write itself refuses to duplicate:
CREATE TABLE roblox_snapshots (
day VARCHAR(10) NOT NULL,
universe_id BIGINT NOT NULL,
playing INT NOT NULL,
visits BIGINT NOT NULL,
PRIMARY KEY (day, universe_id)
)The composite primary key makes "one row per game per day" a property of the schema instead of a promise the code has to keep. The insert is an upsert whose update clause is a no-op, so the first write of the day wins and every later attempt does nothing. Races, restarts, and retries all collapse into a constraint the database enforces for free.
There is one honest weakness: a day with zero visitors records nothing. I can live with that, for two reasons. The practical one is that crawlers alone make a truly empty day unlikely. The real one is that a missing row is an accurate record of a day nobody looked, while a dead cron produces something worse: silence that looks like data loss. When collection is coupled to traffic, the failure mode is visible and proportional.
I think schedulers get reached for by default when the actual requirement is much weaker than it sounds. "Daily" rarely means 00:00:00. It usually means roughly once a day is plenty, and that weaker requirement can often ride on events the system already produces. The strongest version of a system is frequently the one with fewer moving parts, not the one with better monitoring on the parts you added.
The same bias shows up across the rest of the stats stack. The Roblox client (src/core/integrations/roblox.ts) caches live counts for 60 seconds, icons for six hours, and universe id lookups for a day, and it serves the last good snapshot when the upstream API fails. It follows one subtle rule there: a partial result is never saved as the fallback. A transient blip that drops one game would otherwise poison the last good copy, and a later real failure would serve the shrunken list as if it were the truth.
None of this needed new infrastructure. The history table started filling the day it shipped, one row per game per day, on a host that cannot run a single cron line.