Skip to content
docsv0.3.0

BackgroundJob

An immutable snapshot of one background job's state, as tracked by a {@see \Milpa\Live\Contracts\Tui\BackgroundJobManagerInterface}. `$status` MUST be one of `'running'`, `'done'`, `'failed'`, or `'canceled'`; every `with*()` method returns a new instance rather than mutating this one, so callers can never observe a job partway through a transition.

BackgroundJob::__construct()

public function __construct(string $id, string $label, string $status, string $command = '', float $progress = 0, array $output = [], ?int $exitCode = null, ?string $error = null, array $meta = []):

Parameters

Parameters of __construct()
NameTypeDescription
$idstringA unique job id; MUST NOT be empty.
$labelstringHuman-readable job description.
$statusstringOne of `'running'`, `'done'`, `'failed'`, `'canceled'`.
$commandstringThe command/operation this job represents, if applicable.
$progressfloatFractional completion in `[0.0, 1.0]`.
$outputarray<int, array{stream: string, line: string}>Captured output lines in emission order.
$exitCode(int | null)Process exit code, once the job has finished/failed/been canceled.
$error(string | null)Failure reason, when `$status` is `'failed'`.
$metaarray<string, mixed>Caller-defined extra context beyond the fields above.

Throws

\InvalidArgumentException If `$id` is empty.

BackgroundJob::withOutput()

public function withOutput(string $line, string $stream = 'stdout'): self

Returns a copy with one more output line appended.

Parameters

Parameters of withOutput()
NameTypeDescription
$linestring
$streamstring

BackgroundJob::withProgress()

public function withProgress(float $progress): self

Returns a copy with progress updated, clamped to `[0.0, 1.0]`.

Parameters

Parameters of withProgress()
NameTypeDescription
$progressfloat

BackgroundJob::withStatus()

public function withStatus(string $status, ?int $exitCode = null, ?string $error = null): self

Returns a copy transitioned to a new status. Forces `$progress` to `1.0` when `$status` is one of the terminal statuses (`'done'`, `'failed'`, `'canceled'`), so a finished job is never left showing partial progress.

Parameters

Parameters of withStatus()
NameTypeDescription
$statusstring
$exitCode?int
$error?string