Scheduling tasks
Introduction
Some work runs at set times rather than when someone asks for it: pruning old sessions every hour, emailing a report at 03:00, checking a feed every five minutes. Instead of a line in the system’s crontab for each one, a KScript project lists its tasks in one file, schedule.kscript, next to kscript.json:
import KScript.Queue
import "jobs/prune-sessions.kscript"
import "jobs/send-digest.kscript"
fn check_feeds() {
log "Checking the feeds"
}
Schedule {
Task(every: 1h, job: PruneSessions())
Task(cron: '0 3 * * *', job: SendDigest(kind: 'daily'), without_overlapping)
Task(every: 5m, run: @check_feeds, name: 'feeds')
}
Then one long-running command runs each task at its time:
kscript schedule work # runs the due tasks at the start of every minute, until Ctrl+C
kscript make schedule prune-sessions --every 1h writes the file with a first task for you. The schedule is part of KScript.Queue, so the file starts with import KScript.Queue.
Defining tasks
Each Task says when it runs, with cron or every, and what it does, with job or run.
| Argument | What it does |
|---|---|
cron |
When it runs, as a cron schedule: '0 3 * * *' (03:00 each day), '*/15 9-17 * * mon-fri', '@hourly', '@daily'. |
every |
How often it runs instead, in whole minutes: 1m, 5m, 1h, 1d. |
job |
A job to dispatch each time, such as PruneSessions(). It goes on the job’s queue, and kscript worker runs it. |
run |
A function the scheduler calls each time, with no arguments: @check_feeds, or @reports.nightly for a function of an imported script. |
name |
The task’s name in kscript schedule list and the scheduler’s output. Defaults to the job’s or the function’s name. Two tasks can’t share a name. |
without_overlapping |
Skip the task while its last run is still going. |
queue, connection |
The queue and connection its job goes to, instead of the job’s own. |
description |
What the task is for, shown by kscript schedule list. |
every counts from midnight, so every: 15m runs at :00, :15, :30 and :45, and every: 1d runs at midnight. Times are the machine’s local time, as in a crontab.
Jobs or functions
A task with job only dispatches: the work happens in kscript worker, with the job’s retries, backoff and timeout. Prefer it for anything that may fail or take a while. The job is built when the task is due, so its fields can depend on the time:
import KScript.Queue
import "jobs/send-digest.kscript"
Schedule {
Task(cron: '0 8 * * mon', job: SendDigest(kind: 'weekly'), queue: 'mail')
}
A task with run calls its function in the scheduler itself. It suits short work: an error is reported, and the task runs again at its next time.
Preventing overlaps
With without_overlapping, a task whose last run hasn’t finished is skipped, and the scheduler prints SKIP. For a job, the last run lasts until a worker has finished the job it dispatched. The lock is the queue’s unique job lock, so with a file or database connection it holds across processes and machines. A lock is let go after 24 hours at the most, so a crashed run can’t stop a task for good.
Running the scheduler
kscript schedule work # every minute, until Ctrl+C
kscript schedule run # the tasks due this minute, then stop
kscript schedule run --task feeds # one task now, to try it
kscript schedule list # each task, when it runs and its next run
kscript serve --with-worker --with-scheduler # both inside the server, while you work on a site
In production, run kscript schedule work as a service next to kscript worker, or add one line to the system’s crontab that runs the due tasks each minute:
* * * * * cd /srv/shop && kscript schedule run >> /var/log/shop-schedule.log 2>&1
Each minute’s run loads the schedule file afresh, so a change to it takes effect the next minute without a restart. A task that takes longer than a minute doesn’t hold up the next minute’s tasks. --file names another schedule file; without it the scheduler looks for schedule.kscript, then routes/schedule.kscript.
Testing tasks
kscript schedule run --task name runs one task straight away, whatever its time. To check what a job task would dispatch, call its job’s ->run() in a test, or dispatch it with @fake_queue() in place.