Core\Queue\Stats::deadLettered
$stats->deadLettered(): uintCounts the jobs of one queue that failed for good.
Core\Queue::stats returns a Core\Queue\Stats, and deadLettered() is one of its numbers. A job that fails more times than its maxAttempts allows moves to the dead-letter table (a table of jobs that no worker runs again). deadLettered() counts the jobs of the queue in that table. A queue with no failed jobs returns 0.
A failed job stays in the dead-letter table until you delete it. Core\Queue::purge with State::Dead deletes these jobs, and Core\Queue::delete deletes one job. Only then does the number go down.
These jobs did not finish their work, so deadLettered() is a good number to alert on. The number of attempts they used is deadAttempts().
The examples below count the failed jobs, show that they stay until you delete them, and send an alert when more jobs failed since the last check.
Return value
Returns uint. A uint. It goes down only when purge or delete deletes a job from the dead-letter table.
Examples
The examples below run with this configuration file.
# The `jobs` database is kept in memory. It is deleted when the program ends.[db.jobs]driver = "sqlite"path = ":memory:"
# The program may open the `jobs` database itself, to create the tables.[capabilities.db]connect = ["jobs"]
# The program may delete the jobs of the `webhooks` queue.# Without this list, `Core\Queue::purge` throws an error.[capabilities.queue]purge = ["webhooks"]
# `Core\Queue` saves its jobs in the `jobs` database.# `workers = 0` starts no workers, so no job is started.[queue]connection = "jobs"workers = 0Count the failed jobs
<?nvs// Shows how to count the jobs of a queue that failed for good.use Core\Queue;
// On a real server, `nvs queue migrate` creates the queue tables.// This database is in memory, so the example creates them itself.var $db = Core\Db::connect('jobs');var $jobs = <<<'SQL'create table nvs_jobs (id integer primary key autoincrement, queue varchar(255) not null, script text not null, args text, state smallint not null, attempts integer not null, max_attempts integer not null, backoff_ms bigint not null, run_at bigint not null, tag varchar(255), dedupe_key varchar(255), dedupe_pending varchar(255), created_at bigint not null, claimed_at bigint, errors text, grants text, limits text, non_transactional smallint, token text, constraint nvs_jobs_dedupe unique (dedupe_pending))SQL;var $dead = <<<'SQL'create table nvs_dead_jobs (id bigint not null, queue varchar(255) not null, script text not null, args text, attempts integer not null, max_attempts integer not null, backoff_ms bigint not null, run_at bigint not null, tag varchar(255), dedupe_key varchar(255), created_at bigint not null, failed_at bigint not null, errors text not null, token text, primary key (id))SQL;$db->execute($jobs, []);$db->execute($dead, []);
// A job that waits for a worker has not failed. This prints `failed: 0`.Queue::push('jobs/call-webhook.nvs', { queue: 'webhooks' });echo 'failed: ', Queue::stats('webhooks')->deadLettered(), "\n";
// On a real server, a worker moves a job that failed too often to the dead-letter table.// No worker runs here, so SQL adds the rows. Two jobs failed for good.$db->execute(<<<'SQL'insert into nvs_dead_jobs (id, queue, script, attempts, max_attempts, backoff_ms, run_at, created_at, failed_at, errors)values (2, 'webhooks', 'jobs/call-webhook.nvs', 5, 5, 1000, 0, 0, 0, '[]'), (3, 'webhooks', 'jobs/call-webhook.nvs', 5, 5, 1000, 0, 0, 0, '[]')SQL, []);
// This prints `failed: 2, pending: 1`.var $stats = Queue::stats('webhooks');echo 'failed: ', $stats->deadLettered(), ', pending: ', $stats->pending(), "\n";failed: 0failed: 2, pending: 1Failed jobs stay until deleted
<?nvs// Shows that failed jobs stay counted until `Core\Queue::purge` deletes them.use Core\Queue;
// On a real server, `nvs queue migrate` creates the queue tables.// This database is in memory, so the example creates them itself.var $db = Core\Db::connect('jobs');var $jobs = <<<'SQL'create table nvs_jobs (id integer primary key autoincrement, queue varchar(255) not null, script text not null, args text, state smallint not null, attempts integer not null, max_attempts integer not null, backoff_ms bigint not null, run_at bigint not null, tag varchar(255), dedupe_key varchar(255), dedupe_pending varchar(255), created_at bigint not null, claimed_at bigint, errors text, grants text, limits text, non_transactional smallint, token text, constraint nvs_jobs_dedupe unique (dedupe_pending))SQL;var $dead = <<<'SQL'create table nvs_dead_jobs (id bigint not null, queue varchar(255) not null, script text not null, args text, attempts integer not null, max_attempts integer not null, backoff_ms bigint not null, run_at bigint not null, tag varchar(255), dedupe_key varchar(255), created_at bigint not null, failed_at bigint not null, errors text not null, token text, primary key (id))SQL;$db->execute($jobs, []);$db->execute($dead, []);
// Three jobs failed for good. No worker runs here, so SQL adds the rows.$db->execute(<<<'SQL'insert into nvs_dead_jobs (id, queue, script, attempts, max_attempts, backoff_ms, run_at, created_at, failed_at, errors)values (1, 'webhooks', 'jobs/call-webhook.nvs', 5, 5, 1000, 0, 0, 0, '[]'), (2, 'webhooks', 'jobs/call-webhook.nvs', 5, 5, 1000, 0, 0, 0, '[]'), (3, 'webhooks', 'jobs/call-webhook.nvs', 5, 5, 1000, 0, 0, 0, '[]')SQL, []);
// The failed jobs do not go away by themselves. This prints `failed: 3`.echo 'failed: ', Queue::stats('webhooks')->deadLettered(), "\n";
// You looked at the failed jobs and do not need them any more.// `Queue::State::Dead` deletes the jobs in the dead-letter table. This prints `deleted: 3`.echo 'deleted: ', Queue::purge('webhooks', { state: Queue::State::Dead }), "\n";
// This prints `failed: 0`.echo 'failed: ', Queue::stats('webhooks')->deadLettered(), "\n";failed: 3deleted: 3failed: 0Alert on new failures
<?nvs// Shows a check that sends an alert when more jobs of a queue failed since the last check.use Core\Queue;
// On a real server, `nvs queue migrate` creates the queue tables.// This database is in memory, so the example creates them itself.var $db = Core\Db::connect('jobs');var $jobs = <<<'SQL'create table nvs_jobs (id integer primary key autoincrement, queue varchar(255) not null, script text not null, args text, state smallint not null, attempts integer not null, max_attempts integer not null, backoff_ms bigint not null, run_at bigint not null, tag varchar(255), dedupe_key varchar(255), dedupe_pending varchar(255), created_at bigint not null, claimed_at bigint, errors text, grants text, limits text, non_transactional smallint, token text, constraint nvs_jobs_dedupe unique (dedupe_pending))SQL;var $dead = <<<'SQL'create table nvs_dead_jobs (id bigint not null, queue varchar(255) not null, script text not null, args text, attempts integer not null, max_attempts integer not null, backoff_ms bigint not null, run_at bigint not null, tag varchar(255), dedupe_key varchar(255), created_at bigint not null, failed_at bigint not null, errors text not null, token text, primary key (id))SQL;$db->execute($jobs, []);$db->execute($dead, []);
class FailureCheck{ // Returns an alert text, or an empty string when no new job failed. public static function run(string $queue, uint $before): string { uint $now = Queue::stats($queue)->deadLettered(); if ($now <= $before) { return ''; } return $queue . ': ' . ($now - $before) . ' new failed jobs, ' . $now . ' in total'; }}
// One job failed yesterday. Your check saved the count `1` then.// No worker runs here, so SQL adds the rows.$db->execute(<<<'SQL'insert into nvs_dead_jobs (id, queue, script, attempts, max_attempts, backoff_ms, run_at, created_at, failed_at, errors)values (1, 'webhooks', 'jobs/call-webhook.nvs', 5, 5, 1000, 0, 0, 0, '[]')SQL, []);uint $saved = 1;
// Nothing new failed. This prints `no alert`.string $alert = FailureCheck::run('webhooks', $saved);echo $alert == '' ? 'no alert' : $alert, "\n";
// Two more jobs fail for good.$db->execute(<<<'SQL'insert into nvs_dead_jobs (id, queue, script, attempts, max_attempts, backoff_ms, run_at, created_at, failed_at, errors)values (2, 'webhooks', 'jobs/call-webhook.nvs', 5, 5, 1000, 0, 0, 0, '[]'), (3, 'webhooks', 'jobs/call-webhook.nvs', 5, 5, 1000, 0, 0, 0, '[]')SQL, []);
// This prints `webhooks: 2 new failed jobs, 3 in total`.$alert = FailureCheck::run('webhooks', $saved);echo $alert == '' ? 'no alert' : $alert, "\n";no alertwebhooks: 2 new failed jobs, 3 in total