Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to main content
Open console

Workers

Processes kept running beside your site, such as queue consumers, schedulers and listeners, in your application's own image.

8 min read

A worker is your application started on a different command and kept running beside your site, for as long as the environment is up. It is for a queue consumer, a scheduler, a listener on a stream: anything that is neither a request nor a scheduled command.

Declaring one

Nothing processes your jobs until vallic.yaml declares a worker that does:

workers:
  - name: queue
    command: 'php bin/console messenger:consume async --time-limit=3600'
    replicas: 2
Key Value
name Lowercase letters, digits and hyphens, starting with a letter, and different for each worker. It names the containers: worker-queue-1, worker-queue-2.
command One line, run under sh in the release at /var/www/html/current. $VALLIC_PRIVATE_DIR and your other variables expand in it. It can be a framework command, a script, a binary your build made or a shell loop.
replicas How many copies run, from 1 to 16. Leave it out for one.

The list is read at the commit being deployed and applied by that deploy:

Change in the file On deploy
A worker added It starts
A command changed The worker starts again on the new one
A worker removed It is stopped
replicas raised Copies are added beside the ones running
replicas lowered The highest-numbered copies stop

What it runs in

A worker has no port and no hostname, and nothing routes to it.

Your application's image, at its version. On a PHP project that is the PHP-FPM image, at the PHP your runtime names; on Node.js, Bun, Python, Ruby, Go or Rust, that application's image. The tools your build put in the release are on PATH. What the running image does not carry is not there: a Go or Rust worker runs the binary you built, not go run or cargo run.

The site's variables and settings. A worker runs the same release as the site. Your team's variables and the platform's reach a worker as they reach the site. So does every setting you give the application: on PHP, the memory limit and the rest of what you set on php. Applying changed variables restarts the workers with the site. An APM agent your project uses (Monitoring) traces the workers too.

The site's files. The release is read-only. A worker writes where the site does: under /mnt/files, and its own log files in /var/log/app. What it prints is collected with the rest of your logs.

Kept running

If it exits, it is started again, whether it finished, failed or was killed. A time limit is therefore safe: messenger:consume --time-limit=3600 exits after an hour and starts again fresh.

A command that stops when the work runs out needs something to wait on. Drush's queue:run returns as soon as its queue is empty, so on its own it is started over and over. Loop it, with a pause:

workers:
  - name: imports
    command: 'while true; do drush queue:run imports --time-limit=300; sleep 30; done'

A deploy does not wait on a worker. It checks the site, at its health path. A worker that cannot start says why in its logs. One that was not running when its machine last reported is named under that machine, with its state, on the environment's overview.

Deploys restart them

Every deploy restarts every worker on the new release once it is live. Nothing like queue:restart or horizon:terminate is needed.

  • On the machine that deploys first, workers restart before the deploy steps run.
  • Every other machine, a worker machine included, takes the release after that first machine has run the steps.
  • A deploy that rolls back puts the workers back on the release it returns to.

A worker has ten seconds to stop. A deploy, and applying variables, each stop it and start it again. Whatever it is doing when the ten seconds run out is cut off, so make every job safe to run again from the start.

How many, and where

One declared worker per machine that can run one. An environment has room for as many different workers as it has worker machines. Without any, it has room for as many as it has web servers, and never fewer than one. replicas does not count against it. A file that declares more refuses the deploy, and says so.

Staging, development and preview environments have room for one. They run on one machine, whatever production runs, so they refuse a second worker that production has room for. Fold related work into one worker instead: one consumer can read several queues, as in queue:work --queue=high,default, messenger:consume async high or Celery's -Q high,default.

Every machine that runs your workers runs all of them, replicas copies on each:

The environment has Your workers run on
One machine that machine, beside the site
A worker machine the worker machine, and not the web machine
Two or more web servers and no worker machine every web server
Two or more worker machines every worker machine

The last two are the dedicated shape, where replicas: 2 is two copies on each of those machines. Consumers share a queue however many of them there are.

