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 package="KScript.Queue" />
<import path="jobs/prune-sessions.kscript" />
<import path="jobs/send-digest.kscript" />
<fn name="check_feeds">
<log>Checking the feeds</log>
</fn>
<Schedule>
<Task every="1h" job="PruneSessions()" />
<Task cron="0 3 * * *" job="SendDigest(kind: 'daily')" without_overlapping="yes" />
<Task every="5m" run="check_feeds" name="feeds" />
</Schedule>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:
| Command | What it does |
|---|---|
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.