-
-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Running as a daemon #88
Comments
Thanks for asking! Having a daemon mode is likely necessary. I was hoping that everyone had moved onto upstart, systemd, or containers like Docker or platforms like Heroku. But I know that's not true because in my day job we run DelayedJob as a daemon 😁 Would this interface be sufficient for your needs?
I'm assuming that a nice-to-have would be an easy way to kill an existing daemonized process (e.g. Anything else I'm missing? |
Personally I think baking daemonization into every process is an anti-pattern. Being a good daemon is difficult. Doing it with a tool which solves these problems like systemd or upstart or launchd is ideal. If you just want to run the process in the background, can you background it (
|
Just leaving init.d/ script here for legacy folks. Also would love a review xD. Not the best bash writer :) /etc/init.d/app-name-good-job APP_USER=your_user_here
APP_ROOT=path_to_your_app
# GoodJob PID
# This can be improved to detect multiple processes and destroy but I have other monitoring in place
# Finds PID of good_job executable
PID=$(ps U "$APP_USER" | grep "[/]good_job" | awk '{ print $1 }')
# Command to run good_job
CMD="cd $APP_ROOT && RAILS_ENV=sandbox GOOD_JOB_MAX_THREADS=5 GOOD_JOB_QUEUES=queue1,queue2 GOOD_JOB_POLL_INTERVAL=10 bundle exec good_job start"
# Function to kill pid if it's set, with signal as argument
sig () {
[ -n "$PID" ] && kill -$1 $PID
}
run () {
if [ "$(id -un)" = "$APP_USER" ]; then
eval $1
else
su -c "$1" - $APP_USER
fi
}
# I run only single GoodJob process, but can be modified
case "$1" in
start)
sig 0 && echo >&2 "Already running, only a single good-job process should be run, and previous one before deploy should be turned off first." && exit 1
run "$CMD" &
;;
stop)
sig TERM && exit 0
echo >&2 "good-job is already turned off"
;;
status)
if [ -n "$PID" ]; then
echo "good-job is running (pid $PID)."
exit 0
else
echo "good-job is not running."
exit 1
fi
;;
status_without_fail_exit)
if [ -n "$PID" ]; then
echo "good-job is running (pid $PID)."
else
echo "good-job is not running."
fi
;;
*)
echo >&2 "Usage: $0 <start|stop|status|status_without_fail_exit>"
exit 1
;;
esac
desc "Restart Jobs"
task :restart_jobs do
on roles(:app), in: :sequence, wait: 5 do
# Current GoodJob PID
execute :sudo, "-u #{fetch(:application)} /etc/init.d/#{fetch(:application)}-good-job status_without_fail_exit"
# TERM GoodJob process
execute :sudo, "-u #{fetch(:application)} nohup /etc/init.d/#{fetch(:application)}-good-job stop"
# Starts new GoodJob process
execute :sudo, "-u #{fetch(:application)} nohup /etc/init.d/#{fetch(:application)}-good-job start"
# Current GoodJob PID
execute :sudo, "-u #{fetch(:application)} /etc/init.d/#{fetch(:application)}-good-job status"
end
end |
This feature has been released in |
Awesome news, thank you! |
First of all great job and thanks for this gem! I love the idea and how it works.
I just wanted to ask what the best way is to run is as a deamon? Is a -d flag planned?
Thanks!
The text was updated successfully, but these errors were encountered: