Skip to content

Scope magento:cron:stop kill to the current deploy path - #4277

Open
sprankhub wants to merge 1 commit into
deployphp:masterfrom
sprankhub:fix-magento2-scope-cron-stop
Open

sprankhub wants to merge 1 commit into
deployphp:masterfrom
sprankhub:fix-magento2-scope-cron-stop

Conversation

@sprankhub

Copy link
Copy Markdown
Contributor

Problem

magento:cron:stop kills matching processes without looking at where they live:

run('pgrep -U "$(id -u)" -f "bin/magento +(cron:run|queue:consumers:start)" | xargs -r kill');

pgrep -f matches against the full command line of every process owned by the deploying user, so the pattern hits any Magento installation on the box, not just the one being deployed. Shared hosting and staging-next-to-production setups commonly run several installations under one system user — a typical maxcluster layout has /var/www/share/example.com and /var/www/share/dev.example.com side by side, both owned by web-user.

Deploying either one then kills the cron jobs and queue consumers of the other. It is silent: the other site's crontab is untouched, so cron restarts it a minute later, and consumers come back on their next consumers_runner tick. What you see is consumers dying mid-message and cron groups aborting on an unrelated deployment.

Fix

Filter the matches by {{deploy_path}} before killing:

run('pgrep -a -U "$(id -u)" -f "bin/magento +(cron:run|queue:consumers:start)" | grep -F "{{deploy_path}}/" | cut -d" " -f1 | xargs -r kill');

The paths Magento itself writes are absolute and under deploy_path: cron:install writes {magentoRoot}bin/magento cron:run into the crontab, and ConsumersRunner builds its command from BP, both resolving through {{deploy_path}}/current.

grep -F rather than folding the path into the pgrep pattern, for two reasons: deploy_path frequently contains dots (dev.example.com) that would silently become regex wildcards, and escaping it for ERE and for the surrounding double-quoted shell string is easy to get subtly wrong. This way the pgrep pattern stays byte-for-byte what it is today.

The trailing slash in "{{deploy_path}}/" keeps /var/www/share/example.com from matching /var/www/share/example.com2.

Verification

Four dummy processes under site-a, site-b and site-a2, with site-a as deploy_path:

--- all matches (current pattern) ---
1828957 /bin/sh .../site-a/current/bin/magento cron:run
1828958 /bin/sh .../site-a/current/bin/magento queue:consumers:start async.operations.all
1828959 /bin/sh .../site-b/current/bin/magento cron:run
1828960 /bin/sh .../site-a2/current/bin/magento cron:run
--- selected by the patched pipeline ---
1828957
1828958

Caveats

Two things worth a maintainer's call:

  1. pgrep -a is procps-ng (Linux, since 3.3.4 / 2012). BSD and macOS pgrep have no -a, where this would select nothing rather than kill everything. If deployment to those targets matters, the alternative is escaping deploy_path into the pgrep regex instead — happy to switch.
  2. Behaviour change for consumers whose command line does not contain deploy_path, e.g. a supervisor config using directory= plus a relative php bin/magento queue:consumers:start. Those are no longer killed. Worth noting that for supervisor-managed consumers the kill is already close to pointless, since supervisor respawns them within seconds against the same old release.

docs/recipe/magento2.md was regenerated with bin/docgen; the diff there is line-number references only.

The pgrep pattern matched any bin/magento cron:run or
queue:consumers:start owned by the deploying user, anywhere on the
filesystem. Where several Magento installations share a server and a
system user, deploying one of them killed the cron jobs and queue
consumers of all the others.

Filter the matches by deploy_path. grep -F keeps the path a literal, so
dots in a hostname are not treated as regex wildcards.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant