title: Background jobs with Resque on Heroku
body: <p>I am having a really strange problem on Heroku that I have been spinning my wheels to figure out for a while now.</p>
  
  <p>My app has a few external API calls and mailers which I have set up ActiveJob to run in the background. On Heroku I have two workers set up as and I am using a Resque/Redis combo for the jobs as per the below snippets. I am using the Redis Cloud add-on on Heroku.</p>
  
  <h1>Config / setup</h1>
  
  <p><strong>Procfile</strong></p>
  
  <pre><code>web: bundle exec puma -C config/puma.rb
  resque: env TERM_CHILD=1 QUEUE=* bundle exec rake resque:work
  </code></pre>
  
  <p><strong>lib/tasks/resque.rake</strong></p>
  
  <pre><code>require "resque/tasks"
  require "resque/scheduler/tasks"
  
  task "resque:setup": :environment do
    Resque.before_fork = proc { ActiveRecord::Base.connection.disconnect! }
    Resque.after_fork = proc { ActiveRecord::Base.establish_connection }
  end
  </code></pre>
  
  <p><strong>config/initializers/active_job.rb</strong></p>
  
  <pre><code>Rails.application.config.active_job.queue_adapter = :resque
  </code></pre>
  
  <p><strong>config/initializers/redis.rb</strong></p>
  
  <pre><code>if ENV["REDISCLOUD_URL"]
    $redis = Redis.new(url: ENV["REDISCLOUD_URL"])
  end
  </code></pre>
  
  <p><strong>config/initializers/resque.rb</strong></p>
  
  <pre><code>if Rails.env.production?
    uri = URI.parse ENV["REDISCLOUD_URL"]
    Resque.redis = Redis.new(host: uri.host, port: uri.port,
                             password: uri.password)
  else
    Resque.redis = "localhost:6379"
  end
  </code></pre>
  
  <h1>The problem</h1>
  
  <p>The problem I am having is when a user is using the app in browser (i.e., interfacing with the web worker) and performs an action which triggers one of the ActiveJob jobs the job is run "inline" using the <code>web</code> worker and not the <code>resque</code> worker. When I run the specific model method that queues the job in my Heroku app console (opened by running <code>heroku run rails console</code>) it adds the job to Redis and runs it using the <code>resque</code> worker as expected.</p>
  
  <p>Why would one way work properly and the other way not work? I have looked at almost every tutorial / SO question on the topic and have tried everything so any help getting the jobs to be run but the right worker would be amazing!</p>
  
  <p>Thanks in advance!</p>