Core\Queue\Id::parse
Core\Queue\Id::parse(string $s): Core\Queue\IdCore\Queue\Id::parse( string $s, ): Core\Queue\Id
Reads the string that $id->toString() gave and returns a Core\Queue\Id for the same job. A later request can then call Core\Queue::status or Core\Queue::cancel for a job that an earlier request added.
The string may be tainted, so a value from the URL can go in directly. A string that is not in the form toString writes throws a RuntimeError. The message does not quote the string. When the string may not be a job id, Core\Queue\Id::tryParse returns null.
The string has a random token. A string with a changed row number or token still parses, but finds no job. status then throws a RuntimeError, and cancel returns false.
Any user who has the string can use the id. Save the string together with the user who started the job. Look up both before you call status or cancel.
The examples below read an id back, show a wrong string and a changed one, and check the user in a request.
Parameters
$sstringThe string to read, such as a value from the URL. It may betainted.
Return value
Returns Core\Queue\Id. The Core\Queue\Id that the string gives. Any user who has the string can use the id. Store the string with the user who started the job. Check that user before you call status or cancel.
Errors
RuntimeError$sis not in the form thattoStringwrites. The message does not quote$s.
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 = 0Ask about a job a later request names
<?nvs// Shows how a program reads a job id back from its string, then checks and cancels the job.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;$db->execute($jobs, []);var $dead = <<<'SQL'create table nvs_dead_jobs (id bigint not null primary key, 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)SQL;$db->execute($dead, []);
// `toString` gives the string that a program sends to the client or saves.var $pushed = Queue::push('jobs/build-report.nvs', { queue: 'reports' });string $text = $pushed->toString();
// A later request has only the string. `Id::parse` reads it and returns an Id for the same job.Id $id = Id::parse($text);
// No worker runs here, so the job is still waiting. This prints `waiting: yes`.echo 'waiting: ', Queue::status($id) == Queue::State::Pending ? 'yes' : 'no', "\n";
// `Queue::cancel` with the new Id cancels the job. This prints `cancelled: yes`.echo 'cancelled: ', Queue::cancel($id) ? 'yes' : 'no', "\n";waiting: yescancelled: yesA wrong or changed string
<?nvs// Shows what happens when the string is not a job id, and when somebody changed it.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;$db->execute($jobs, []);var $dead = <<<'SQL'create table nvs_dead_jobs (id bigint not null primary key, 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)SQL;$db->execute($dead, []);
// This string is not in the form that `toString` writes, so `parse` throws a `RuntimeError`.// The message does not repeat the string.try { Id::parse('report/42');} catch (RuntimeError $e) { echo 'error: ', $e->message, "\n";}
// The string of a job id is the row number, a random token and the queue, with dots between.// This code changes the row number from 1 to 2. The string is still in the right form.var $pushed = Queue::push('jobs/build-report.nvs', { queue: 'reports' });array<string> $parts = Core\Str::split($pushed->toString(), '.');Id $changed = Id::parse('2.' . $parts[1] . '.' . $parts[2]);
// No job has this row number and this token. `Queue::cancel` returns `false`,// and `Queue::status` throws a `RuntimeError`.echo 'cancelled: ', Queue::cancel($changed) ? 'yes' : 'no', "\n";try { Queue::status($changed);} catch (RuntimeError $e) { echo 'status: ', $e->message, "\n";}
// The job itself is not changed. This prints `waiting: yes`.echo 'waiting: ', Queue::status($pushed) == Queue::State::Pending ? 'yes' : 'no', "\n";error: Core\Queue\Id::parse(): the string is not a job id, because it has a character that no job id has. A job id is the string that `toString` writescancelled: nostatus: Core\Queue::status: no job 2 is in the `reports` queue on `[db.jobs]`. The job was deleted, or the id is not one that `push` returned on this databasewaiting: yesCheck who started the job
<?nvs// Shows a request that reads the status of an export job, but only for the user who started it.use Core\Db\Connection;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;$db->execute($jobs, []);var $dead = <<<'SQL'create table nvs_dead_jobs (id bigint not null primary key, 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)SQL;$db->execute($dead, []);
// Anybody who has the string of a job id can use it. So the app saves the string// together with the user who started the job.$db->execute('create table exports (job text not null, user_id text not null)', []);array<string> $owners = ['alice', 'bob'];foreach ($owners as string $owner) { var $id = Queue::push('jobs/export.nvs', { queue: 'exports' }); $db->execute('insert into exports (job, user_id) values (?, ?)', [$id->toString(), $owner]);}
class Exports{ // Returns the status of the job, or `not found` when this user did not start it. public static function status(Connection $db, tainted string $job, tainted string $user): string { var $rows = $db->query('select job from exports where job = ? and user_id = ?', [$job, $user]); foreach ($rows as Core\Db\Row $row) { // The row exists, so this user started the job. Only now does the code read the Id. Id $id = Id::parse($job); return Queue::status($id) == Queue::State::Pending ? 'pending' : 'not pending'; } return 'not found'; }}
// The `x-user` header replaces the signed-in user of a real app. Here it is `bob`.tainted string $user = Core\Request::header('x-user') ?? '';
// A real client sends the string of a job id in the URL. The token in it is random,// so this example reads the two saved strings instead.array<tainted string> $saved = [];foreach ($db->query('select job from exports order by user_id', []) as Core\Db\Row $row) { $saved[] = $row->string('job') ?? '';}
// Bob asks about the job of Alice, then about his own job.// This prints `alice's job: not found`, then `bob's job: pending`.echo "alice's job: ", Exports::status($db, $saved[0], $user), "\n";echo "bob's job: ", Exports::status($db, $saved[1], $user), "\n";alice's job: not foundbob's job: pending