-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Minion Daemon to Help with Task Processing
- Loading branch information
David Kobia
committed
Oct 31, 2013
1 parent
fdbb390
commit 65e4445
Showing
2 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#!/bin/bash | ||
# | ||
# This script is similar to minion but will do a few additional things: | ||
# - PHP process is run in the background | ||
# - PHP process is monitored and restarted if it exits for any reason | ||
# - Added handlers for SUGHUP, SIGINT, and SIGTERM | ||
# | ||
# This is meant for long running minion tasks (like background workers). | ||
# Shutting down the minion tasks is done by sending a SIGINT or SIGTERM signal | ||
# to this miniond process. You can also restart the minion task by sending a | ||
# SUGHUP signal to this process. It's useful to restart all your workers when | ||
# deploying new code so that the workers reload their code as well. | ||
# You cannot use this script for tasks that require user input because of the | ||
# PHP process running in the background. | ||
# | ||
# Usage: ./miniond [task:name] [--option1=optval1 --option2=optval2] | ||
# | ||
# And so on. | ||
# | ||
|
||
# Define some functions | ||
function start_daemon() | ||
{ | ||
echo "Starting" | ||
./minion $ARGS & # This assumes miniond is sitting next to minion | ||
pid=$! # Store pid (globally) for later use.. | ||
} | ||
|
||
function stop_daemon() | ||
{ | ||
kill -TERM $pid | ||
wait $pid # Wait for the task to exit and store the exit code | ||
ecode=$? # Store exit code (globally) for later use.. | ||
} | ||
|
||
function handle_SIGHUP() | ||
{ | ||
echo "Restarting" | ||
stop_daemon | ||
start_daemon | ||
} | ||
|
||
function handle_SIGTERM_SIGINT() | ||
{ | ||
echo "Shutting Down" | ||
stop_daemon | ||
exit $ecode | ||
} | ||
|
||
# Register signal handlers | ||
trap handle_SIGHUP SIGHUP | ||
trap handle_SIGTERM_SIGINT SIGTERM SIGINT | ||
|
||
ARGS=$@ | ||
|
||
start_daemon | ||
|
||
while : | ||
do | ||
# Pauses the script until $pid is dead | ||
wait $pid | ||
|
||
# Make sure someone didn't start it back up already (SIGHUP handler does this) | ||
if ! kill -0 $pid > /dev/null 2>&1 | ||
then | ||
start_daemon | ||
fi | ||
done |