Skip to content

Core\Queue::cancel

Stops a background job before it starts.

You pass the id that Core\Queue::push returned. If the job is still waiting, cancel sets its state to Cancelled and returns true. No worker will run it. If a worker already started the job, or the job already ran, cancel returns false and changes nothing. A job that is running is never stopped.

The row of a cancelled job stays in the database, so status returns Cancelled for it. delete removes the row.

The examples below cancel a waiting job, show what a second cancel returns, and cancel the reminders of a user who unsubscribed.

Parameters

  • $jobCore\Queue\IdThe Core\Queue\Id that push returned. It identifies the job and its queue.

Return value

Returns bool. true if this call cancelled the job. false if the job was no longer waiting: a worker took it, it already ran, or an earlier cancel removed it. After true, status returns Cancelled.

Errors

  • RuntimeErrorThe configuration has no [queue] block, or the database driver of the queue's connection cannot run statements yet.
  • IOErrorThe connection to the database failed, or the database rejected the update. The most common cause is that nvs queue migrate has not created the table yet.

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

Cancel a waiting job

<?nvs
// Shows how to cancel a job that no worker has started yet.
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 $job = Queue::push('jobs/resize-photo.nvs', { queue: 'images', arguments: ['photo' => 17] });
// The job is still waiting, so `cancel` returns `true`. This prints `cancelled: yes`.
echo 'cancelled: ', Queue::cancel($job) ? 'yes' : 'no', "\n";
// The row is still in the database. This prints `state is Cancelled: yes`.
echo 'state is Cancelled: ', Queue::status($job) == Queue::State::Cancelled ? 'yes' : 'no', "\n";
Output
cancelled: yes
state is Cancelled: yes

Cancel twice

<?nvs
// Shows what `cancel` returns when the job is already cancelled.
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 $job = Queue::push('jobs/send-invoice.nvs', { queue: 'mail', arguments: ['order' => 1042] });
// A user clicks the Cancel button twice. Only the first click cancels the job.
// This prints `first click: true` and `second click: false`.
echo 'first click: ', Queue::cancel($job) ? 'true' : 'false', "\n";
echo 'second click: ', Queue::cancel($job) ? 'true' : 'false', "\n";
Output
first click: true
second click: false

Stop the reminders of a user

<?nvs
// Shows how to cancel the reminder emails of a user who unsubscribed.
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, []);
// When the user signed up, the program added three reminders and kept their ids.
array<string> $days = ['day 1', 'day 3', 'day 7'];
array<Id> $reminders = [];
foreach ($days as string $day) {
$reminders[] = Queue::push('jobs/send-reminder.nvs', { queue: 'mail', arguments: ['day' => $day] });
}
// Earlier, the user turned off the first reminder, so it is already cancelled.
Queue::cancel($reminders[0]);
// Now the user unsubscribes. The program cancels every reminder and counts
// the ones that were still waiting. This prints `2 reminders stopped`.
int $stopped = 0;
foreach ($reminders as Id $job) {
if (Queue::cancel($job)) {
$stopped = $stopped + 1;
}
}
echo $stopped, " reminders stopped\n";
Output
2 reminders stopped