Skip to content

Core\Queue\Stats::deadAttempts

$stats->deadAttempts(): uint

Counts how many times the failed jobs of one queue were started before they failed for good.

Core\Queue::stats returns a Core\Queue\Stats, and deadAttempts() is one of its numbers. A job that fails too often moves to the dead-letter table (a table of jobs that no worker runs again). deadAttempts() adds up the attempts of the jobs of the queue in that table. A queue with no failed jobs returns 0.

Every attempt is counted in attempts() or in deadAttempts(), never in both. When a job moves to the dead-letter table, its attempts move with it. Add the two numbers to get every attempt of the queue.

deadLettered() counts the failed jobs. Divide deadAttempts() by it to see how many attempts a job uses before it fails for good.

The examples below count the attempts of failed jobs, compute the attempts per failed job, and print a report of failed work for several queues.

Return value

Returns uint. A uint for the dead-letter table only. Every attempt is counted in this number or in attempts, never in both. Add the two to get the total.

Examples

The examples below run with this configuration file.

nvs.toml
# 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"]
# `Core\Queue` saves its jobs in the `jobs` database.
# `workers = 0` starts no workers, so no job is started.
[queue]
connection = "jobs"
workers = 0

Count the attempts of failed jobs

<?nvs
// Shows how to count the attempts of 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, []);
// No job has failed yet. This prints `failed attempts: 0`.
echo 'failed attempts: ', Queue::stats('webhooks')->deadAttempts(), "\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. One job used 5 attempts, the other used 3.
$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', 3, 3, 1000, 0, 0, 0, '[]')
SQL, []);
// This prints `failed attempts: 8`.
echo 'failed attempts: ', Queue::stats('webhooks')->deadAttempts(), "\n";
Output
failed attempts: 0
failed attempts: 8

Attempts per failed job

<?nvs
// Shows how many attempts a job of a queue uses, on average, before it fails 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, []);
// Three jobs failed for good. They used 5, 5 and 2 attempts.
// 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, 'mail', 'jobs/send-mail.nvs', 5, 5, 1000, 0, 0, 0, '[]'),
(2, 'mail', 'jobs/send-mail.nvs', 5, 5, 1000, 0, 0, 0, '[]'),
(3, 'mail', 'jobs/send-mail.nvs', 2, 2, 1000, 0, 0, 0, '[]')
SQL, []);
// `deadAttempts()` is 12 and `deadLettered()` is 3.
// This prints `4 attempts per failed job`.
var $stats = Queue::stats('mail');
if ($stats->deadLettered() > 0) {
echo $stats->deadAttempts() / $stats->deadLettered(), " attempts per failed job\n";
}
Output
4 attempts per failed job

A report of failed work

<?nvs
// Shows a report of the work that failed in each queue, as a daily report prints it.
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, []);
// Two jobs of `mail` and one job of `exports` 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, 'mail', 'jobs/send-mail.nvs', 5, 5, 1000, 0, 0, 0, '[]'),
(2, 'mail', 'jobs/send-mail.nvs', 5, 5, 1000, 0, 0, 0, '[]'),
(3, 'exports', 'jobs/export.nvs', 3, 3, 1000, 0, 0, 0, '[]')
SQL, []);
// This prints one line for each queue. The first line is `mail: 2 failed, 10 attempts used`.
// `images` has no failed jobs.
array<string> $queues = ['mail', 'exports', 'images'];
foreach ($queues as string $queue) {
var $stats = Queue::stats($queue);
if ($stats->deadLettered() == 0) {
echo $queue, ": no failed jobs\n";
} else {
echo $queue, ': ', $stats->deadLettered(), ' failed, ';
echo $stats->deadAttempts(), " attempts used\n";
}
}
Output
mail: 2 failed, 10 attempts used
exports: 1 failed, 3 attempts used
images: no failed jobs