KScript docs
EN

Queues and jobs: KScript.Queue

Events and listeners

An event says that something happened, such as an order being placed. The code that places the order dispatches the event, and listeners react to it: send a receipt, update a report, tell the warehouse. The order code doesn't need to know about any of them.

Declare an event with event, and the fields its listeners need. Keep events in the project's events folder (kscript make event OrderPlaced):

<import package="KScript.Queue" />

<event name="OrderPlaced">
  <field name="order_id" type="int" />
  <field name="total" type="number" />
</event>
import KScript.Queue

event OrderPlaced {
  order_id: int
  total: number
}

A listener says what to do when an event is dispatched. Each function inside takes the event, and they run in order. Keep listeners in the listeners folder (kscript make listener OrderPlaced). The first @dispatch_event() runs the scripts there, so the code that dispatches doesn't import them:

<import package="KScript.Queue" />

<event name="OrderPlaced">
  <field name="order_id" type="int" />
  <field name="total" type="number" />
</event>

<listener for="OrderPlaced">
  <fn name="log_order" params="event: OrderPlaced">
    <log>Order {$event.order_id} placed</log>
  </fn>

  <fn name="thank" params="event: OrderPlaced">
    <echo>Thanks for spending {$event.total}</echo>
  </fn>
</listener>

<let name="reached" value="@dispatch_event(OrderPlaced(order_id: 7, total: 25))" />
<echo>{$reached} listeners</echo>
import KScript.Queue

event OrderPlaced {
  order_id: int
  total: number
}

listener for OrderPlaced {
  fn log_order(event: OrderPlaced) {
    log "Order {$event.order_id} placed"
  }

  fn thank(event: OrderPlaced) {
    echo "Thanks for spending {$event.total}"
  }
}

$reached = @dispatch_event(OrderPlaced(order_id: 7, total: 25))
echo "{$reached} listeners"

@dispatch_event(event) returns how many listeners it reached. An error in a listener stops the ones after it, and reaches the code that dispatched the event. This is not @emit_event(), which sends an event to the browser in a live page.

Queued listeners

Work that shouldn't make the dispatch wait goes in a job declared with listens_to. Each dispatch of the event queues the job, with its fields filled from the event's fields of the same names. Inside a transaction, the job is queued when it commits, like any other dispatch:

<import package="KScript.Queue" />

<event name="OrderPlaced">
  <field name="order_id" type="int" />
  <field name="total" type="number" />
</event>

<job name="SendReceipt" listens_to="OrderPlaced" queue="mail">
  <field name="order_id" type="int" />

  <fn name="handle">
    <log>Sending the receipt for order {.order_id}</log>
  </fn>
</job>

<let name="queue" value="@fake_queue()" />
<do>@dispatch_event(OrderPlaced(order_id: 7, total: 25))</do>
<echo value="$queue->count_of(SendReceipt)" />
import KScript.Queue

event OrderPlaced {
  order_id: int
  total: number
}

job SendReceipt(listens_to: OrderPlaced, queue: 'mail') {
  order_id: int

  fn handle() {
    log "Sending the receipt for order {.order_id}"
  }
}

$queue = @fake_queue()
@dispatch_event(OrderPlaced(order_id: 7, total: 25))
echo $queue->count_of(SendReceipt)

Each field of the job must be one of the event's, or have a default.