Skip to content

Core\Queue\Stats::pending

$stats->pending(): uint

Counts the jobs of one queue that wait for a worker.

Core\Queue::stats returns a Core\Queue\Stats, and pending() is one of its numbers. A job is pending from the moment Core\Queue::push saves it until a worker starts it. A job that a worker runs now is counted by claimed(), not by pending().

Some pending jobs cannot start yet. A job pushed with a runAt time in the future is pending. A job that failed and waits before its next attempt is pending too. So pending() can be larger than the work your workers can start right now.

If pending() grows and does not go down, your workers cannot keep up with the queue.

The examples below count the waiting jobs, show jobs that wait for a later time, and warn when too many jobs wait in a queue.

Return value

Returns uint. A uint. It is 0 for a new queue and for a queue with no work left.

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 waiting jobs

<?nvs
// Shows how to count the jobs of a queue that wait for a worker.
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 new queue has no jobs. This prints `pending: 0`.
echo 'pending: ', Queue::stats('emails')->pending(), "\n";
Queue::push('jobs/send-email.nvs', { queue: 'emails' });
Queue::push('jobs/send-email.nvs', { queue: 'emails' });
Queue::push('jobs/send-email.nvs', { queue: 'emails' });
// This prints `pending: 3`.
echo 'pending: ', Queue::stats('emails')->pending(), "\n";
// On a real server, a worker starts a job. No worker runs here, so SQL changes the row.
// A running job is counted by `claimed()`. This prints `pending: 2, claimed: 1`.
$db->execute('update nvs_jobs set state = 1, attempts = 1, claimed_at = 1 where id = 1', []);
var $stats = Queue::stats('emails');
echo 'pending: ', $stats->pending(), ', claimed: ', $stats->claimed(), "\n";
Output
pending: 0
pending: 3
pending: 2, claimed: 1

Jobs that wait for later

<?nvs
// Shows that a job that must wait for a later time is also pending.
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, []);
// The first job may start now. `runAt` says the second job may start in one hour.
Queue::push('jobs/send-reminder.nvs', { queue: 'reminders' });
Queue::push('jobs/send-reminder.nvs', { queue: 'reminders', runAt: Core\Time::now()->plus(1h) });
// Both jobs wait for a worker. This prints `pending: 2`.
echo 'pending: ', Queue::stats('reminders')->pending(), "\n";
// On a real server, a worker starts the first job and it fails.
// The job then waits before its next attempt. No worker runs here, so SQL changes the row.
$db->execute('update nvs_jobs set attempts = 1, run_at = 9000000000000 where id = 1', []);
// A job that waits between two attempts is still pending. This prints `pending: 2, claimed: 0`.
var $stats = Queue::stats('reminders');
echo 'pending: ', $stats->pending(), ', claimed: ', $stats->claimed(), "\n";
Output
pending: 2
pending: 2, claimed: 0

Warn when work piles up

<?nvs
// Shows a health check that warns when too many jobs 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, []);
int $n = 0;
while ($n < 12) {
Queue::push('jobs/export.nvs', { queue: 'exports', arguments: ['n' => $n] });
$n = $n + 1;
}
Queue::push('jobs/send-email.nvs', { queue: 'emails' });
// Your team wants a warning when more than 10 jobs wait in one queue.
uint $limit = 10;
array<string> $queues = ['exports', 'emails'];
// This prints `exports: 12 waiting, too many` and then `emails: 1 waiting, ok`.
foreach ($queues as string $name) {
uint $waiting = Queue::stats($name)->pending();
echo $name, ': ', $waiting, ' waiting, ', $waiting > $limit ? 'too many' : 'ok', "\n";
}
Output
exports: 12 waiting, too many
emails: 1 waiting, ok