Cron runs when nobody is watching: Magento 2 cron vs the Laravel scheduler
On 2 June I moved a Magento 2 store from PHP 8.1 to 8.2, and the client confirmed that everything worked. On Monday, 8 June, he wrote that an email had announced a new trade account application, but it wasn't in the admi
On 2 June I moved a Magento 2 store from PHP 8.1 to 8.2, and the client confirmed that everything worked. On Monday, 8 June, he wrote that an email had announced a new trade account application, but it wasn't in the admin's customer grid. The web server ran 8.2. The cron job still named the old PHP version, and the index behind that grid hadn't been refreshed. My change, my bug. 33 minutes after his message the grid was reindexed, the cron job pointed at 8.2 and the cause was written in the ticket.
Magento and Laravel both put their whole schedule behind one crontab line. They differ in which PHP that line runs, and in what happens when a job takes longer than a minute.
One crontab line, every job
In Magento 2.4.9 the line comes from bin/magento cron:install. The PHP path in it is the binary that ran the command, written as plain text. Started through a symlink, the command wrote the symlink's target. Run cron:install again after an upgrade, and it answers "Crontab has already been generated and saved" and changes nothing. Only --force, or cron:remove first, rewrites the line.
# the PHP path is the binary that ran cron:install, kept until --force
#~ MAGENTO START 69dd2b02e1f3a65918182048ea4e29979a849d8942e8f53ed20a4bf10e529b36
* * * * * /usr/local/bin/php /var/www/html/bin/magento cron:run >> /var/www/html/var/log/magento.cron.log 2>&1
#~ MAGENTO END 69dd2b02e1f3a65918182048ea4e29979a849d8942e8f53ed20a4bf10e529b36
Every job inherits that binary. By default cron:run starts the index, consumers and analytics groups as separate processes, and it starts them with its own PHP binary.
Laravel's documentation gives a different line: cd /path-to-your-project && php artisan schedule:run. Here php is whatever cron's PATH finds, every minute. Each scheduled Artisan command then starts with the binary that ran schedule:run. In the test Laravel built it as '/usr/local/bin/php' 'artisan' acme:slow, while schedule:list without -v showed only php artisan acme:slow.
After a PHP upgrade, Magento's line keeps the version it names. Laravel's follows php on cron's PATH, and that's only the new version if the upgrade moved it.
A job that outlives its minute
Take a job scheduled every minute that runs for 100 seconds.
In Magento, a second cron:run a minute later didn't start a second copy. Each running job holds a lock in Magento's lock manager. The second run couldn't take it, logged "Could not acquire lock for cron job: acme_slow_job" and left the next minute's row pending.
Laravel runs it again. Its documentation says it plainly: tasks run "even if the previous instance of the task is still running". In the test, two copies ran side by side. With withoutOverlapping(), the second schedule:run printed "No scheduled commands are ready to run" and left the first copy alone.
use Illuminate\Support\Facades\Schedule;
// without it, a run that outlives its minute starts a second copy
Schedule::command('orders:export')
->everyMinute()
// the lock sits in the cache: expire it after 10 minutes, not 24 hours
->withoutOverlapping(10);
# php is whatever cron's PATH finds, every minute
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
When the process dies
Now kill the job with kill -9 halfway through, together with the cron:run or schedule:run that started it, as a crash or the OOM killer would.
In Magento the row stays "running". With the default db lock provider the lock dies with the process, and the next minute's cron:run marked the old row as an error and ran the job again.
Laravel's withoutOverlapping() keeps its lock in the cache, and by default the lock expires after 24 hours. After the kill, the next run printed "No scheduled commands are ready to run" again. php artisan schedule:clear-cache deleted the lock, and the run after that started the job. It deletes the locks of every task, so run it when nothing is running. You can pass a shorter expiry in minutes, withoutOverlapping(10), that matches how long the task can really take.
It's the reverse of part 9, where the lock that outlived a killed process was Magento's.
Before you close a PHP upgrade
My upgrade had covered the web server. Cron, systemd timers and Supervisor programs carry their own paths, and nothing fails loudly when they keep the old one. Now I search them for the old version before I call an upgrade done.
# as root, to read every user's crontab: anything that still names the old version
sudo grep -rnE "php[/-]?8\.?1" /etc/cron* /var/spool/cron /etc/systemd/system /etc/supervisor 2>/dev/null
# Magento: rewrite the crontab block as the shop's user, with the new binary
sudo -u shop php8.2 bin/magento cron:install --force
# Laravel: removes the withoutOverlapping() locks of every task; run it when nothing is running
php artisan schedule:clear-cache
The rule
After a PHP upgrade, check what cron runs as well as what the web server runs. In Magento, run cron:install --force with the new binary, as the user who owns the shop. In Laravel, put withoutOverlapping() on every task that can outlive its interval, with an expiry that fits it, and keep schedule:clear-cache in mind for the day a server dies mid-run.
Next: cache by who's asking, Magento's full page cache next to Laravel's cache keys.
After your last PHP upgrade, did you check the crontab, or did the crontab tell you?
Part 11 of 15 of the Magento 2 vs Laravel series. Originally published on LinkedIn.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.