KScript docs
EN

Queues and jobs: KScript.Queue

Dispatching jobs

->dispatch() queues a job with its own options, and returns what was queued: its id, job, queue, connection and when it's due (available_at). It returns null for a unique job that is already queued:

<import package="KScript.Queue" />
<import path="jobs/send-welcome.kscript" />

<let name="queued" value="SendWelcome(user_id: 7, email: 'joan@example.com')->dispatch()" />
<echo>Queued {$queued?.id}</echo>
import KScript.Queue
import "jobs/send-welcome.kscript"

$queued = SendWelcome(user_id: 7, email: 'joan@example.com')->dispatch()
echo "Queued {$queued?.id}"

A job's fields are given by name, and both the editor and the worker check them against the job's declaration.

Delaying a job

Give ->dispatch() a delay (a duration) or an at (a date) to run the job later, and a queue or connection to send it somewhere other than its own. Give delay or at, not both:

<import package="KScript.Queue" />
<import path="jobs/send-welcome.kscript" />

<do>SendWelcome(user_id: 8, email: 'ann@example.com')->dispatch(delay: 10m, queue: 'low')</do>
import KScript.Queue
import "jobs/send-welcome.kscript"

SendWelcome(user_id: 8, email: 'ann@example.com')->dispatch(delay: 10m, queue: 'low')

Dispatching inside a transaction

A job dispatched inside a transaction waits for it to commit. If the transaction rolls back, the job is dropped, so a worker never picks up a job for an order that was never saved:

<import package="KScript.Data" />
<import package="KScript.Queue" />
<import path="jobs/orders.kscript" />

<model name="Order">
  <field name="total" type="number" />
</model>

<fn name="place_order" params="total: number">
  <let name="order" value="Order->create(total: $total)" />
  <do>SendReceipt(order_id: $order.id)->dispatch()</do> <!-- queued once the order is saved -->
</fn>

<do>$db->transaction(fn() => @place_order(25))</do>
import KScript.Data
import KScript.Queue
import "jobs/orders.kscript"

model Order {
  total: number
}

fn place_order(total: number) {
  $order = Order->create(total: $total)
  SendReceipt(order_id: $order.id)->dispatch()    // queued once the order is saved
}

$db->transaction(fn() => @place_order(25))

A transaction inside another waits for the outermost one, and a job dispatched in an inner transaction that rolls back is dropped with it. Write ->dispatch(not after_commit) to queue the job at once anyway. The transaction a test runs in doesn't count, because it never commits.

Running a job now

->run() runs handle here and now, with no queue and no retries, and its errors reach the caller:

<import package="KScript.Queue" />
<import path="jobs/send-welcome.kscript" />

<do>SendWelcome(user_id: 9, email: 'bo@example.com')->run()</do>
import KScript.Queue
import "jobs/send-welcome.kscript"

SendWelcome(user_id: 9, email: 'bo@example.com')->run()

On the sync connection, ->dispatch() also runs the job straight away and returns null, because nothing was queued. kscript test uses sync, so code that reads $queued.id should allow for null, or use @fake_queue().

Chains

@chain([...]) runs jobs one after another. Each job is queued when the one before it succeeds, and a job that fails for good stops the chain:

<import package="KScript.Queue" />
<import path="jobs/orders.kscript" />

<do>@chain([ChargeCard(order_id: 42), ShipOrder(order_id: 42), SendReceipt(order_id: 42)])->dispatch()</do>
import KScript.Queue
import "jobs/orders.kscript"

@chain([ChargeCard(order_id: 42), ShipOrder(order_id: 42), SendReceipt(order_id: 42)])->dispatch()

Batches

@batch([...]) dispatches jobs together and keeps count as they finish, which suits work split into many small jobs, such as importing the rows of a file. Name a job to dispatch when the batch ends: ->then() when every job succeeded, ->catch() at the first failure, and ->finally() once every job has run either way:

<import package="KScript.Queue" />
<import path="jobs/import.kscript" />

<let name="batch">@batch($rows->map(fn(row) => ImportRow(id: $row.id)))
    ->then(ImportFinished(file: $path))
    ->catch(ImportFailed(file: $path))
    ->dispatch()</let>

<echo>Batch {$batch.id}: {$batch.total} rows</echo>
import KScript.Queue
import "jobs/import.kscript"

$batch = @batch($rows->map(fn(row) => ImportRow(id: $row.id)))
  ->then(ImportFinished(file: $path))
  ->catch(ImportFailed(file: $path))
  ->dispatch()

echo "Batch {$batch.id}: {$batch.total} rows"

The queue keeps each batch, so you can ask how far it has got from anywhere, such as a page that shows a progress bar. @queue()->batch(id) gives the batch as it is now, with .total, .pending, .processed, .failed_count, .failed_ids, .progress (0 to 100), .is_finished and .finished_at. Inside a batch's jobs, and its then, catch and finally jobs, $job.batch_id is its id:

<import package="KScript.Queue" />

<let name="batch" value="@queue()->batch($id)" />
<echo>{$batch.progress}% done, {$batch.failed_count} failed</echo>
import KScript.Queue

$batch = @queue()->batch($id)
echo "{$batch.progress}% done, {$batch.failed_count} failed"

A job that fails for good counts once, after its tries, and doesn't stop the others. On the sync connection the jobs run as the batch is dispatched; there, without a ->catch() job, the first error reaches the caller once every job has run. @fake_queue() keeps a batch's jobs, with their batch_id, but doesn't run its then, catch or finally jobs.