Sunday, 14 February 2016

Sidekiq Upstart Job or Sidekiq as a service

Sidekiq Upstart Job

Running Sidekiq as a Service

How to run sidekiq automatically if sidekiq stops running

To run the processes in background sidekiq is used. Here we will see an example of sidekiq being used to serve the background jobs of a rails application. Many times you might observe sidekiq gets stopped suddenly and won't get started as a result of which your background jobs will get queued and will fail after a specific time.
In this post we will run sidekiq as a service or as an upstart job which will start the sidekiq on system reboot or if the sidekiq crashes suddenly.

First make a file in /etc/init/ directory, say sidekiq.conf

sudo vim /etc/init/sidekiq.conf

Now copy and paste the following code in the file:


# Just a custom description for our Job
description "Sidekiq Background Worker"

# On which conditions the job should start. In this case it's very simple: On the system startup (this is basically when the system is booted)
#start on startup
start on runlevel [2345]

# On which conditions the job should stop. In this case when the system reboot (http://upstart.ubuntu.com/cookbook/#runlevels)
stop on runlevel [06]

# This are the User and User Group that will be used to run the Job. On our case it should be the user that we have set on our capistrano script for instance.
# You can check at `config/deploy/<environment>.rb` on this line `server <some_ip_addreess>, user: <deploy_user>`

setuid ubuntu
setgid ubuntu

# This indicate that we want to restart the Job if it crashes
respawn
respawn limit 5 10

# TERM is sent by sidekiqctl when stopping sidekiq.  Without declaring these as normal exit codes, it just respawns.
normal exit 0 TERM

script
# this script runs in /bin/sh by default
# respawn as bash so we can source in RVM
exec /bin/bash <<EOT
  # use syslog for logging
  exec &> /dev/kmsg

  # Jump into the capistrano deployment directory
  cd /home/ubuntu/myproject/

  # Start Sidekiq through RVM. Note that I'm using the standard Capistrano paths
  exec ~/.rvm/bin/rvm-shell -c 'bundle exec sidekiq -C ./config/sidekiq.yml --environment production --logfile /home/ubuntu/myproject/log/sidekiq.log'

EOT

end script

The script is itself self explanatory. You just need to replace the project directory in place of myproject and the environment in which it runs like here it is the production environment. One more thing to note here is the log file of sidekiq is /home/ubuntu/myproject/log/sidekiq.log and similarly the conf file is /home/ubuntu/myproject/config/sidekiq.yml, so change these as per your project scaffolding.

Now save and exit. We are almost done now we need to start this job by running the following command.


sudo service sidekiq start

This will start the sidekiq service which you can see by running the following command

sudo service sidekiq status

And we can see that sidekiq is also running:

ps aux | grep sidekiq

You can now kill the sidekiq process by the process id shown in the result of above command and wait a bit and keep the status again if sidekiq is running or not. You will notice that the sidekiq has started again after we killed the sidekiq process.

Try this out to get rid of checking again and again whether sidekiq is running or not. If you find any difficulty or have any doubt in the above procedure than your doubts are welcome in comments.

8 comments:

  1. the script is look great, thank Ajeet :)

    ReplyDelete
  2. Failed to start sidekiq.service: Unit sidekiq.service not found
    I am getting this error

    ReplyDelete
    Replies
    1. Please save this upstart as sidekiq.conf, so that it will be available as a service with "sidekiq" name. Try saving it in /etc/init.d file if you are using RHEL based OS.

      Delete
  3. Thank you for this guide, very helpful! But please, can you add as an alternative a call of sidekiq via rbenv? Here is what to add:
    --
    # Start Sidekiq through RBENV. Note that I'm using the standard Capistrano paths
    /home/ubuntu/.rbenv/shims/bundle exec sidekiq -c 1 -L log/sidekiq.log

    ReplyDelete
  4. Hi I run this script and run sudo service sidekiq start and return me error:

    Failed to start sidekiq.service: Unit sidekiq.service not found

    My environment is an ubuntu 16.04LTS in a vm machine and I saved the script in the same location and same name mention above in the first step.

    Please help...

    ReplyDelete
    Replies
    1. Yes, this will be a problem for UBUNTU 16.04LTS user as the upstart jobs are being replaced by systemd. So you need to change this script to match with systemd script requirement.

      Delete
  5. Hello,
    I tried putting the script in /etc/init/ and also in /etc/init.d/ but when I try to start the service it says:
    sidekiq: unrecognized service

    Please help.

    ReplyDelete

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel