Core\Queue\Id::tryParse
Core\Queue\Id::tryParse(string $s): ?Core\Queue\IdCore\Queue\Id::tryParse( string $s, ): ?Core\Queue\Id
Reads a string as a job id, like Core\Queue\Id::parse, and returns null when the string is not a job id. It never throws an error, so you can pass it a value from a URL or a form directly.
The string must have the form that toString writes. A string with the wrong parts, a character that is not allowed, or too many characters gives null.
Good to know: tryParse checks the form only. A changed or guessed string in the correct form still gives an id, but that id finds no job. Core\Queue::cancel returns false for it, and Core\Queue::status throws an error. Your program decides who may use a job: save the string next to the user who started the job, and look it up for the signed-in user first.
The examples below read a valid id, show which strings give null, and check an id from a query parameter before the program cancels the job.
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, or null. A changed or guessed string in the correct form gives an id that finds no job.
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 = 0Read an id or null
<?nvs// Shows how to read a job id from a string when the string may not be one.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, []);
// `toString` writes the id of a new job as a string.var $job = Queue::push('jobs/send-invoice.nvs', { queue: 'mail', arguments: ['invoice' => 1042] });string $text = $job->toString();
// The string came from `toString`, so `Id::tryParse` returns an id.// This prints a job id.?Id $id = Id::tryParse($text);echo $id == null ? 'not a job id' : 'a job id', "\n";
// This string is not in the form that `toString` writes, so the result is `null`.// This prints not a job id.?Id $other = Id::tryParse('job 1042');echo $other == null ? 'not a job id' : 'a job id', "\n";a job idnot a job idStrings that give null
<?nvs// Shows which strings give a job id, and that a correct form does not prove the job exists.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 $job = Queue::push('jobs/send-invoice.nvs', { queue: 'mail' });
class Check{ // Prints yes when `$text` gives an id, and no when the result is `null`. public static function show(string $label, string $text): void { echo Id::tryParse($text) == null ? 'no ' : 'yes', ' ', $label, "\n"; }}
// Only the first two strings have the correct form. This prints yes, yes, no, no and no.Check::show('written by toString', $job->toString());Check::show('same form, other token', '1.AAAAAAAAAAAAAAAAAAAAAA.bWFpbA');Check::show('only a number', '1');Check::show('has a space', '1.abc def.bWFpbA');Check::show('empty', '');
// The second string gives an id, but no job has that token.// `Queue::cancel` finds no job, so it returns `false`. This prints no job.?Id $guess = Id::tryParse('1.AAAAAAAAAAAAAAAAAAAAAA.bWFpbA');if ($guess != null) { echo Queue::cancel($guess) ? 'cancelled' : 'no job', "\n";}yes written by toStringyes same form, other tokenno only a numberno has a spaceno emptyno jobCheck the id in a query
<?nvs// Shows how to check a job id from a query parameter before the program 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, []);
class Exports{ // Returns the response for a request such as /exports/cancel?job=<id>. // `$job` is `null` when the query has no `job` parameter. public static function cancel(?tainted string $job): string { // `?? ''` replaces a missing parameter with an empty string. // `Id::tryParse` returns `null` for it, and for any string in the wrong form. ?Id $id = Id::tryParse($job ?? ''); if ($id == null) { return '400 the job id is missing or not valid'; } return Queue::cancel($id) ? '200 cancelled' : '409 the job is no longer waiting'; }}
var $export = Queue::push('jobs/export-orders.nvs', { queue: 'exports' });string $text = $export->toString();
// This prints 400 the job id is missing or not valid, two times.echo Exports::cancel(null), "\n";echo Exports::cancel('../../etc/passwd'), "\n";
// The first call cancels the job and prints 200 cancelled.// The job is not waiting anymore, so the second call prints 409 the job is no longer waiting.echo Exports::cancel($text), "\n";echo Exports::cancel($text), "\n";400 the job id is missing or not valid400 the job id is missing or not valid200 cancelled409 the job is no longer waiting