Skip to content
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

Closed
nik736 opened this issue Aug 22, 2020 · 5 comments · Fixed by #202
Closed

Running as a daemon #88

nik736 opened this issue Aug 22, 2020 · 5 comments · Fixed by #202
Labels
enhancement New feature or request

Comments

@nik736
Copy link

nik736 commented Aug 22, 2020

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!

@bensheldon
Copy link
Owner

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?

--daemon        backgrounds the process as a daemon
--pidfile=PATH  path to a file with the pid

I'm assuming that a nice-to-have would be an easy way to kill an existing daemonized process (e.g. $ good_job stop --pidfile=PATH) as a quicker way to running $ kill.

Anything else I'm missing?

@sj26
Copy link
Contributor

sj26 commented Aug 24, 2020

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 (good_job &), or use something like nohup:

nohup good_job &

@bensheldon bensheldon added the enhancement New feature or request label Aug 26, 2020
@gadimbaylisahil
Copy link
Contributor

gadimbaylisahil commented Sep 9, 2020

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

deploy.rb - Capistrano

  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

@bensheldon bensheldon changed the title Running as a deamon Running as a daemon Sep 9, 2020
@bensheldon
Copy link
Owner

This feature has been released in v1.6.0 🎉

@gadimbaylisahil
Copy link
Contributor

This feature has been released in v1.6.0 tada

Awesome news, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants