André Laszlo
André Laszlo

Reputation: 15537

delayed_paperclip uses ActiveJob instead of Resque

I have configured Delayed::Paperclip to process Paperclip attachments in the background. I installed and configured Resque and according to the README, Resque should now handle background tasks:

Make sure that you have Resque up and running. The jobs will be dispatched to the :paperclip queue, so you can correctly dispatch your worker. Configure resque and your workers exactly as you would otherwise.

However, Active Job, another framework for running background tasks, is installed as a dependency for Rails 4.1/ActionMailer and it "steals" the task from Resque.

 [ActiveJob] Enqueued DelayedPaperclip::Jobs::ActiveJob (Job ID: ba60f576-e544-4f53-8db2-eb0085f1f653) to Inline(paperclip) with arguments: "Photo", 79, "picture"

The problem is that Active Job seems to be running immediately in the same thread - basically it doesn't run in the background at all.

I checked out the code for Delayed::Paperclip and it seems like there's a priority to the installed backends:

def detect_background_task
  return DelayedPaperclip::Jobs::ActiveJob  if defined? ::ActiveJob::Base
  return DelayedPaperclip::Jobs::DelayedJob if defined? ::Delayed::Job
  return DelayedPaperclip::Jobs::Resque     if defined? ::Resque
  return DelayedPaperclip::Jobs::Sidekiq    if defined? ::Sidekiq
end

When I switched them around and put Resque on top, it works. The priority seems to be hardcoded, but I figure I can't be the only one with this problem if that's the case. Is this a bug or am I missing something?

Upvotes: 3

Views: 671

Answers (1)

André Laszlo
André Laszlo

Reputation: 15537

Active Job is actually a really good primary choice since it acts as an abstraction layer:

Active Job is a framework for declaring jobs and making them run on a variety of queueing backends. [...] The main point is to ensure that all Rails apps will have a job infrastructure in place, even if it's in the form of an "immediate runner".

The default is to run tasks immediately, but other backends can be chosen as well. One of these backends is Resque. So the only thing you need to do is configure Active Job to use Resque: Delayed::Paperclip → Active Job → Resque. Just put this in your application.rb:

module MyApp
  class Application < Rails::Application
    ...
    config.active_job.queue_adapter = :resque
  end
end

The Delayed::Paperclip README should probably be updated.

Upvotes: 3

Related Questions