Skip to content

Instantly share code, notes, and snippets.

@kkestell
Last active October 4, 2016 03:45
Show Gist options
  • Save kkestell/7224409 to your computer and use it in GitHub Desktop.
Save kkestell/7224409 to your computer and use it in GitHub Desktop.
Using Upstart to Keep Puma Running

Add an Upstart script to start Puma and keep it running:

# nano /etc/init/puma.conf

Add:

start on startup
stop on shutdown

env RAILS_ENV=staging

chdir /home/git/travelogue/current

script
  exec sudo -u git bundle exec puma -C /home/git/travelogue/current/config/puma.rb
end script

respawn

Then you can:

# start puma

Your /shared/config/puma.rb can look like this:

threads     1,16
workers     2
environment "staging"

stdout_redirect "/home/git/travelogue/current/log/puma-stdout.log",
                "/home/git/travelogue/current/log/puma-stderr.log"

# The state file is how pumactl communicates with your server, don't
# forget to create the pids directory!
state_path "/home/git/travelogue/shared/pids/puma.state"

bind "tcp://0.0.0.0:8080"

on_worker_boot do
  ActiveSupport.on_load(:active_record) do
    ActiveRecord::Base.establish_connection
  end
end

daemonize
preload_app!

Want to restart the server when you deploy? No problem. Add something like this to your deploy.rb

  desc "Restart application"
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      # Your restart mechanism here, for example:
      execute "cd #{release_path} && bundle exec pumactl -S #{shared_path}/pids/puma.state restart"
    end
  end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment