Queues and jobs: KScript.Queue
Controlling a running job
Inside handle, @release_job(after: 30s) puts the job back on its queue to run again later, without using up a try. Use it for work that isn't ready yet:
<import package="KScript.Queue" />
<job name="WaitForPayment" tries="1">
<field name="order_id" type="int" />
<field name="paid" type="bool" />
<fn name="handle">
<if not .paid>
<do>@release_job(after: 1m)</do>
</if>
<echo>Order {.order_id} is paid</echo>
</fn>
</job>import KScript.Queue
job WaitForPayment(tries: 1) {
order_id: int
paid: bool
fn handle() {
if not .paid => @release_job(after: 1m)
echo "Order {.order_id} is paid"
}
}@fail_job() fails the job now, with no more tries, for work that will never succeed. Give it an error or a message:
<import package="KScript.Queue" />
<job name="ImportFile" tries="5">
<field name="path" type="string" />
<fn name="handle">
<if not .path->ends_with('.csv')>
<do>@fail_job(Validation(message: 'Only CSV files can be imported'))</do>
</if>
<echo>Importing {.path}</echo>
</fn>
</job>import KScript.Queue
job ImportFile(tries: 5) {
path: string
fn handle() {
if not .path->ends_with('.csv') {
@fail_job(Validation(message: 'Only CSV files can be imported'))
}
echo "Importing {.path}"
}
}Both work only inside a job's handle. Anywhere else they raise a Context error. An exit in a handle ends that job only: exit or exit 0 finishes it, and any other code fails the attempt like an error, so the worker carries on with the next job.