Core\Queue::push
Core\Queue::push(string $script, {…}): Core\Queue\IdCore\Queue::push( string $script, {…}, ): Core\Queue\Id
Adds a job that a worker runs later, so a request can answer quickly and leave slow work for the background.
A job is a Novis file and the data it receives. push saves it as a row in the database that [queue] connection names, and returns an id for the job. A worker takes the row later and runs the file. When you call push inside a transaction on that database, the job is saved only if the transaction commits. So an order and the email about that order are saved together, or not at all.
Good to know: a job can run more than once, for example after a worker stops in the middle. Write jobs that are safe to run twice.
The job reads its data with Core\Script::arguments(). Every string in that data is tainted, which means it came from outside the program. Check it before you use it in SQL or as a file path. A secret value cannot be pushed.
The examples below add one job, stop a duplicate with a key, and save a job together with an order.
Parameters
$scriptstringThe path of the file a worker runs, written the wayspawn scriptwrites one. A relative path starts at the folder of the file that callspush. It is a file, not a class or a function, so the job has no captured variables.
Options
argumentsmixedoptionalThe data the job receives. It is copied into the row and decoded into the types the job declares. It is never passed by reference, because the worker usually runs in another process.queuestringoptionalThe name of the queue the job goes in. Each worker takes jobs from the queues it is configured for, so you can keep slow work and fast work apart.runAtCore\Time\InstantoptionalThe earliest time a worker may start the job. The default is now.maxAttemptsuintoptionalHow many times the job may run before it moves to the dead-letter table (the table of jobs that failed too often). The default is[queue] max_attempts. The number is always finite, so a job cannot retry forever.backoffCore\Time\DurationoptionalThe base wait between two attempts. The wait grows after each failed attempt, and the worker adds a small random amount. The default is one second.keystringoptionalA key that stops duplicate jobs. While a job with this key is still pending, a secondpushwith the same key adds nothing. It returns the id of the pending job.tagstringoptionalA group name for the job. Many jobs can have the same tag. Onlypurgereads it, to delete one group of jobs. You cannot add a tag after thepush.grantsarray<string>optionalThe capabilities the job may use, written the waynvs.tomlgrants them. The list may only contain capabilities this request has. The default is what this request has.[]gives the job no capabilities.memorystringoptionalThe memory limit for the job, written like[limits] memory.[limits.hard]sets the highest value allowed. The default is the limit of this request.cpuTimestringoptionalThe CPU time limit for the job, written like[limits] cpu_time. It follows the same rules asmemory.wallTimestringoptionalThe real time limit for the job, written like[limits] wall_time. It follows the same rules asmemory.maxOutputstringoptionalThe limit on the output the job may print, written like[limits] max_output. It follows the same rules asmemory.
Return value
Returns Core\Queue\Id. A Core\Queue\Id for the new job. You pass it to status, cancel and delete. When key finds a pending job, it is the id of that job.
Errors
RuntimeErrorThe configuration has no[queue]block, or the database driver of the queue's connection cannot run statements yet. It is also thrown whengrantscontains a capability this request does not have.LogicErrormaxAttemptsis0, orbackoffis negative. It is also thrown whengrantscontains a name that is not a capability, or a limit is one this request may not set.IOErrorThe connection to the database failed, or the database rejected the insert. The most common cause is thatnvs queue migratehas not created the table 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 every job stays `Pending`.[queue]connection = "jobs"workers = 0Run a job later
<?nvs// Shows how to add a job that a worker runs later, outside the current request.use Core\Db\Row;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;$db->execute($jobs, []);
// `Queue::push` saves the job and returns at once. A worker runs the file// `jobs/send-welcome.nvs` in the folder of this file later, with this data.Queue::push('jobs/send-welcome.nvs', { queue: 'mail', arguments: ['user' => 42] });
// The job is a row in the `nvs_jobs` table, with the full path of the file.// This prints the last folder and name of that file, and the data the worker receives.foreach ($db->query('select script, args from nvs_jobs', []) as Row $row) { tainted string $script = $row->string('script') ?? ''; echo Core\Path::fileName(Core\Path::parent($script)), '/', Core\Path::fileName($script), "\n"; echo $row->string('args') ?? '', "\n";}jobs/send-welcome.nvs{"user":42}One job per key
<?nvs// Shows how the `key` option stops the same job from being added twice.use Core\Db\Row;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;$db->execute($jobs, []);
// Two requests ask for the same monthly report. Both use the same key.// The first call adds the job. The second call adds nothing.Queue::push('jobs/build-report.nvs', { queue: 'reports', key: 'report-2026-09' });Queue::push('jobs/build-report.nvs', { queue: 'reports', key: 'report-2026-09' });
// A different key adds a second job.Queue::push('jobs/build-report.nvs', { queue: 'reports', key: 'report-2026-10' });
// This prints 2.foreach ($db->query('select count(*) as n from nvs_jobs', []) as Row $row) { echo $row->int('n') ?? 0, "\n";}2Save an order and its email together
<?nvs// Shows how a job is saved together with the order it belongs to, in one transaction.use Core\Db\RolledBack;use Core\Db\Row;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;$db->execute($jobs, []);$db->execute('create table orders (id integer primary key, total integer not null)', []);
// Order 1 and its receipt email are saved in one transaction.// The transaction commits, so both are saved.$db->runTransaction(fn(Transaction $tx): void => { $tx->execute('insert into orders (id, total) values (?, ?)', [1, 250]); Queue::push('jobs/send-receipt.nvs', { queue: 'mail', arguments: ['order' => 1] });});
// The payment for order 2 fails, so the transaction rolls back.// The order is not saved, and its receipt email is not saved either.try { $db->runTransaction(fn(Transaction $tx): void => { $tx->execute('insert into orders (id, total) values (?, ?)', [2, 90]); Queue::push('jobs/send-receipt.nvs', { queue: 'mail', arguments: ['order' => 2] }); $tx->rollBack('payment declined'); });} catch (RolledBack $e) { echo 'order 2: ', $e->reason, "\n";}
// This prints 1 and 1: one order, and one email for it.foreach ($db->query('select count(*) as n from orders', []) as Row $row) { echo 'orders: ', $row->int('n') ?? 0, "\n";}foreach ($db->query('select count(*) as n from nvs_jobs', []) as Row $row) { echo 'emails: ', $row->int('n') ?? 0, "\n";}order 2: payment declinedorders: 1emails: 1