How do I run resque background tasks on my droplet using foreman, capistrano and nginx?

August 20, 2014 2.8k views

I have not been able to find any documentation on how to run worker tasks in the background for something like Resque. There is an article for installing Redis on the droplet: [](http:// ""), and I have a Procfile using Foreman gem in which I have the background tasks, but I can't figure how to run them on the droplet.

2 Answers

The Resque wiki provide a sample cap task to manage Resque processes.

namespace :foreman do
  desc "Start the application services"
  task :start, :roles => :app do
    sudo "start #{application}"

  desc "Stop the application services"
  task :stop, :roles => :app do
    sudo "stop #{application}"

  desc "Restart the application services"
  task :restart, :roles => :app do
    run "sudo start #{application} || sudo restart #{application}"

  desc "Display logs for a certain process - arg example: PROCESS=web-1"
  task :logs, :roles => :app do
    run "cd #{current_path}/log && cat #{ENV["PROCESS"]}.log"

  desc "Export the Procfile to upstart scripts"
  task :export, :roles => :app do
    # 5 resque workers, 1 resque scheduler
    run "cd #{release_path} && rvmsudo bundle exec foreman export upstart /etc/init -a #{application} -u #{user} -l #{shared_path}/log  -f #{release_path}/Procfile.production -c worker=5,scheduler=1"

It builds off this blog post on managing and monitoring a Ruby application with Foreman and Upstart. That should point you in the right direction.

Thanks for pointing me in the right direction. There were still a few issues that I faced in implementing this properly, so I thought I'd help others trying to implement this in their app. I assume that resque has already been implemented in the app locally. I followed these steps for implementing resque on my app hosted on digital ocean:

This is what my Procfile looks like :

worker:  RAILS_ENV=production bundle exec rake resque:work QUEUE='*'
scheduler: RAILS_ENV=production bundle exec rake resque:scheduler
faye: bundle exec rackup -s thin -E production
  • Make changes in the resque configuration to run the resque server on the app's production IP: in initializers/resque-web.rb
Resque.redis = ENV['REDIS_URL']
Resque.after_fork = { ActiveRecord::Base.establish_connection }
  • The following capistrano tasks need to be created to run the servers/tasks mentioned in the Procfile : in config/deploy.rb
task :export do

    on roles(:app) do
      execute [
        "cd #{release_path} &&",
        'export rvmsudo_secure_path=0 && ',
        "#{fetch(:rvm_path)}/bin/rvm #{fetch(:rvm_ruby_version)} do",
        'RAILS_ENV=production bundle exec foreman export --app appname --user username -l logfile-path -f ./Procfile upstart /etc/init -c worker=1,scheduler=1,faye=1'
      ].join(' ')

task :restart do 
  on roles(:app) do
    execute :sudo, "restart appname"
after :publishing, :export
   after :publishing, :restart

This should get the workers going.

by Etel Sverdlov
This tutorial covers the Redis Installation, some simple Redis operations, and the 5 Redis data types. Redis, developed in 2009, is a flexible, open-source, key value data store that allows users to store vast amounts of data without the limits of a relational database.
Have another answer? Share your knowledge.