Upstart Boilerplate for Background Workers

I use foreman to run background work process when developing.

After writing the Procfile, we can export it to many process management through foreman export command.

The generated files never worked well for me (when exporting to Upstart). Very likely because rbenv or RVM is not loaded.

Here is the boilerplate I've used:

# ironman.conf

pre-start script

bash << "EOF"
  mkdir -p /var/log/ironman
  chown -R deploy /var/log/ironman
EOF

end script

start on runlevel [2345]

stop on runlevel [016]
# ironman-worker.conf

start on starting ironman
stop on stopping ironman
# ironman-worker-1.conf

start on starting ironman-worker
stop on stopping ironman-worker
respawn

exec su - deploy -c \
'cd /path/to/ironman; export RAILS_ENV=staging; export PORT=5100; bundle exec sidekiq >> /var/log/ironman/worker-1.log 2>&1'

Note:

  • Replace ironman by the name of your application
  • Replace deploy by your server's user
  • Replace /var/log/ironman/ by your log path
  • Replace /path/to/ironman by the path of your application
  • Replace staging (in export RAILS_ENV=staging;) by your application environment
  • Replace bundle exec sidekiq by the command you wanna run (bundle exec rake resque:work QUEUE=communication,normal for instance)

Place these files in /etc/init, then you can use sudo service ironman to start, stop etc.

You can spawn more processes by creating new files, for instance: ironman-clock.conf and ironman-clock-1.conf.

You will want to restart the workers after deploying new code (to reload application). But if your user isn't root, you have no permission to run sudo service ironman restart.

To solve it, I grant permission to my application user. Run sudo visudo and add the following:

# User privilege specification
deploy  ALL=(root) NOPASSWD: /sbin/start ironman, /sbin/stop ironman
Don't forget to replace deploy (server's user) and ironman here again.

That is it, thanks.

Written on December 2, 2015

Share: