Other languages are translated automatically from English.
KScript.Queue
Queued jobs: job declarations, dispatch, retries with backoff, failed jobs, the worker that runs them, and the schedule of tasks that run at set times.
Import it at the top of a script:
<import package="KScript.Queue" />
import KScript.Queue
KScript.Queue queues jobs for kscript worker, so its examples can't run inside the docs. Copy one into a .ks.xml file and run it with kscript.
Declares an event: something that happened in the app, with the fields its listeners need, typed like a <type>'s. Keep events in the project's events folder. @dispatch_event(OrderPlaced(order_id: $order.id)) tells every listener: each listener for OrderPlaced { } function, and each job declared with listens_to: OrderPlaced, which is queued.Declares an event: something that happened in the app, with the fields its listeners need, typed like a type's. Keep events in the project's events folder. @dispatch_event(OrderPlaced(order_id: $order.id)) tells every listener: each listener for OrderPlaced { } function, and each job declared with listens_to: OrderPlaced, which is queued.
Declares an app event (KScript.Queue). Write event OrderPlaced { order_id: int }, then @dispatch_event(OrderPlaced(order_id: 7)).
Holds statements.Holds statements in { }. Scripts only.
Attribute
Value
Description
namerequired
a new variable's name
The event's name, starting with a capital letter and saying what happened: OrderPlaced, UserSignedUp.
fields
fields
The fields on one line, like fn params: order_id: int, total: number. Bigger events list them one per line.
Declares a job: work to do later, on a queue, by kscript worker. Its fields are the data it needs, typed like a <type>'s; fn handle() does the work and sees the fields as variables. SendWelcome(user_id: 7)->dispatch() queues one. An error in handle is retried after its backoff until its tries run out; then fn failed(err) runs and the job is kept with the failed jobs (kscript queue failed). fn middleware() gives what runs around each attempt, such as [@without_overlapping()].Declares a job: work to do later, on a queue, by kscript worker. Its fields are the data it needs, typed like a type's; fn handle() does the work and sees the fields as variables. SendWelcome(user_id: 7)->dispatch() queues one. An error in handle is retried after its backoff until its tries run out; then fn failed(err) runs and the job is kept with the failed jobs (kscript queue failed). fn middleware() gives what runs around each attempt, such as [@without_overlapping()].
Declares a background job. Put its fields and a fn handle() { ... } method inside job SendMessage { ... }.
Holds statements.Holds statements in { }.
Attribute
Value
Description
namerequired
a new variable's name
The job's name, starting with a capital letter: SendWelcome, BuildReport.
queue
a string
The queue it goes on, unless the dispatch names one. Defaults to default.
connection
a string
The connection in kscript.json's queue.connections it goes to, unless the dispatch names one. Defaults to queue.default.
tries
a string
Attempts before it counts as failed. Defaults to 3.
backoff
expression
How long to wait before each retry: one time (30s), a list ([10s, 1m, 5m], the last one repeats), or 'exponential' (2s, 4s, 8s, up to 10m; the default).
timeout
expression
The longest one attempt may run, such as 30s or 5m. Past it the attempt stops with a JobTimeout error. Defaults to 60s.
retry_until
expression
How long after it was dispatched it may still be retried, such as 1h.
unique_for
expression
While a job with the same fields is waiting or running, another dispatch of it is dropped, for this long at most, such as 10m.
unique_by
a list of names: [a, b]
The fields that make two dispatches the same for unique_for: [user_id]. Defaults to all of them.
retry_on
an error type
Only errors of these types are retried: [Timeout, Network]. Others fail the job at once.
fail_on
an error type
Errors of these types fail the job at once, without more tries: [Validation, NotFound].
listens_to
a string
An event the job listens to: each @dispatch_event() of it queues the job, with its fields filled from the event's.
fields
fields
The fields on one line, like fn params: user_id: int, email: email. Bigger jobs list them one per line.
description
a string
What the job does, shown on hover.
Option
Value
Description
queue
a string
The queue it goes on, unless the dispatch names one. Defaults to default.
connection
a string
The connection in kscript.json's queue.connections it goes to, unless the dispatch names one. Defaults to queue.default.
tries
a string
Attempts before it counts as failed. Defaults to 3.
backoff
expression
How long to wait before each retry: one time (30s), a list ([10s, 1m, 5m], the last one repeats), or 'exponential' (2s, 4s, 8s, up to 10m; the default).
timeout
expression
The longest one attempt may run, such as 30s or 5m. Past it the attempt stops with a JobTimeout error. Defaults to 60s.
retry_until
expression
How long after it was dispatched it may still be retried, such as 1h.
unique_for
expression
While a job with the same fields is waiting or running, another dispatch of it is dropped, for this long at most, such as 10m.
unique_by
a list of names: [a, b]
The fields that make two dispatches the same for unique_for: [user_id]. Defaults to all of them.
retry_on
an error type
Only errors of these types are retried: [Timeout, Network]. Others fail the job at once.
fail_on
an error type
Errors of these types fail the job at once, without more tries: [Validation, NotFound].
listens_to
a string
An event the job listens to: each @dispatch_event() of it queues the job, with its fields filled from the event's.
Listens for an event. Each <fn> inside takes the event and runs, in order, when @dispatch_event() dispatches it: listener for OrderPlaced { fn send_receipt(event: OrderPlaced) { } }. An error in one stops the rest and reaches the dispatch. Keep listeners in the project's listeners folder, whose scripts run the first time an event is dispatched. Work that should happen later goes in a job with listens_to: OrderPlaced instead.Listens for an event. Each fn inside takes the event and runs, in order, when @dispatch_event() dispatches it: listener for OrderPlaced { fn send_receipt(event: OrderPlaced) { } }. An error in one stops the rest and reaches the dispatch. Keep listeners in the project's listeners folder, whose scripts run the first time an event is dispatched. Work that should happen later goes in a job with listens_to: OrderPlaced instead.
Listens for an event (KScript.Queue). Write listener for OrderPlaced { fn send_receipt(event: OrderPlaced) { } }.
Holds statements.Holds statements in { }. Scripts only.
The schedule file (schedule.kscript): the tasks kscript schedule work runs at their times, each a <Task> with a cron schedule or an every: time, and a job to dispatch or a function to run.The schedule file (schedule.kscript): the tasks kscript schedule work runs at their times, each a Task with a cron schedule or an every: time, and a job to dispatch or a function to run.
Holds statements.Holds statements in { }. Scripts only.
A task of the schedule: when it runs (cron or every) and what it does (job, dispatched to its queue, or run, a function called by the scheduler). Task(cron: '0 3 * * *', job: PruneSessions(), without_overlapping: true).
Self-closing.A statement on its own. Scripts only. Goes inside <Schedule>Schedule.
AttributeOption
Value
Description
cron
a string
When it runs, as a cron schedule: '0 3 * * ' (03:00 every day), '/15 9-17 * * mon-fri', @hourly, @daily.
every
expression
How often it runs instead, in whole minutes: 1m, 5m, 1h, 1d. Counted from midnight, so every: 15m runs at :00, :15, :30 and :45.
job
expression
A job to dispatch each time: PruneSessions() or SendDigest(kind: 'daily'). It is built when the task is due and goes on its queue for kscript worker.
run
a function: @name
A function the scheduler calls each time, with no arguments: @tidy or @reports.nightly.
name
a string
The task's name in kscript schedule list and the scheduler's output. Defaults to the job's or the function's name.
without_overlapping
true or false
The task is skipped while its last run is still going (a dispatched job counts until a worker finishes it). Uses the queue's locks, so it holds across processes with a file or database connection.
queue
a string
The queue its job goes on, instead of the job's own.
connection
a string
The connection its job goes to, instead of the job's own.
description
a string
What the task is for, shown by kscript schedule list.
Jobs dispatched together, whose progress the queue keeps: @batch($rows->map(fn(r) => ImportRow(id: $r.id)))->then(ImportDone())->catch(ImportFailed())->dispatch(). then runs when every job succeeded, catch at the first failure, and finally when all have run. ->dispatch() returns the batch; @queue()->batch($batch.id).progress reads how far it has got.
Jobs that run one after another: each starts when the one before it succeeds, and a failure stops the rest. @chain([ImportCsv(path: $f), RebuildIndex()])->dispatch().
Tells every listener of an event: runs each listener for function with it, in order, then queues each job that listens_to it (after the transaction commits, inside one). Returns how many listeners it reached. Not @emit_event(), which sends an event to the browser.
Job middleware, for fn middleware(): at most limit jobs with this key start in each period, across every worker: @rate_limited('mail', limit: 10, per: 1m). A job over the limit goes back on its queue until the next period, without using up a try.
Job middleware, for fn middleware(): only one job of this type with the same key runs at a time; another goes back on its queue for release_after (10s by default) without using up a try. expires frees the key if a worker stops while holding it (the job's timeout by default).