Core\Queue::State
What has become of a background job. Asking the queue about a job you handed it earlier answers with one of five states, and between them they cover everything a job can be doing.
Core\Queue::State is declared in the class Core\Queue. The method status returns it.
Pending means it is waiting for a worker to pick it up — including while its start time is still in the future, and while it waits between attempts. Claimed means a worker is holding it and running it right now. Succeeded means it ran to the end. Dead means it used up every attempt it was allowed and has been set aside, with its payload and each attempt's error, where it stays until a person deals with it. Cancelled means it was called off before any worker took it.
Good to know: there is no Failed state, because a failed attempt does not end a job. It sends the job back to Pending to be tried again, and only running out of attempts makes it Dead. So the one state that means work somebody asked for never happened is Dead, and that is the one worth watching.
Examples
The five states a job can be in
<?nvs// Every background job is in exactly one of five states. This walks all five// and says, for each one, what it means and whether the work is still going to// happen.
class Jobs{ public static function meaning(Core\Queue::State $state): string { return match ($state) { Core\Queue::State::Pending => 'waiting for a worker to pick it up', Core\Queue::State::Claimed => 'a worker is running it right now', Core\Queue::State::Succeeded => 'it ran to the end', Core\Queue::State::Dead => 'it used up every attempt and was set aside', Core\Queue::State::Cancelled => 'it was called off before anyone took it', }; }
// Two of the five are on their way somewhere. The other three are where // the job came to rest. public static function stillToCome(Core\Queue::State $state): bool { return match ($state) { Core\Queue::State::Pending => true, Core\Queue::State::Claimed => true, default => false, }; }}
array<Core\Queue::State> $states = [ 'Pending' => Core\Queue::State::Pending, 'Claimed' => Core\Queue::State::Claimed, 'Succeeded' => Core\Queue::State::Succeeded, 'Dead' => Core\Queue::State::Dead, 'Cancelled' => Core\Queue::State::Cancelled,];
foreach ($states as string $name => Core\Queue::State $state) { echo $name, ': ', Jobs::meaning($state), Jobs::stillToCome($state) ? ' (still to come)' : '', "\n";}Pending: waiting for a worker to pick it up (still to come)Claimed: a worker is running it right now (still to come)Succeeded: it ran to the endDead: it used up every attempt and was set asideCancelled: it was called off before anyone took itA failed attempt sends a job back to pending
<?nvs// There is no `Failed` state, and this shows why none is needed. A job whose// attempt goes wrong is tried again, so the attempt leaves it back at// `Pending`. Only running out of attempts ends it, as `Dead`.
class Attempt{ // Where one attempt leaves a job: it worked, or there is another attempt // left, or there is not. public static function leaves(bool $worked, uint $used, uint $allowed): Core\Queue::State { if ($worked) { return Core\Queue::State::Succeeded; } return $used < $allowed ? Core\Queue::State::Pending : Core\Queue::State::Dead; }
public static function name(Core\Queue::State $state): string { return match ($state) { Core\Queue::State::Pending => 'Pending', Core\Queue::State::Claimed => 'Claimed', Core\Queue::State::Succeeded => 'Succeeded', Core\Queue::State::Dead => 'Dead', Core\Queue::State::Cancelled => 'Cancelled', }; }}
uint $allowed = 3;uint $used = 0;Core\Queue::State $state = Core\Queue::State::Pending;
// The payment provider is down all afternoon, so every attempt goes wrong.// The job keeps coming back until the third one, which has nothing after it.while ($state == Core\Queue::State::Pending) { $used = $used + 1; $state = Attempt::leaves(false, $used, $allowed); echo 'attempt ', $used, ' of ', $allowed, ' -> ', Attempt::name($state), "\n";}
// The same job on a day the provider answers. One attempt and it is over.echo "\n";echo 'attempt 1 of ', $allowed, ' -> ', Attempt::name(Attempt::leaves(true, 1, $allowed)), "\n";attempt 1 of 3 -> Pendingattempt 2 of 3 -> Pendingattempt 3 of 3 -> Dead
attempt 1 of 3 -> SucceededThe check somebody does every morning
<?nvs// A morning check of last night's queue. A job has one of five states.// Only `Dead` needs a person. A `Dead` job used up every attempt and never ran to the end.// The other states are finished, cancelled, waiting, or running now.
class Board{ public static function needsAPerson(Core\Queue::State $state): bool { return $state == Core\Queue::State::Dead; }
public static function onItsWay(Core\Queue::State $state): bool { return $state == Core\Queue::State::Pending || $state == Core\Queue::State::Claimed; }}
array<Core\Queue::State> $lastNight = [ 'invoice 1041' => Core\Queue::State::Succeeded, 'invoice 1042' => Core\Queue::State::Dead, 'welcome mail for ada' => Core\Queue::State::Succeeded, 'nightly report' => Core\Queue::State::Claimed, 'invoice 1043' => Core\Queue::State::Pending, 'welcome mail for grace' => Core\Queue::State::Cancelled,];
uint $coming = 0;uint $stuck = 0;
foreach ($lastNight as string $job => Core\Queue::State $state) { if (Board::onItsWay($state)) { $coming = $coming + 1; } if (Board::needsAPerson($state)) { $stuck = $stuck + 1; echo 'needs a person: ', $job, "\n"; }}
echo "\n", $coming, ' job(s) still on their way, ', $stuck, " waiting for a person\n";
// The cancelled job is not on the list, because nobody wanted it to run.// A `Dead` job did not run either, but somebody still needs it to.needs a person: invoice 1042
2 job(s) still on their way, 1 waiting for a person