Skip to content

Instantly share code, notes, and snippets.

@aCandidMind
Forked from czottmann/Procfile
Last active January 6, 2016 17:05
Show Gist options
  • Save aCandidMind/4f7f304d92aa90ff170d to your computer and use it in GitHub Desktop.
Save aCandidMind/4f7f304d92aa90ff170d to your computer and use it in GitHub Desktop.
Example of a Foreman/Capistrano/upstart setup

Example of a Foreman/Capistrano/upstart setup

I need to jot down my Foreman-based web app setup real quick lest I forget.

When I build a Rails app (any web app, actually), it's not just the app itself anymore. There also are background jobs to be taken care of -- started, stopped, restarted. Usually these are required to run in production on a remote box, too, so I use Foreman to run those jobs. Foreman uses a Procfile to learn about said jobs, gives me a dead-simple way to start and stop them, and provides me with color-coded output when they run. It's a great timesaver.

Then, when I deploy my app to a remote machine, I use Foreman's ability to export upstart (or inittab) configurations. For that, I have custom tasks in my Capistrano-based config/deploy.rb file. Those halt the current jobs on the remote machine, trigger Foreman to write out new configurations for upstart and re-start the jobs. (Please note: Foreman needs to be installed on the remote box as well.)

Since it took me a while to get this setup working, I figured I'd share it real quick.

My examples come from a Rails-based environment, but I tried to make them app-agnostic as possible.

This here "article" is basically a bare-bone, stripped down version of Tim Riley's "Run Your Own Piece of Heroku with Foreman".

Assumptions

  • The Foreman gem is installed both on the local dev machine and the remote box
  • Deployment is done using Capistrano
  • The remote machine is running Ubuntu
  • The remote machine has upstart installed
### Foreman-related snippet of `config/deploy.rb` below.
### Rest of the file omitted!
### Updated for capistrano 3 conformance via https://gist.github.com/carlo/1027117#gistcomment-1415398
namespace :foreman do
desc "Export the Procfile to Ubuntu's upstart scripts"
task :export do
on roles(:app) do
within current_path do
execute :rbenv, :exec, "bundle exec foreman export upstart /etc/init --procfile=./Procfile -a #{fetch(:application)} -u #{fetch(:user)} -l #{current_path}/log"
end
end
end
desc "Start the application services"
task :start do
on roles(:app) do
within current_path do
execute :rbenv, :exec, "foreman start #{fetch(:application)}"
end
end
end
desc "Stop the application services"
task :stop do
on roles(:app) do
within current_path do
execute :rbenv, :exec, "foreman stop #{fetch(:application)}"
end
end
end
desc "Restart the application services"
task :restart do
on roles(:app) do
within current_path do
execute :rbenv, :exec, "foreman start #{fetch(:application)} || foreman restart #{fetch(:application)}"
end
end
end
end
after "deploy:publishing", "foreman:export"
after "deploy:publishing", "foreman:restart"
worker: QUEUE=* bundle exec rake environment resque:work
scheduler: bundle exec rake environment resque:scheduler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment