Queues and jobs: KScript.Queue
Job options
Options go in the brackets after the job's name. Times are durations, written with no quotes: 500ms, 30s, 10m, 2h, 1d. A plain number is seconds.
Queues and connections
queue is the queue the job goes on (default unless you say), and connection is the connection in kscript.json it goes to (queue.default unless you say). See Connections and drivers.
Retries and backoff
A job gets 3 tries unless you give tries. backoff is how long to wait before each retry: one time (30s), a list where the last one repeats ([10s, 1m, 5m]), or 'exponential', the default, which waits 2s, 4s, 8s and so on up to 10m. retry_until stops retrying a while after the job was dispatched, even with tries left:
<import package="KScript.Queue" />
<job name="FetchRates" tries="10" backoff="[10s, 1m, 5m]" retry_until="1h">
<field name="currency" type="string" />
<fn name="handle">
<echo>Fetching {.currency} rates</echo>
</fn>
</job>import KScript.Queue
job FetchRates(tries: 10, backoff: [10s, 1m, 5m], retry_until: 1h) {
currency: string
fn handle() {
echo "Fetching {.currency} rates"
}
}Timeouts
timeout is the longest one attempt may run, 60 seconds unless you say. Past it, the attempt stops with a JobTimeout error and counts as a try:
<import package="KScript.Queue" />
<job name="BuildReport" timeout="5m">
<field name="month" type="string" />
<fn name="handle">
<echo>Building the report for {.month}</echo>
</fn>
</job>import KScript.Queue
job BuildReport(timeout: 5m) {
month: string
fn handle() {
echo "Building the report for {.month}"
}
}Which errors are retried
By default, every error is retried. retry_on retries only the error types you list, and fails the job at once on any other. fail_on never retries the types you list. Error types match their children too, so retry_on: [Network] also retries an error declared as error ApiDown extends Network:
<import package="KScript.Queue" />
<job name="PostWebhook" retry_on="[Network, Timeout]" fail_on="[Validation]">
<field name="url" type="url" />
<fn name="handle">
<echo>Posting to {.url}</echo>
</fn>
</job>import KScript.Queue
job PostWebhook(retry_on: [Network, Timeout], fail_on: [Validation]) {
url: url
fn handle() {
echo "Posting to {.url}"
}
}Unique jobs
unique_for queues a job only once while one with the same fields is waiting or running, for up to that long. unique_by compares only the fields you list:
<import package="KScript.Queue" />
<job name="RebuildSearchIndex" unique_for="10m" unique_by="user_id">
<field name="user_id" type="int" />
<fn name="handle">
<echo>Rebuilding the index for user {.user_id}</echo>
</fn>
</job>import KScript.Queue
job RebuildSearchIndex(unique_for: 10m, unique_by: [user_id]) {
user_id: int
fn handle() {
echo "Rebuilding the index for user {.user_id}"
}
}