A scheduler does not: two copies run every task twice. Keep a schedule in cron, which runs on one machine however many the environment has: the first worker machine where there is one, otherwise the machine holding the site's files. Or run it as a worker only where one machine runs your workers.

Different work on each machine. Each worker is told which machine it is on in VALLIC_MACHINE: web-1, worker-1, worker-2, the names the console shows. A worker that reads it can take one queue on worker-1 and another on worker-2, or run a job on one machine only:

workers:
  - name: jobs
    command: './bin/jobs --machine "$VALLIC_MACHINE"'

Druker is this pattern for Drupal: one binary on every machine, and the jobs each one runs mapped to its name. The container's own hostname is its id, so read VALLIC_MACHINE instead.

A machine for them

Without a worker machine, workers share a machine with the site, and a busy worker slows the site. A worker machine takes every worker off the web machine. You choose its size, and it is billed as a machine like any other. See Shapes.

Environment Worker machines
Flexible, in production The environment's Resources tab offers Workers among what can move to a machine of its own, once a deploy has recorded some. It can bring them back to the web machine. See Upgrades.
Dedicated, in production The same tab sets how many worker machines there are, as it does web servers: up to eight, each the size of the others.
A shared plan or a bare-metal server No second machine to give them.

Giving them more room. A worker machine is resized like any other, and an upscale for seven days can reach it. Autoscale moves only the machines that serve a page. Each copy of a worker takes the memory a copy of your application takes: on PHP, up to its memory limit. The configurator's figures cannot know what your code holds, so leave room for every copy when you choose a size.

Where the jobs wait

A worker takes its work from a queue: a service of its own, or the database you already run. Name the one you use under services in vallic.yaml:

  • The database needs nothing added: Laravel's database driver, Symfony Messenger's Doctrine transport, Rails' Solid Queue.
  • Valkey or Redis keeps nothing on disk by default, so a restart empties the queue. Set it to store for a queue: see Keeping its data. Keep the queue on a database number of its own, apart from the cache: clearing a Redis cache empties its whole database, queue and all.
  • RabbitMQ is a broker, with its queues and messages kept on disk.

Framework by framework

Project Worker command More
Laravel php artisan queue:work, or php artisan horizon The queue
Symfony php bin/console messenger:consume async --time-limit=3600 Messenger
Drupal drush queue:run <queue>, in a loop as above —
Django and Python python -m celery -A mysite worker, RQ, Dramatiq Django, Python
Rails bin/jobs for Solid Queue, bundle exec sidekiq Rails
Node.js, Go, Rust your own: node worker.js, bin/worker Node.js
  • Laravel: use queue:work, not queue:listen. Running the scheduler as a worker, php artisan schedule:work, takes the place of the platform's schedule:run cron, which is then not added. It is a scheduler, so two copies run every task twice. The PHP image has pcntl, which queue:work needs to time a job out and Horizon needs to run.
  • Python: run it as python -m …, so the release's own Python and packages run it.
  • Go and Rust: the worker is your binary; nothing is compiled where it runs.

Getting into one

Each copy has a container of its own, worker-<name>-<n>, on the machine that runs it. Open it from the CLI:

vallic ssh production --container worker-queue-1 -- php bin/console messenger:failed:show

Or use ssh, as @worker-queue-1: the Plain SSH section of the environment's Shell access card has the lines for yours. See Shell access.

When a deploy refuses

Message What to do
"workers" should be a list of processes to keep running. Start each worker with - name:
Each entry under "workers" needs a name and a command. Add whichever is missing
Worker name "Queue" should be lowercase letters, digits and hyphens, starting with a letter. Rename it
There is more than one worker called "queue". Give each its own name
"workers.command" should be a single line of text. Put it on one line, or in a script your release ships
Worker "queue" asks for 20 copies; between 1 and 16. Ask for 16 or fewer
declares 2 worker(s) and this environment has room for 1 Add a worker machine or a web server, or fold the work into one worker

Next