[queue]
queue.connection
The database where this deployment stores its background jobs.
Novis keeps queued jobs in an ordinary database table. This setting names the connection for that table, which is one of the database blocks in the same file. There is no default. If the file has a [queue] block without this setting, the server does not start.
In plain words: use the application's own database. Then a job is added in the same transaction as the work that caused it. If that transaction is rolled back, the job is never created.
A program cannot change this setting. The server applies a new value while it runs. It first checks the new database, the same way it does when it starts. Workers on the old connection put their current job back and stop, and new workers take jobs from the new connection.
The example prints where this deployment stores its jobs. Then it tries to change that, and Core\Config::set returns false.
Only nvs.toml sets this key. A request may not change it.
A change in the file takes effect a few seconds after you save it. The server does not restart.
The examples below run with this configuration file.
# Queued jobs are stored in the `main` database. This server runs one worker.[queue]connection = "main"workers = 1
# The `main` database is the PostgreSQL server on this computer at port 15432.# `tls_ca_file` is the certificate that the server's certificate is checked against.[db.main]driver = "postgres"host = "127.0.0.1"port = 15432user = "novis"password = "Novis-Test-Pw1"database = "novis_test"tls_ca_file = "../../../../tests/db/ca.crt"Where the jobs are written
<?nvs// Shows how to read `[queue] connection`, the database block where queued jobs are stored.// There is no default. A deployment that uses the queue must set it.
echo 'jobs are stored in: ', Core\Config::get('queue.connection') ?? '(nothing written — this deployment queues no work)', "\n";echo 'workers here: ', Core\Config::get('queue.workers') ?? '(nothing written)', "\n";
// Use the application's own database. Then a job is added in the same transaction// as the work that caused it, and a rolled-back transaction creates no job.
// `Core\Config::set('queue.connection', ...)` does not compile,// because a program may not change `queue.connection`.
// This program keeps the name in a variable. The compiler does not check a variable, so `set` runs and returns `false`.// This prints `not allowed`.string $name = 'queue.connection';echo 'store them elsewhere: ', Core\Config::set($name, 'somewhere-else') ? 'accepted' : 'not allowed', "\n";
// The same is true for the value that is already set.echo 'write the same one back: ', Core\Config::set($name, Core\Config::get('queue.connection') ?? 'main') ? 'accepted' : 'not allowed', "\n";
// The server applies a new value in the file while it runs. Workers on the old// connection put their current job back, and new workers use the new connection.jobs are stored in: mainworkers here: 1store them elsewhere: not allowedwrite the same one back: not allowedqueue.workers
How many queued jobs this instance works on at the same time.
A deployment often runs the same program on several instances. This setting makes them different: some instances work on the queue, and some only add jobs to it. This way a web server can give its slow work to other machines.
In plain words: 0 means "I add the work, somebody else does it". To turn the queue off, leave out the [queue] block.
A worker with no job checks the queue once every second. A job that this instance adds starts at once. A job that another instance adds can start up to one second late.
A program cannot change this setting. The server applies a new value while it runs, and starts or stops workers. A worker that stops first puts its current job back in the queue.
The example prints how many workers this instance runs. Then it tries to change the count, and Core\Config::set returns false.
Only nvs.toml sets this key. A request may not change it.
A change in the file takes effect a few seconds after you save it. The server does not restart.
The examples below run with this configuration file.
# Queued jobs are stored in the `main` database. This server runs one worker.[queue]connection = "main"workers = 1
# The `main` database is the PostgreSQL server on this computer at port 15432.# `tls_ca_file` is the certificate that the server's certificate is checked against.[db.main]driver = "postgres"host = "127.0.0.1"port = 15432user = "novis"password = "Novis-Test-Pw1"database = "novis_test"tls_ca_file = "../../../../tests/db/ca.crt"How much of the queue this instance works
<?nvs// `[queue] workers` is how many jobs this server runs at the same time. Some// servers run jobs from the queue. Others only add jobs to it.
echo 'workers here: ', Core\Config::get('queue.workers') ?? '(no value)', "\n";echo 'claiming from: ', Core\Config::get('queue.connection') ?? '(nothing written)', "\n";
// With `workers = 0`, this server only adds jobs to the queue. Another server runs them.
// This example's `nvs.toml` sets `workers = 1`, so this prints `no`.echo 'enqueue-only: ', (Core\Config::get('queue.workers') ?? '4') == '0' ? 'yes' : 'no', "\n";
// `Core\Config::set('queue.workers', ...)` does not compile,// because a program may not change `queue.workers`.
// This program keeps the names in variables. The compiler does not check a variable, so each `set` call runs and returns `false`.string $name = 'queue.workers';
// A program cannot add workers, so the call returns `false`. More workers would// take CPU time from the other requests on the same server.echo 'hire eight more: ', Core\Config::set($name, '8') ? 'accepted' : 'not allowed', "\n";
// A program cannot set the workers to `0` either. This call also returns `false`.echo 'stop working the queue: ', Core\Config::set($name, '0') ? 'accepted' : 'not allowed', "\n";workers here: 1claiming from: mainenqueue-only: nohire eight more: not allowedstop working the queue: not allowedqueue.max_attempts
How many times a failing job is tried before Novis stops retrying it.
Some failures go away: a database was restarting, or a service was unreachable for a moment. Other failures never go away. A job that fails is tried again, with a longer wait before each new try. When it has been tried [queue] max_attempts times, Novis moves it to the dead-letter table, a table of failed jobs, and stops. The job is not deleted. A person can read it there, fix the problem and put it back in the queue.
The value must be at least 1, and there is no value that means "no limit". A server with 0 here does not start.
Only the person who runs the server sets this key. A program cannot raise it or lower it. A worker is the part of the server that takes jobs from the queue and runs them. A change to this key in nvs.toml applies to the next job that a worker takes. The server does not need a restart.
The example prints the number of attempts and tries to change it. Core\Config::set returns false.
Only nvs.toml sets this key. A request may not change it.
A change in the file takes effect a few seconds after you save it. The server does not restart.
The examples below run with this configuration file.
# Queued jobs are stored in the `main` database. This server runs one worker.[queue]connection = "main"workers = 1
# The `main` database is the PostgreSQL server on this computer at port 15432.# `tls_ca_file` is the certificate that the server's certificate is checked against.[db.main]driver = "postgres"host = "127.0.0.1"port = 15432user = "novis"password = "Novis-Test-Pw1"database = "novis_test"tls_ca_file = "../../../../tests/db/ca.crt"How many tries a job gets
<?nvs// `[queue] max_attempts` is how many times a failing job is tried.// The job is then moved to the dead-letter table. This is a table of failed jobs that a person can look at.
// This file does not set `max_attempts`, so this prints the default, `5`.echo 'attempts a job gets: ', Core\Config::get('queue.max_attempts') ?? '(no value)', "\n";echo 'queued in: ', Core\Config::get('queue.connection') ?? '(nothing written)', "\n";
// After the last attempt, the job is not deleted. It stays in the dead-letter// table until somebody reads it.
// `Core\Config::set('queue.max_attempts', ...)` does not compile,// because a program may not change `queue.max_attempts`.
// This program keeps the names in variables. The compiler does not check a variable, so each `set` call runs and returns `false`.string $name = 'queue.max_attempts';
// A program cannot raise this number, so the call returns `false`. Too many// retries slow down every other job in the same queue.echo 'try mine a thousand times: ', Core\Config::set($name, '1000') ? 'accepted' : 'not allowed', "\n";
// A program cannot lower it either, so this call also returns `false`.// A job that is not retried may never be done.echo 'or give mine one try: ', Core\Config::set($name, '1') ? 'accepted' : 'not allowed', "\n";attempts a job gets: 5queued in: maintry mine a thousand times: not allowedor give mine one try: not allowedqueue.visibility
How long a worker keeps a claimed job to itself.
Claiming a job takes a lease on it: for this long no other worker can see it. A worker that finishes is done and the lease never matters. A worker that dies mid-job — the host was restarted, the process was killed — leaves the lease to run out, and then the job is visible again and somebody else picks it up. That is the whole recovery story, and nothing has to notice the crash for it to work.
In plain words: how long to wait before assuming the worker that took this job is not coming back.
Write it comfortably longer than the work takes. A lease shorter than the job means a second worker starts the same job while the first is still running it, and a lease of nothing is refused when the deployment starts, because it makes every claimed job visible to everybody at once. The key is the operator's, and a change reaches the next job claimed rather than the next restart.
The example prints how long a claim is held here and is turned away shortening its own.
Only nvs.toml sets this key. A request may not change it.
A change in the file takes effect a few seconds after you save it. The server does not restart.
How long a claimed job is held
<?nvs// `[queue] visibility` is how long a job stays hidden after a worker takes it.// During that time, no other worker can see the job.
// This file sets neither key, so this prints the defaults, `5m` and `5`.echo 'a claim is held for: ', Core\Config::get('queue.visibility') ?? '(no value)', "\n";echo 'tries per job: ', Core\Config::get('queue.max_attempts') ?? '(no value)', "\n";
// If a worker stops in the middle of a job, the time runs out. The job is then// visible again, and another worker takes it.
// `Core\Config::set('queue.visibility', ...)` does not compile,// because a program may not change `queue.visibility`.
// This program keeps the names in variables. The compiler does not check a variable, so each `set` call runs and returns `false`.string $name = 'queue.visibility';
// A program cannot shorten this time, so the call returns `false`. A shorter time// could let a second worker start a job that is still running.echo 'hold my jobs for a second: ', Core\Config::set($name, '1s') ? 'accepted' : 'not allowed', "\n";
// A program cannot make it longer either. With an hour, a job whose worker// stopped would stay hidden for an hour.echo 'or hold them for an hour: ', Core\Config::set($name, '1h') ? 'accepted' : 'not allowed', "\n";a claim is held for: 5mtries per job: 5hold my jobs for a second: not allowedor hold them for an hour: not allowed