Created
April 28, 2014 15:44
-
-
Save mauris/11375869 to your computer and use it in GitHub Desktop.
Laravel Artisan Queue Ensurer - Set a cron job to run this file periodically to ensure that Laravel's queue is processing all the time. If the queue listener stopped, restart it!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function runCommand () | |
{ | |
$command = 'php artisan queue:listen > /dev/null & echo $!'; | |
$number = exec($command); | |
file_put_contents(__DIR__ . '/queue.pid', $number); | |
} | |
if (file_exists(__DIR__ . '/queue.pid')) { | |
$pid = file_get_contents(__DIR__ . '/queue.pid'); | |
$result = exec('ps | grep ' . $pid); | |
if ($result == '') { | |
runCommand(); | |
} | |
} else { | |
runCommand(); | |
} |
For shared hosted with disabled exec functions, I made a shell script.
Cron job:
* * * * * cd /path/to/public_html && /usr/bin/sh artisan.call.sh >> /dev/null 2>&1
How work
- Check for folder
/storage/queue.lock/
- If not exists, call
artisan queue:work
and create the folder, otherwise do nothing. - Check for file
/storage/.queue-restart
, if exists, runartisan queue:restart
and remove folder/storage/queue.lock/
When run artisan queue:restart
the script must exit and auto remove queue.lock folder, next time call artisan queue:work
.
#!/bin/bash
FULL_PATH_TO_SCRIPT="$(realpath "$0")"
SCRIPT_DIRECTORY="$(dirname "$FULL_PATH_TO_SCRIPT")"
LOCKFILE="$SCRIPT_DIRECTORY/storage/queue.lock"
LOGFILE="$SCRIPT_DIRECTORY/storage/logs/queue.log"
QUEUE_RESTART_FILE="$SCRIPT_DIRECTORY/storage/logs/.queue-restart"
d=$(date '+%F %T')
log()
{
echo '=============================================================================================' >> "${LOGFILE}"
echo "$d" >> "${LOGFILE}"
if [ "$1" ]; then
echo "$1" >> "${LOGFILE}"
fi
}
clean_log()
{
truncate -s 0 "${LOGFILE}"
}
remove_lock()
{
log "Removing lock file $LOCKFILE… exiting"
rmdir "$LOCKFILE"
}
another_instance()
{
log "Lock file $LOCKFILE exists… exiting"
exit 1
}
run_artisan()
{
if [ "$1" ]; then
(php ${SCRIPT_DIRECTORY}/artisan $1)
fi
}
if ! mkdir "${LOCKFILE}" 2>/dev/null; then
another_instance
exit 1
fi
if [ -f "${QUEUE_RESTART_FILE}" ]; then
rm "$QUEUE_RESTART_FILE"
log "restarting queue"
run_artisan "queue:restart"
remove_lock
exit 1
fi
trap remove_lock QUIT INT TERM EXIT
clean_log
log "runing php \"${SCRIPT_DIRECTORY}/artisan queue:work"\"
run_artisan "queue:work"
@alvaro-canepa thanks this work me
I also need the shell-script variant... pgrep
allows to use it without the pid file.
However you can only run one instance of php artisan queue:work
I use pgrep
to only search for the artisan queue:work
since my provider uses an alias for php.
#!/bin/bash
# Check if "artisan queue:work" is running
if pgrep -f "artisan queue:work" > /dev/null
then
echo "queue:work is already running."
exit 1
else
echo "Starting queue:work..."
nohup php artisan queue:work > /dev/null 2>&1 &
echo "queue:work started."
fi
@CodeAndWeb looks nice, more clean than my code!
Thanks to share
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What's the purpose of
& echo $!
here?