Rails, Sidekiq and systemd are on a debian 8 ship

2.5k views Asked by At

The situation: I have a rails app, with sidekiq, working very well, under debian8.

I want an easier control of my sidekiq, so instead of daemonize it with the -d option, I want to create a sidekiq.service file in /etc/systemd/system/sidekiq.service. ( So I'll able to sudo systemctl restart sidekiq.service )

I saw how gitlab do on github, but it's not working for me, because I use rvm to install ruby.

If I do the same with /path/to/rvm/bin/bundle, It returns me an error as "Can't locate Gemfile". ( And I also have run gem install bundler, previously )

I can launch sidekiq from another directory than my rails app with : BUNDLE_GEMFILE=/home/me/myapp/Gemfile bundle exec sidekiq --config /home/me/myapp/config/sidekiq.yml --require /home/me/myapp/config/environment.rb

But in my /etc/systemd/system/sidekiq.service at the ExecStart line, I have an error : Executable path is not absolute

Any clue on how I can do it?

Or, maybe it'll be more efficient / simpler, to run sidekiq in a docker container?

2

There are 2 answers

0
risa_risa On

I just tried this with systemd on Ubuntu 15.04. (This also works for Upstart on Ubuntu 14.x.) I use RVM's alias command for an absolute path for bundle. I think that it will help to also tell it which directory to be in at the start, as well.

To make a RVM wrapper/alias. From the Rails directory:

rvm current                      # grab gemset name
rvm alias create NAME GEMSET_NAME

so if your gemset name was ruby-2.2.2@awesome_app, and you want your wrapper/alias to be called 'awesome_app', it'd be:

rvm alias create awesome_app ruby-2.2.2@awesome_app

Then in systemd, the full path is something like PATH=/usr/local/rvm/wrappers/awesome_app/ which makes the ExecStart command this:

ExecStart=/usr/local/rvm/wrappers/awesome_app/bundle exec sidekiq ...

Edit: I've confirmed that my sidekiq worker is now working properly. Here's my full sidekiq.service file.

Description=Sidekiq Background Worker

[Service]
Type=forking
User=rails
Group=rails
WorkingDirectory=/home/deployer/app
Environment=RAILS_ENV=production
GuessMainPID=true
ExecStart=/usr/local/rvm/wrappers/awesome_app/bundle exec "sidekiq -d -L log/sidekiq.log -q mailer,5 -q default -e production  >> log/sidekiq.log 2>&1"
ExecStop=/usr/local/rvm/wrappers/awesome_app/bundle exec "sidekiqctl stop /home/deployer/app/tmp/pids/sidekiq.pid >> /home/deployer/app/log/sidekiq.log 2>&1"

[Install]
WantedBy=multi-user.target

Currently I have it monitored with monit. I personally prefer Upstart, since systemd requires password every. single. time. but, like I said, can confirm that this works for me.

0
Mike Perham On