Scheduling: jobs that wake a model¶
A scheduled job that runs a shell script and a scheduled job that wakes a language model
look identical in a crontab and behave nothing alike. The second one costs money per run,
produces different output every time, can decide to send a message, and fails in ways that
exit 1 never captures.
Why the scheduler moved into the application¶
The first version used system cron. It broke on all three of these:
No access to application state. A cron entry cannot ask "is the owner mid-conversation right now?" — so the nightly summary arrives in the middle of a live exchange.
Nowhere to record a run. Cron's idea of history is a mail spool. What you actually want is a row: when it fired, what it produced, whether it messaged anyone, what it cost.
No per-job identity. With several agents on one machine, every job needs to know which assistant it belongs to, with which memory and which permissions. Encoding that in the crontab line is a recipe for a job running as the wrong personality.
The fix was a scheduled_tasks table and an in-process ticker. Roughly forty jobs run on
this install; the ticker checks each minute, computes what is due in the owner's timezone,
and dispatches.
Silent by default¶
The distinction that matters most, and the one I got wrong first: jobs that report vs jobs that are silent.
- Silent: backups, index maintenance, memory consolidation, health checks. They run, write to the database, and say nothing unless something is wrong.
- Reporting: the morning digest, a deadline reminder, a weekly review.
I shipped the first batch as reporting jobs because a message is proof it ran. Within two days the assistant was a notification spammer and the human had learned to ignore it, which destroys the value of the one message that mattered. An "everything is fine" report is an anti-pattern: silence is the success signal, and a health ledger is where you look when you want proof.
Timezone, once, at the edge¶
Store schedules in the owner's timezone, convert at dispatch, and never store a local time without its zone. Every scheduling bug I have had traces back to a naive datetime crossing a boundary — DST is the loud version, a VPS in UTC serving an owner at UTC+8 is the quiet one.
Failure modes specific to model-backed jobs¶
The silent burn. A job wakes the model, the model does nothing useful, the job reports nothing because it is silent, and it repeats hourly for a week. Track cost per job, not just cost per day.
The clone. Two processes with the same schedule table — usually one left over from a migration — produce doubled messages and doubled charges. A single-instance lock on the process, and a check that the scheduled job's owner matches the running instance.
The stuck run. A job that hangs holds its slot forever. Every model-backed job needs a hard wall-clock timeout that kills it and records a failure, or one bad run quietly stops everything behind it.
Markers that only work on a live turn. Some conveniences — inline task markers, interactive prompts — depend on there being a human at the other end. Called from a scheduler they either vanish or leak raw syntax into a message. Scheduled jobs need the non-interactive path, always.
What the table looks like¶
Minimum viable columns, all learned by lacking them:
| Column | Why |
|---|---|
schedule + timezone |
the two are meaningless apart |
silent |
decides whether output reaches the human |
agent_id |
which assistant, which memory, which permissions |
last_run_at, last_status, last_error |
otherwise "did it run?" is unanswerable |
enabled |
pause without deleting; deleting loses the history |
timeout_sec |
the stuck-run guard |
Notes on the rest of the architecture live at avelina.ai.