On 31 August, for 30 minutes after release 34, a store I work on kept sending release 33's security policy. The pipeline was green and nothing logged an error. On the server, 3 queue consumers were still running from older release directories, one of them for 6 days.

30 minutes of the previous release

The first check was the full page cache. A request with a unique query string, which the cache can't answer, still came back with the old policy. The second was OPcache: PHP CLI, where OPcache is off, read the same stale configuration. That left the config cache, and a manual bin/magento cache:flush fixed it.

Why had a fresh cache filled with old data? One consumer had been running since 25 August, 13:29, from releases/32, and 2 more ran from releases/33, while current pointed at releases/34. All 3 shared the Redis cache with the live site. The evidence doesn't show which process wrote the old entry.

What an old consumer does after a deploy

A test install with 2 release directories on one database and one cache shows what such a process can do. The consumer starts from the old release, then the new release flushes the cache, as a deploy does. No restart signal is sent.

The old consumer handled its next message with the old release's code and the old store configuration, which it had kept in memory since it started. It didn't write that configuration back. The new release kept reading its own values.

Configuration that Magento merges from XML files on first use is different. The CSP whitelist, the hosts Magento lists in its Content-Security-Policy header, is one example. In the test, a handler written to read the whitelist ran in the old consumer right after the flush. The consumer built the whitelist from the old release's csp_whitelist.xml and saved it to the cache. The storefront, served by the new release, then sent the old release's whitelist until the next cache:flush. When the storefront was requested first, the old consumer read the new whitelist from the cache, and nothing went wrong.

It takes 2 things: a process still running the old release, and a cache entry it needs before the new release does.

Why a Magento consumer lives for days

By default, cron starts a consumer only when its queue has a message. On the test install, with 15 consumers configured and 1 message waiting, a single cron pass started exactly 1 process. But once started, a consumer stays. Cron launches it with --max-messages=10000, and it waits for the next message. On a quiet queue, 10,000 messages can take days.

Magento has a restart signal: bin/magento queue:consumers:restart. setup:upgrade sends the same signal, and a deploy that skips setup:upgrade has to send it itself. But look at where the consumer checks it. In CallbackInvoker::invoke(), the loop dequeues first and compares the restart version only after a message has arrived. An idle consumer never gets that far.

On the test install, an idle consumer got the restart signal and was still running 60 seconds later. Then one message went into its queue, and the process exited within a second, leaving the message for the next consumer.

3 ways to make it exit

The first is configuration for every consumer. With consumers_wait_for_messages set to 0 in env.php, a consumer exits as soon as its queue is empty: in under 1 second on the test install, exit code 0. Cron starts a fresh one from the current release when the next message is waiting. The price is a Magento bootstrap for every batch of messages, and up to a minute of waiting for the cron pass that starts it. The config below also lowers max_messages to 1,000, so a busy consumer is replaced sooner too.

'cron_consumers_runner' => [ 'cron_run' => true, 'max_messages' => 1000, ], 'queue' => [ // exit when the queue is empty; cron starts a fresh one from the new release 'consumers_wait_for_messages' => 0, ], 

The second is configuration per consumer: maxIdleTime in queue_consumer.xml. With 5 seconds set, an idle consumer exited on its own, exit code 0.

The third is the deploy itself: stop this site's consumers with pkill -f after the symlink switch and before the cache is cleared. Scope its pattern to the site's own release path. On a shared server, another installation's consumers must survive, and in the test they did.

One warning: pkill sends SIGTERM, and the consumer code has no handler for it. A consumer killed in the middle of a message exited with code 143. With the MySQL queue the message stayed "in progress": a fresh consumer didn't pick it up, and Magento retries it only after retry_inprogress_after, 1,440 minutes by default.

# queue:consumers:restart is read only when a message arrives: idle ones miss it # $DEPLOY_PATH: this site's root, the parent of releases/ and current pkill -f "$DEPLOY_PATH/releases/[0-9]+/bin/magento queue:consumers:start" || true bin/magento cache:flush 

Laravel: the same risk, a clearer habit

A Laravel worker has the same problem. queue:work boots the app once and keeps the code it has loaded. With the job class rewritten on disk, a running worker still handled the next job with the old version.

The difference is the signal. php artisan queue:restart is a normal deploy step, and by default the worker checks it on every loop, with or without jobs. An idle worker with the default 3-second sleep exited within 3 seconds of the signal. A worker started after the signal handled the next job with the new code. Starting it is the process manager's job, and the Supervisor config below sets autorestart=true for that. --max-time=3600 in the same config is the backstop: after an hour the worker exits as soon as its current job is done.

One condition: the signal is a cache entry. A worker on a different cache store never sees it. In the test, a restart sent through another store left the worker running.

# checked on every loop, even with an empty queue php artisan queue:restart 
[program:worker] command=php /var/www/app/current/artisan queue:work --max-time=3600 autorestart=true ; on stop: SIGTERM, the running job finishes, SIGKILL after stopwaitsecs stopwaitsecs=3600 

The rule

A deploy is done when nothing runs the old release any more, and that comes later than the symlink switch. In Magento, make idle consumers exit, or stop them yourself before the cache clear. In Laravel, keep queue:restart in the deploy, on the cache store the workers use, and let the process manager start the new workers.

Next: cron runs when nobody is watching.

What does your deploy restart besides PHP-FPM?

Part 10 of 15 of the Magento 2 vs Laravel series. Originally published on LinkedIn.