Core\Queue::status
Core\Queue::status(Core\Queue\Id $job): Core\Queue::StateCore\Queue::status( Core\Queue\Id $job, ): Core\Queue::State
Tells you what has happened to a background job: whether it is waiting, running, done, failed or cancelled.
You pass the id that Core\Queue::push returned, and status returns one of the five Core\Queue::State cases. It reads the state from the database on every call, so the result is always the current state. A job that failed once is retried, and stays Pending until it runs again. It becomes Dead only after it used all its attempts.
Good to know: Succeeded means the job ran, but a job can run more than once. If delete removed the job, status throws an error for it.
The examples below read the state of a new job, show a cancelled job, and turn each state into a message for the user.
Parameters
$jobCore\Queue\IdTheCore\Queue\Idthatpushreturned. It identifies the job and its queue.
Return value
Returns Core\Queue::State. Pending while the job waits, also while its runAt time or its backoff has not passed. Claimed while a worker runs it, Succeeded after it ran, Dead after it used all its attempts, and Cancelled after cancel stopped it.
Errors
RuntimeErrorThe configuration has no[queue]block, or the database driver of the queue's connection cannot run statements yet. It is also thrown when neither table has the job, for example becausedeletedeleted it.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 = 0Check a new job
<?nvs// Shows how to read the state of a job that was just added.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` returns the id of the new job.var $job = Queue::push('jobs/resize-photo.nvs', { queue: 'images', arguments: ['photo' => 7] });
// `Queue::status` returns a `Core\Queue::State` case. No worker has started// the job yet, so the state is `Queue::State::Pending` and this prints `waiting`.echo Queue::status($job) == Queue::State::Pending ? 'waiting' : 'not waiting', "\n";waitingA cancelled job
<?nvs// Shows how the state of a job changes when it is cancelled.use Core\Queue;use Core\Queue\Id;
// 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, []);
// Two reminders are added. The user then cancels the first one.var $monday = Queue::push('jobs/send-reminder.nvs', { queue: 'mail', arguments: ['day' => 'monday'] });var $friday = Queue::push('jobs/send-reminder.nvs', { queue: 'mail', arguments: ['day' => 'friday'] });Queue::cancel($monday);
class Reminder{ // Returns the name of the state, so the program can print it. public static function state(Id $job): string { return match (Queue::status($job)) { Queue::State::Pending => 'Pending', Queue::State::Claimed => 'Claimed', Queue::State::Succeeded => 'Succeeded', Queue::State::Dead => 'Dead', Queue::State::Cancelled => 'Cancelled', }; }}
// This prints `monday: Cancelled` and `friday: Pending`.echo 'monday: ', Reminder::state($monday), "\n";echo 'friday: ', Reminder::state($friday), "\n";monday: Cancelledfriday: PendingShow an export to the user
<?nvs// Shows how a page tells a user what happened to the export they asked for.use Core\Queue;use Core\Queue\Id;
// 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, []);
class ExportPage{ // `match` needs one arm for each of the five states. // Each arm returns the text the user sees. public static function message(Id $export): string { return match (Queue::status($export)) { Queue::State::Pending => 'Your export is waiting to start.', Queue::State::Claimed => 'Your export is being built.', Queue::State::Succeeded => 'Your export is ready to download.', Queue::State::Dead => 'Your export failed. Please contact support.', Queue::State::Cancelled => 'You cancelled this export.', }; }}
// The user asks for two exports, and then cancels the second one.var $orders = Queue::push('jobs/export.nvs', { queue: 'exports', arguments: ['table' => 'orders'] });var $customers = Queue::push('jobs/export.nvs', { queue: 'exports', arguments: ['table' => 'customers'] });Queue::cancel($customers);
// This prints the message for each export.echo 'orders: ', ExportPage::message($orders), "\n";echo 'customers: ', ExportPage::message($customers), "\n";orders: Your export is waiting to start.customers: You cancelled this export.