Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erichs/46dcb1ea2e2db16c0aa2 to your computer and use it in GitHub Desktop.
Save erichs/46dcb1ea2e2db16c0aa2 to your computer and use it in GitHub Desktop.

Using Unicorn with Upstart

This configuration works with Upstart on Ubuntu 12.04 LTS

The reason why it needs to be done this way (i.e. with the pre-start and post-stop stanzas), is because Upstart is unable to track whever Unicorn master process re-execs itself on hot deploys. One can use it without hot-deploys and run Unicorn in foreground also, it then only needs one exec stanza.

This presumes you are not using RVM, so no voodoo dances.

#/etc/init/my-app.conf
description "My App"
author "Ilya Dmitrichenko <errordeveloper@gmail.com>"

start on virtual-filesystems
stop on runlevel [06]

env PATH=/opt/ruby/shims:/opt/ruby/rbenv/bin:/usr/local/bin:/usr/bin:/bin

env RAILS_ENV=production

setuid nobody
setgid nobody

chdir /opt/my_app/

pre-start exec ./bin/unicorn_rails -D -c /opt/my_app/config/unicorn.rb --env production

post-stop exec kill `cat /opt/my_app/run/unicorn.pid`

The next example allows one to run a very descriptive command from their deploy script:

initctl emit my-app-deploy TAG=v1.2.3`.

It demonstrates a very appropriate use of Upstart's events. Please also note that it can be used for roll-back just as well.

#/etc/init/my-app-deploy-task.conf
description "Upgade My App!"
author "Ilya Dmitrichenko <errordeveloper@gmail.com>"

start on my-app-deploy

task

console log

env PATH=/opt/ruby/shims:/opt/ruby/rbenv/bin:/usr/local/bin:/usr/bin:/bin

chdir /opt/my_app/

script
  git fetch --quiet --all --tags origin
  git checkout --quiet --force --detach $TAG
  bundle --quiet install
  kill -USR2 `cat ./run/unicorn.pid`
end script
@erichs
Copy link
Author

erichs commented Jun 5, 2014

@errordeveloper, I needed to say 'start' before on my-app-deploy. YMMV. Thanks for the great example!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment