{"success":true,"name":"treat-scheduled-jobs-as-production-citizens-overlap-missed-r","description":"Adding a cron or scheduled task (expiry, reminders, cleanup) to a live app","category":"ai-brimind","path":"ai-brimind/treat-scheduled-jobs-as-production-citizens-overlap-missed-r/SKILL.md","file":"SKILL.md","content":"---\nname: treat-scheduled-jobs-as-production-citizens-overlap-missed-r\ndescription: \"Adding a cron or scheduled task (expiry, reminders, cleanup) to a live app\"\n---\n\n# Treat Scheduled Jobs as Production Citizens: Overlap, Missed Runs, and Silence Are the Real Design\n\n## When to use\nThe app needs (or already has) work that runs on a schedule — expiring memberships, sending reminders, nightly cleanup, syncing data — and the plan is just 'a cron calls this function', with nothing decided about overlaps, crashes mid-run, missed windows, or how you would even know it ran.\n\n## Example request\nThe nightly job takes longer than expected and the next run starts while the first is still going → two workers process the same records twice; jobs need an overlap policy (a lock or a 'still running' flag), because schedules guarantee starts, not spacing. The server was down at 3 AM and the expiry job simply never ran that day → memberships stayed active a day too long and nobody noticed; a job that matters needs catch-up logic (process everything OVERDUE, not just today's) so a missed window heals itself. A job has crashed silently every night for two weeks → no log, no alert, no trace; every run must leave a record — started, finished, how many items, or failed with what — or the job effectively does not exist. The job loops one DB write per record over thousands of rows at peak hours → your batching and aggregation disciplines apply INSIDE jobs too; scheduled code is still code.\n\n## Instructions\nAct as an engineer who knows that scheduled jobs are the app's night shift — working unsupervised, with nobody watching the screen — so every safeguard a request gets from a waiting user (retries, error displays, someone noticing) must be designed in explicitly, because at 3 AM nobody notices anything. For any scheduled task, do this: (1) Write the job's contract in plain words first: what it processes, how often, and — the key sentence — what happens if it runs twice on the same data, and what happens if it does not run at all for a day. Those two sentences ARE the design; 'calls the function nightly' is just the trigger. (2) Make the work idempotent and self-healing rather than schedule-trusting: select work by STATE, not by time-of-run — 'find all memberships where expiry has passed AND status is still active' processes today's, yesterday's missed, and anything a crash left behind, in one honest query; a job that only handles 'today's batch' quietly loses whatever a downtime swallowed. Mark items done as they are processed so a re-run skips them — your read-modify-write and idempotency disciplines apply verbatim here. (3) Decide the overlap policy on purpose: either the state-based selection plus per-item marking makes overlap harmless (best), or take a simple lock (a flag with a timestamp that also expires, so a crashed run cannot hold the lock forever) — pick one and say why. (4) Give the night shift a logbook: every run logs start, end, items processed, and failures per your logging discipline, and a job whose silence matters (payments, expiries) deserves the inverse alert — you want to know when it did NOT run, which monitoring services and simple heartbeat checks exist for; check what your platform offers (managed cron, queue schedulers) before hand-rolling the scheduler itself. (5) Schedule with the timezone discipline attached — 'runs at midnight' in UTC or in the club's local day? — and place heavy jobs off peak hours consciously. Only ship the job once you can answer: 'ran twice — harmless? skipped a day — self-heals? failed last night — how do I know by breakfast?' — a scheduled task without those three answers is not automation, it is a slow-motion incident with a calendar.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #231\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}