Skip to main content

Job context

Every job's handle method receives a Zeridion\Flare\Worker\JobContext. It carries the job's metadata, lets you report progress, exposes the cooperative cancellation handle, and provides a logger pre-bound with this job's identity. You never construct it yourself — the worker provides it.

Metadata

PropertyTypeDescription
$ctx->jobIdstringUnique id for this execution. Use it as your idempotency key.
$ctx->attemptint1-based attempt number.
$ctx->maxAttemptsintConfigured maximum attempts.
$ctx->jobTypestringThe job's wire type.
$ctx->enqueuedAtstringWhen the job was originally enqueued (RFC-3339). Parse defensively.

Reporting progress

$ctx->reportProgress(0.5); // halfway

Progress is a value from 0.0 to 1.0 and surfaces on the dashboard. The reported value rides the next heartbeat — calling reportProgress does not itself make a network request. Values are clamped (a value above 1 becomes 1); 0, negative, NaN, and infinite values are ignored. The most recent valid value wins.

Cancellation

Cancellation is cooperative — the worker cannot forcibly interrupt a running handler, so a long-running job must check the handle and stop on its own.

public function handle(mixed $payload, JobContext $ctx): void
{
foreach ($this->workItems() as $item) {
if ($ctx->cancellationRequested()) {
return; // stop promptly
}
$this->process($item);
}
}
MethodDescription
$ctx->cancellationRequested(): boolWhether cancellation (timeout, server cancel, or shutdown) was requested.
$ctx->throwIfCancelled(): voidThrows if cancellation was requested — a convenient way to bail out.

A cancelled job is acknowledged as failed. A handler that ignores the handle and never yields keeps running until the server reclaims the job.

Logging

$ctx->logger()->log('info', 'sending email', ['email' => $payload['email']]);

The logger is pre-bound with the job's id, type, and attempt, so your handler's log lines automatically carry the same correlation fields the worker emits. Avoid logging raw payloads or secrets.

See also