Your software
My Mautic version is: 5.2.1 (docker: mautic/mautic:5.2.1-apache)
My PHP version is: 8.1
My Database type and version is: mysql 8.4
I’m trying to understand why the documentation (Configuring Doctrine for Email Queue Management in Mautic with Cron Jobs - Mautic Knowledgebase) provides instructions to create a script which basically runs php bin/console messenger:consume email --limit=60 --time-limit=480 --memory-limit=128M
every 10 minutes, but by default in the worker container there is already the same process running controlled by supervisord. What’s the purpose of having it twice?
The documentation is probably aimed toward administrators who aren’t using docker.
Anyways, overlapping cronjobs is usually a bad idea.
And the script seems to use a lock file logic trying to prevent that:
# Check if the lock file exists
if [ -e $LOCKFILE ]; then
LOCKFILE_AGE=$(($(date +%s) - $(stat -c %Y $LOCKFILE)))
if [ $LOCKFILE_AGE -ge $LOCKFILE_TIMEOUT ]; then
log "Lock file is older than $LOCKFILE_TIMEOUT seconds. Removing stale lock file."
rm -f $LOCKFILE
else
log "Process is already running. Exiting."
exit 1
fi
fi
So you could basically run the script as much as you want. And it will keep exiting unless the lock is gone or expired.
But beware, that script may cause overlaps. And ultimately result in a server crash.
Pro tip;
Ignore that script which was DOA. And just use something like “flock” which is often already installed.
Is it correct then to assume that if using docker worker, we can rely on the daemonized php bin/console messenger:consume email
that is running there (and respawned by supervisord if it dies) and do away with the script entirely?
I couldn’t say on that part (I’m not using docker).
Still, if the php script is ran from time to time (ideally on a scheduler), no need to use that script.
And if not, no need to use that script too.
All in all, don’t use that script. It’s a bad idea that may cause you some problems later on.
How to replace that whole script, if you ever need to:
* * * * * flock /tmp/mautic-messenger --command "/path/to/php /path/to/bin/console messenger:consume email --limit=60 --time-limit=480 --memory-limit=128M"
An edited version of the KB:
Step 2: Implementing Cron Jobs for Automated Email Sending
Adding the Cron Job
Add this line to your crontab:
* * * * * flock /tmp/mautic-messenger --command "/path/to/php /path/to/bin/console messenger:consume email --limit=60 --time-limit=480 --memory-limit=128M" >> /path/to/mautic/var/logs/mautic_consume.log 2>&1
This schedules the script to run every minute, avoid overlapping, and log the output to mautic_consume.log
.