Skip to content

Core\Queue\Stats::nonTransactional

$stats->nonTransactional(): uint

Counts the jobs of one queue that were pushed inside a transaction of another database.

Core\Queue::stats returns a Core\Queue\Stats, and nonTransactional() is one of its numbers. Core\Queue saves its jobs in the database that [queue] connection names. Your program can push a job while a transaction is open on a different database. That job is saved at once, and it is not part of the transaction. If the transaction is rolled back, the job stays in the queue.

nonTransactional() counts these jobs in the jobs table. A push outside any transaction is not counted. A push inside a transaction on the queue's own database is not counted either, because a rollback deletes that job too. Jobs in the dead-letter table (the table of jobs that failed too often) are not counted.

A number above 0 means that some jobs may belong to work that was rolled back. To keep a job and its data together, push the job inside a transaction on the queue's database.

The examples below count a push inside another database's transaction, show that a rollback keeps the job, and warn about it in a health check.

Return value

Returns uint. A uint for the jobs table only. It is 0 when every transaction that pushes a job runs on the connection that [queue] connection names.

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 `shop` database is a second database in memory, for the orders of a shop.
[db.shop]
driver = "sqlite"
path = ":memory:"
# The program may open both databases itself, to create the tables.
[capabilities.db]
connect = ["jobs", "shop"]
# `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 a push in another database

<?nvs
// Shows how to count the jobs that were pushed inside a transaction of another database.
use Core\Db\Transaction;
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 orders are in the `shop` database. The jobs are in the `jobs` database.
var $shop = Core\Db::connect('shop');
$shop->execute('create table orders (id integer primary key)', []);
// This push is not inside a transaction. This prints `pushed in another transaction: 0`.
Queue::push('jobs/send-receipt.nvs', { queue: 'mail' });
echo 'pushed in another transaction: ', Queue::stats('mail')->nonTransactional(), "\n";
// This push is inside a transaction of the `shop` database.
// The job is saved in the `jobs` database at once, so it is counted.
$shop->runTransaction(fn(Transaction $tx): void => {
$tx->execute('insert into orders (id) values (?)', [1]);
Queue::push('jobs/send-receipt.nvs', { queue: 'mail' });
});
// This prints `pushed in another transaction: 1`.
echo 'pushed in another transaction: ', Queue::stats('mail')->nonTransactional(), "\n";
Output
pushed in another transaction: 0
pushed in another transaction: 1

A rollback keeps the job

<?nvs
// Shows that a rollback in another database does not delete a job, and how to avoid that.
use Core\Db\Transaction;
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, []);
var $shop = Core\Db::connect('shop');
$shop->execute('create table orders (id integer primary key)', []);
// The order is rolled back, but the job was already saved in the `jobs` database.
try {
$shop->runTransaction(fn(Transaction $tx): void => {
$tx->execute('insert into orders (id) values (?)', [1]);
Queue::push('jobs/send-receipt.nvs', { queue: 'mail' });
$tx->rollBack('payment declined');
});
} catch (Core\Db\RolledBack $e) {
echo 'rolled back: ', $e->reason, "\n";
}
// This prints `waiting: 1, pushed in another transaction: 1`.
var $stats = Queue::stats('mail');
echo 'waiting: ', $stats->pending(), ', pushed in another transaction: ';
echo $stats->nonTransactional(), "\n";
// A transaction on the `jobs` database contains the push. A rollback deletes the job too.
try {
$db->runTransaction(fn(Transaction $tx): void => {
Queue::push('jobs/send-receipt.nvs', { queue: 'mail' });
$tx->rollBack('payment declined');
});
} catch (Core\Db\RolledBack $e) {
echo 'rolled back: ', $e->reason, "\n";
}
// The numbers did not change. This prints `waiting: 1, pushed in another transaction: 1`.
$stats = Queue::stats('mail');
echo 'waiting: ', $stats->pending(), ', pushed in another transaction: ';
echo $stats->nonTransactional(), "\n";
Output
rolled back: payment declined
waiting: 1, pushed in another transaction: 1
rolled back: payment declined
waiting: 1, pushed in another transaction: 1

Warn in a health check

<?nvs
// Shows a health check that warns when a queue has jobs pushed inside another database's transaction.
use Core\Db\Transaction;
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, []);
var $shop = Core\Db::connect('shop');
$shop->execute('create table orders (id integer primary key)', []);
// The `mail` queue gets two jobs inside a transaction of the `shop` database.
// The `exports` queue gets one job inside a transaction of the `jobs` database.
$shop->runTransaction(fn(Transaction $tx): void => {
Queue::push('jobs/send-receipt.nvs', { queue: 'mail' });
Queue::push('jobs/send-receipt.nvs', { queue: 'mail' });
});
$db->runTransaction(fn(Transaction $tx): void => {
Queue::push('jobs/export.nvs', { queue: 'exports' });
});
// This prints one line for each queue. The first line is `mail: 2 jobs to check`.
array<string> $queues = ['mail', 'exports'];
foreach ($queues as string $queue) {
uint $count = Queue::stats($queue)->nonTransactional();
if ($count > 0) {
echo $queue, ': ', $count, " jobs to check\n";
} else {
echo $queue, ": ok\n";
}
}
Output
mail: 2 jobs to check
exports: ok