Core\Queue\Id::format
$id->format(): stringReturns the string that Core\Queue\Id::parse reads. format returns the same string as toString. The string has three parts joined by dots: the job's number, a random token and the name of the queue. The name is encoded, so exports is written ZXhwb3J0cw. The string contains only letters, digits, ., _ and -.
Core\Queue\Id implements the Parses interface, and format is the method that interface needs. Core\Router::url calls it to write a job id into a link, in the path or in the query string. Core\Uri::buildQuery calls it too. A URL never escapes the string, so the link contains it exactly.
The string works like a password for the job. Anybody who has it can ask for the state of the job. Save the string beside the user who started the job, and check that user in a later request.
The examples below read the string back with parse, write an id into a link to a route, and write one into a query string.
Return value
Returns string. A string of letters, digits, ., - and _. Keep it secret like a password.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Uuid::format and Core\Queue\Id::format return the text that parse reads. It is the same text as toString. |
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 = 0Format gives the string parse reads
<?nvs// Shows that `format` returns the string that `Core\Queue\Id::parse` reads.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, []);
var $job = Queue::push('jobs/export.nvs', { queue: 'exports' });
// The string has a random token in it. This prints it as `1.<token>.ZXhwb3J0cw`.string $text = $job->format();array<string> $parts = Core\Str::split($text, '.');echo $parts[0], '.<token>.', $parts[2], "\n";
// `format` and `toString` return the same string. This prints `same`.echo $text == $job->toString() ? 'same' : 'different', "\n";
// `Id::parse` reads the string back to the same job. This prints `Pending`.var $again = Id::parse($text);echo Queue::status($again) == Queue::State::Pending ? 'Pending' : 'not found', "\n";1.<token>.ZXhwb3J0cwsamePendingPut the id in a link
<?nvs// Shows how `Core\Router::url` writes a job id into a link.use Core\Queue\Id;
class Exports{ #[Core\Route(path: '/exports/{job}', method: Core\Http\Method::Get)] #[Core\Access(allow: Core\Audience::Public)] public static function show(Id $job): void { echo 'Export ', $job->format(), "\n"; }}
// This is the id of a job in the queue `exports`.// A real program gets it from `Core\Queue::push` and saves it.var $job = Id::parse('42.q3vX9bT0LmN2pR8sK4dYfw.ZXhwb3J0cw');
// `Core\Router::url` calls `format` to write the id into the path.// This prints `/exports/42.q3vX9bT0LmN2pR8sK4dYfw.ZXhwb3J0cw`.string $link = Core\Router::url(Exports::show, ['job' => $job]);echo $link, "\n";
// The string has no character that a URL must escape. This prints `unchanged`.echo $link == '/exports/' . $job->format() ? 'unchanged' : 'changed', "\n";/exports/42.q3vX9bT0LmN2pR8sK4dYfw.ZXhwb3J0cwunchangedBuild a query string
<?nvs// Shows how `Core\Uri::buildQuery` writes a job id into a query string.use Core\Queue\Id;
// An earlier request saved this id.var $job = Id::parse('42.q3vX9bT0LmN2pR8sK4dYfw.ZXhwb3J0cw');
// `buildQuery` calls `format` for the id. Other values are written as they are.// This prints `job=42.q3vX9bT0LmN2pR8sK4dYfw.ZXhwb3J0cw&tab=status`.string $query = Core\Uri::buildQuery(['job' => $job, 'tab' => 'status']);echo $query, "\n";
// A later request reads the id back with `Id::parse`. This prints `same`.string $sent = Core\Str::split(Core\Str::split($query, '&')[0], '=')[1];echo Id::parse($sent)->format() == $job->format() ? 'same' : 'different', "\n";job=42.q3vX9bT0LmN2pR8sK4dYfw.ZXhwb3J0cw&tab=statussame