KScript docs
EN

batch fields and methods

A field is a plain fact about the value, read with a dot and no brackets: $value.name. A method takes arguments or does work, and is called with ->: $value->name(arguments), or with the value piped in: $value |> name(arguments).

Fields

FieldTypeDescription
.created_atdateWhen it was dispatched.
.failed_countnumberHow many of its jobs failed, after their tries.
.failed_idslistThe ids of the jobs that failed, for @queue()->retry(id).
.finished_atdateWhen its last job finished, or null.
.idstringThe batch's id, for @queue()->batch(id). Null until it is dispatched.
.is_finishedboolTrue once every job has finished.
.pendingnumberHow many of its jobs haven't finished yet.
.processednumberHow many of its jobs have finished, whether they succeeded or failed.
.progressnumberHow much of the batch has finished, from 0 to 100.
.totalnumberHow many jobs the batch has.

From KScript.Queue

These need <import package="KScript.Queue" />import KScript.Queue. See KScript.Queue.

#batch->catch(job: job) → batch KScript.Queue

The job to dispatch when the first job of the batch fails. Returns the batch, to chain.
ParameterTakes
job requiredjob
Example
<job name="Step" fields="n: int">
  <fn name="handle"><if test="$n == 2"><do>@fail_job('bad row')</do></if></fn>
</job>
<job name="Report">
  <fn name="handle"><echo>A step failed</echo></fn>
</job>
<let name="batch" value="@batch([Step(n: 1), Step(n: 2)])->catch(Report())->dispatch()" />
<echo>{ $batch.failed_count } failed</echo>
job Step {
  n: int
  fn handle() {
    if .n == 2 {
      @fail_job('bad row')
    }
  }
}
job Report {
  fn handle() {
    echo 'A step failed'
  }
}
$batch = @batch([Step(n: 1), Step(n: 2)])->catch(Report())->dispatch()
echo "{ $batch.failed_count } failed"

#batch->dispatch(queue?: string(min: 1), connection?: string(min: 1), after_commit?: bool) → batch KScript.Queue

Queues the batch's jobs and returns the batch, with its id. Inside a transaction it waits for the commit, as a job does. On the sync connection the jobs run now.
ParameterTakes
queue optionalstring(min: 1)
connection optionalstring(min: 1)
after_commit optionala flag: after_commit or not after_commit

Arguments can be named: ->dispatch(…, connection: …, after_commit: …).

Example
<job name="Step" fields="n: int">
  <fn name="handle"><echo>Step { $n }</echo></fn>
</job>
<let name="batch" value="@batch([Step(n: 1), Step(n: 2)])->dispatch()" />
<echo>{ $batch.processed } of { $batch.total }</echo>
job Step {
  n: int
  fn handle() {
    echo "Step { .n }"
  }
}
$batch = @batch([Step(n: 1), Step(n: 2)])->dispatch()
echo "{ $batch.processed } of { $batch.total }"

#batch->finally(job: job) → batch KScript.Queue

The job to dispatch once every job of the batch has run, whether or not they succeeded. Returns the batch, to chain.
ParameterTakes
job requiredjob
Example
<job name="Step" fields="n: int">
  <fn name="handle"><echo>Step { $n }</echo></fn>
</job>
<job name="Tidy">
  <fn name="handle"><echo>Tidied up</echo></fn>
</job>
<do>@batch([Step(n: 1)])->finally(Tidy())->dispatch()</do>
job Step {
  n: int
  fn handle() {
    echo "Step { .n }"
  }
}
job Tidy {
  fn handle() {
    echo 'Tidied up'
  }
}
@batch([Step(n: 1)])->finally(Tidy())->dispatch()

#batch->then(job: job) → batch KScript.Queue

The job to dispatch once every job of the batch has succeeded. Returns the batch, to chain.
ParameterTakes
job requiredjob
Example
<job name="Step" fields="n: int">
  <fn name="handle"><echo>Step { $n }</echo></fn>
</job>
<job name="Done">
  <fn name="handle"><echo>All done</echo></fn>
</job>
<do>@batch([Step(n: 1)])->then(Done())->dispatch()</do>
job Step {
  n: int
  fn handle() {
    echo "Step { .n }"
  }
}
job Done {
  fn handle() {
    echo 'All done'
  }
}
@batch([Step(n: 1)])->then(Done())->dispatch()

Methods from any