Core\Queue::stats
Core\Queue::stats(string $queue): Core\Queue\StatsCore\Queue::stats( string $queue, ): Core\Queue\Stats
Counts the jobs of one queue.
Core\Queue::stats returns a Core\Queue\Stats with six numbers. pending() is the number of jobs that wait for a worker. claimed() is the number of jobs that a worker is running now. attempts() is how many times the jobs in the jobs table have started. deadLettered() is the number of jobs in the dead-letter table (the table of jobs that failed too often). deadAttempts() is how many times those jobs started. nonTransactional() is the number of jobs in the jobs table that were pushed inside a transaction of a different database. Such a job is saved at once, so a rollback of that transaction does not delete it.
All six numbers are read at the same moment, so they agree with each other. A queue with no jobs returns six zeros. Finished and cancelled jobs are not waiting and not running, so pending() and claimed() do not count them.
The examples below count the waiting jobs, count two queues one at a time, and check the health of a queue.
Parameters
$queuestringThe name of the queue, the same namepushused in itsqueueoption. Each call counts one queue.
Return value
Returns Core\Queue\Stats. A Core\Queue\Stats. Each number is a method, so you write $stats->pending(), not $stats->pending.
Errors
RuntimeErrorThe configuration has no[queue]block, or the database driver of the queue's connection cannot run statements yet.IOErrorThe connection to the database failed, or the database rejected the query. The most common cause is thatnvs queue migratehas not created the tables yet.
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 waiting jobs
<?nvs// Shows how to count the jobs that wait in a queue.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'] });Queue::push('jobs/send-mail.nvs', { queue: 'mail', arguments: ['to' => 'eva@example.com'] });
// No worker runs, so all three jobs wait.// This prints `waiting: 3` and `running: 0`.var $stats = Queue::stats('mail');echo 'waiting: ', $stats->pending(), "\n";echo 'running: ', $stats->claimed(), "\n";waiting: 3running: 0Each queue is counted alone
<?nvs// Shows that `stats` counts one queue, and that a queue with no jobs returns zeros.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' });Queue::push('jobs/export.nvs', { queue: 'exports' });Queue::push('jobs/export.nvs', { queue: 'exports' });
// One call counts one queue. Nobody added a job to `images`.// This prints `mail: 1`, `exports: 2` and `images: 0`.array<string> $queues = ['mail', 'exports', 'images'];foreach ($queues as string $queue) { echo $queue, ': ', Queue::stats($queue)->pending(), "\n";}mail: 1exports: 2images: 0A health check for a queue
<?nvs// Shows a health check that warns when a queue has too many waiting jobs or failed jobs.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 < 4) { Queue::push('jobs/send-mail.nvs', { queue: 'mail', arguments: ['n' => $n] }); $n = $n + 1;}
// 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 = 1, attempts = 1 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 (90, 'mail', 'jobs/send-mail.nvs', 5, 5, 1000, 0, 0, 0, '[]')SQL, []);
// This prints `mail: 3 waiting, 1 running, 1 failed`,// and then a warning, because a job failed.var $stats = Queue::stats('mail');echo 'mail: ', $stats->pending(), ' waiting, ', $stats->claimed(), ' running, ', $stats->deadLettered(), " failed\n";if ($stats->pending() > 100 || $stats->deadLettered() > 0) { echo "warning: the mail queue needs attention\n";}mail: 3 waiting, 1 running, 1 failedwarning: the mail queue needs attention