Core\Queue\Stats::attempts
$stats->attempts(): uintCounts how many times the jobs of one queue have started.
Core\Queue::stats returns a Core\Queue\Stats, and attempts() is one of its numbers. Each time a worker starts a job, the job uses one attempt. attempts() adds up the attempts of every job of the queue in the jobs table. Waiting, running, finished and cancelled jobs are all counted. A new job has not started yet, so a queue of new jobs returns 0.
A job that fails too often moves to the dead-letter table, and its attempts move with it. deadAttempts() counts them there. Add the two numbers to get every attempt of the queue.
If attempts() grows between two checks and pending() does not go down, jobs fail and run again.
The examples below count the attempts of a queue, add the attempts of failed jobs, and warn when jobs fail and run again.
Return value
Returns uint. A uint for the jobs table only. The jobs in the dead-letter table are counted by deadLettered and deadAttempts.
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"]
# `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 attempts of a queue
<?nvs// Shows how to count how many times the jobs of a queue have started.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, []);
Queue::push('jobs/resize-image.nvs', { queue: 'images' });Queue::push('jobs/resize-image.nvs', { queue: 'images' });
// The new jobs have not started yet. This prints `attempts: 0`.echo 'attempts: ', Queue::stats('images')->attempts(), "\n";
// On a real server, workers start the jobs. No worker runs here, so SQL changes the rows.// The first job finished at its first attempt. The second job failed twice and waits.$db->execute('update nvs_jobs set state = 2, attempts = 1 where id = 1', []);$db->execute('update nvs_jobs set state = 0, attempts = 2 where id = 2', []);
// This prints `attempts: 3`. The finished job is counted too.echo 'attempts: ', Queue::stats('images')->attempts(), "\n";attempts: 0attempts: 3Add the attempts of failed jobs
<?nvs// Shows how to count every attempt of a queue, with the attempts of the jobs that failed.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, []);
Queue::push('jobs/send-mail.nvs', { queue: 'mail', arguments: ['to' => 'ana@example.com'] });Queue::push('jobs/send-mail.nvs', { queue: 'mail', arguments: ['to' => 'ben@example.com'] });
// On a real server, workers run the jobs and move a job that failed 5 times// to the dead-letter table. No worker runs here, so SQL changes the rows.$db->execute('update nvs_jobs set state = 2, attempts = 2 where id = 1', []);$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, 'mail', 'jobs/send-mail.nvs', 5, 5, 1000, 0, 0, 0, '[]')SQL, []);$db->execute('delete from nvs_jobs where id = 2', []);
// `attempts()` counts the jobs table. `deadAttempts()` counts the dead-letter table.// This prints `attempts: 2`, `failed: 5` and `total: 7`.var $stats = Queue::stats('mail');echo 'attempts: ', $stats->attempts(), "\n";echo 'failed: ', $stats->deadAttempts(), "\n";echo 'total: ', $stats->attempts() + $stats->deadAttempts(), "\n";attempts: 2failed: 5total: 7Warn when jobs run again
<?nvs// Shows a check that warns when the jobs of a queue fail and run again.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, []);
int $n = 0;while ($n < 3) { Queue::push('jobs/export.nvs', { queue: 'exports', arguments: ['n' => $n] }); $n = $n + 1;}
// The check reads the numbers two times.var $before = Queue::stats('exports');
// On a real server, workers run the jobs between the two checks. Here, every job// fails two times and waits again. No worker runs, so SQL changes the rows.$db->execute('update nvs_jobs set attempts = attempts + 2', []);
var $after = Queue::stats('exports');uint $retries = $after->attempts() - $before->attempts();
// This prints `6 new attempts, 3 jobs waiting`, and then a warning,// because the jobs started again and no job finished.echo $retries, ' new attempts, ', $after->pending(), " jobs waiting\n";if ($retries > 0 && $after->pending() >= $before->pending()) { echo "warning: jobs in the exports queue fail and run again\n";}6 new attempts, 3 jobs waitingwarning: jobs in the exports queue fail and run again