Core\Queue\Stats::claimed
$stats->claimed(): uintCounts the jobs of one queue that a worker is running now.
Core\Queue::stats returns a Core\Queue\Stats, and claimed() is one of its numbers. When a worker starts a job, the job is claimed. It stays claimed until the job finishes or fails. Jobs that wait for a worker are counted by pending(), not by claimed().
A worker can stop while it runs a job, for example when its server restarts. The job is then still counted in claimed(). After the [queue] visibility time, another worker can start the job again.
If claimed() equals the number of jobs your workers can run at once, every worker is busy.
The examples below count the running jobs, show a job whose worker stopped, and print a status line for a queue.
Return value
Returns uint. A uint. When it equals the number of jobs your workers can run at once, the queue is busy but not stuck.
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 running jobs
<?nvs// Shows how to count the jobs of a queue that a worker is running now.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' });Queue::push('jobs/resize-image.nvs', { queue: 'images' });
// No worker has started a job yet. This prints `claimed: 0, pending: 3`.var $stats = Queue::stats('images');echo 'claimed: ', $stats->claimed(), ', pending: ', $stats->pending(), "\n";
// On a real server, workers start the jobs. No worker runs here, so SQL changes the rows.// Two workers start a job each.$db->execute('update nvs_jobs set state = 1, attempts = 1, claimed_at = 1 where id <= 2', []);
// This prints `claimed: 2, pending: 1`.$stats = Queue::stats('images');echo 'claimed: ', $stats->claimed(), ', pending: ', $stats->pending(), "\n";
// The first job finishes. This prints `claimed: 1, pending: 1`.$db->execute('update nvs_jobs set state = 2 where id = 1', []);$stats = Queue::stats('images');echo 'claimed: ', $stats->claimed(), ', pending: ', $stats->pending(), "\n";claimed: 0, pending: 3claimed: 2, pending: 1claimed: 1, pending: 1A job whose worker stopped
<?nvs// Shows that a job is still claimed after its worker stopped.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-invoice.nvs', { queue: 'invoices' });
// A worker started the job a long time ago and then stopped.// No worker runs here, so SQL changes the row. `claimed_at` 0 is the year 1970.$db->execute('update nvs_jobs set state = 1, attempts = 1, claimed_at = 0', []);
// The job does not go back to `pending()` by itself. This prints `claimed: 1, pending: 0`.// After the `[queue] visibility` time, the next worker that looks for work starts it again.var $stats = Queue::stats('invoices');echo 'claimed: ', $stats->claimed(), ', pending: ', $stats->pending(), "\n";claimed: 1, pending: 0A status line for a queue
<?nvs// Shows a status line for a queue, as an admin page or a health check 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, []);
int $n = 0;while ($n < 6) { Queue::push('jobs/export.nvs', { queue: 'exports', arguments: ['n' => $n] }); $n = $n + 1;}
// On a real server, workers start the jobs. No worker runs here, so SQL changes the rows.// Four workers start a job each.$db->execute('update nvs_jobs set state = 1, attempts = 1, claimed_at = 1 where id <= 4', []);
// Your server runs 4 workers for this queue.uint $workers = 4;var $stats = Queue::stats('exports');
// This prints `exports: 4 of 4 workers busy, 2 jobs waiting`, and then a note.echo 'exports: ', $stats->claimed(), ' of ', $workers, ' workers busy, ';echo $stats->pending(), " jobs waiting\n";if ($stats->claimed() >= $workers && $stats->pending() > 0) { echo "note: every worker is busy, so new jobs wait\n";}exports: 4 of 4 workers busy, 2 jobs waitingnote: every worker is busy, so new jobs wait