Skip to content
This repository was archived by the owner on Oct 5, 2018. It is now read-only.

Commit

Permalink
Merge pull request #169 from morgoth/set-queue-name
Browse files Browse the repository at this point in the history
Allow to set queue name for background jobs
  • Loading branch information
Scott Carleton committed Apr 26, 2016
2 parents 8477e5c + c74726b commit ff68309
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 40 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,19 @@ end
@user.avatar.reprocess_without_delay!(:medium)
````

#### Set queue name

You can set queue name for background job. By default it's called "paperclip".
You can set it by changing global default options or by:

```
class User < ActiveRecord::Base
has_attached_file :avatar
process_in_background :avatar, queue: "default"
end
```

Defaults
--------

Expand Down
7 changes: 3 additions & 4 deletions lib/delayed_paperclip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ def options
@options ||= {
:background_job_class => detect_background_task,
:url_with_processing => true,
:processing_image_url => nil
:processing_image_url => nil,
:queue => "paperclip"
}
end

Expand Down Expand Up @@ -59,11 +60,9 @@ def process_in_background(name, options = {})
:only_process => only_process_default,
:url_with_processing => DelayedPaperclip.options[:url_with_processing],
:processing_image_url => DelayedPaperclip.options[:processing_image_url],
:queue => nil
:queue => DelayedPaperclip.options[:queue]
}.each do |option, default|

paperclip_definitions[name][:delayed][option] = options.key?(option) ? options[option] : default

end

# Sets callback
Expand Down
8 changes: 3 additions & 5 deletions lib/delayed_paperclip/jobs/active_job.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
module DelayedPaperclip
module Jobs
class ActiveJob < ActiveJob::Base
queue_as :paperclip

def self.enqueue_delayed_paperclip(instance_klass, instance_id, attachment_name)
# ActiveJob currently does not support symbol arguments
self.perform_later(instance_klass, instance_id, attachment_name.to_s)
queue_name = instance_klass.constantize.paperclip_definitions[attachment_name][:delayed][:queue]
set(:queue => queue_name).perform_later(instance_klass, instance_id, attachment_name.to_s)
end

def perform(instance_klass, instance_id, attachment_name)
DelayedPaperclip.process_job(instance_klass, instance_id, attachment_name.to_sym)
end
end
end
end
end
5 changes: 3 additions & 2 deletions lib/delayed_paperclip/jobs/delayed_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module DelayedPaperclip
module Jobs
class DelayedJob < Struct.new(:instance_klass, :instance_id, :attachment_name)

if Gem.loaded_specs['delayed_job'].version >= Gem::Version.new("2.1.0") # this is available in newer versions of DelayedJob. Using the newee Job api thus.
# This is available in newer versions of DelayedJob. Using the newee Job api thus.
if Gem.loaded_specs['delayed_job'].version >= Gem::Version.new("2.1.0")

def self.enqueue_delayed_paperclip(instance_klass, instance_id, attachment_name)
::Delayed::Job.enqueue(
Expand All @@ -31,4 +32,4 @@ def perform
end
end
end
end
end
5 changes: 2 additions & 3 deletions lib/delayed_paperclip/jobs/resque.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
module DelayedPaperclip
module Jobs
class Resque
@queue = :paperclip

def self.enqueue_delayed_paperclip(instance_klass, instance_id, attachment_name)
@queue = instance_klass.constantize.paperclip_definitions[attachment_name][:delayed][:queue]
::Resque.enqueue(self, instance_klass, instance_id, attachment_name)
end

Expand All @@ -14,4 +13,4 @@ def self.perform(instance_klass, instance_id, attachment_name)
end
end
end
end
end
8 changes: 7 additions & 1 deletion lib/delayed_paperclip/jobs/sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ module DelayedPaperclip
module Jobs
class Sidekiq
include ::Sidekiq::Worker
sidekiq_options :queue => :paperclip

def self.enqueue_delayed_paperclip(instance_klass, instance_id, attachment_name)
queue_name = instance_klass.constantize.paperclip_definitions[attachment_name][:delayed][:queue]
# Sidekiq >= 4.1.0
if respond_to?(:set)
set(:queue => queue_name)
else
sidekiq_options :queue => queue_name
end
perform_async(instance_klass, instance_id, attachment_name)
end

Expand Down
8 changes: 3 additions & 5 deletions spec/delayed_paperclip/attachment_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'spec_helper'

describe DelayedPaperclip::Attachment do

before :each do
DelayedPaperclip.options[:background_job_class] = DelayedPaperclip::Jobs::Resque
reset_dummy(dummy_options)
Expand All @@ -11,15 +10,14 @@
let(:dummy) { Dummy.create }

describe "#delayed_options" do

it "returns the specific options for delayed paperclip" do
dummy.image.delayed_options.should == {
expect(dummy.image.delayed_options).to eq({
:priority => 0,
:only_process => [],
:url_with_processing => true,
:processing_image_url => nil,
:queue => nil
}
:queue => "paperclip"
})
end
end

Expand Down
19 changes: 16 additions & 3 deletions spec/delayed_paperclip/class_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,24 @@
:only_process => [],
:url_with_processing => true,
:processing_image_url => nil,
:queue => nil}
:queue => "paperclip"}
}
}
end

it "allows to set queue name" do
Dummy.process_in_background(:image, :queue => "custom")
expect(Dummy.paperclip_definitions).to eq({ :image => {
:delayed => {
:priority => 0,
:only_process => [],
:url_with_processing => true,
:processing_image_url => nil,
:queue => "custom"}
}
})
end

context "with processing_image_url" do
before :each do
Dummy.process_in_background(:image, processing_image_url: "/processing/url")
Expand All @@ -36,7 +49,7 @@
:only_process => [],
:url_with_processing => true,
:processing_image_url => "/processing/url",
:queue => nil}
:queue => "paperclip"}
}
}
end
Expand All @@ -56,7 +69,7 @@
:only_process => [:small, :large],
:url_with_processing => true,
:processing_image_url => nil,
:queue => nil}
:queue => "paperclip"}
}
}
end
Expand Down
1 change: 1 addition & 0 deletions spec/delayed_paperclip/instance_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
it "clears instance variables" do
dummy.instance_variable_set(:@_enqued_for_processing, ['foo'])
dummy.instance_variable_set(:@_enqued_for_processing_with_processing, ['image'])
dummy.expects(:enqueue_post_processing_for).with('foo')
dummy.enqueue_delayed_processing
dummy.instance_variable_get(:@_enqued_for_processing).should == []
dummy.instance_variable_get(:@_enqued_for_processing_with_processing).should == []
Expand Down
9 changes: 5 additions & 4 deletions spec/delayed_paperclip_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
it ".options returns basic options" do
DelayedPaperclip.options.should == {:background_job_class => DelayedPaperclip::Jobs::Resque,
:url_with_processing => true,
:processing_image_url => nil}
:processing_image_url => nil,
:queue => "paperclip"}
end
end

Expand Down Expand Up @@ -60,14 +61,14 @@
end

it "returns paperclip options regardless of version" do
Dummy.paperclip_definitions.should == {:image => { :styles => { :thumbnail => "25x25" },
expect(Dummy.paperclip_definitions).to eq({:image => { :styles => { :thumbnail => "25x25" },
:delayed => { :priority => 0,
:only_process => [],
:url_with_processing => true,
:processing_image_url => nil,
:queue => nil}
:queue => "paperclip"}
}
}
})
end
end
end
4 changes: 2 additions & 2 deletions spec/integration/active_job_resque_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def process_jobs
worker.process
end

def jobs_count
Resque.size(:paperclip)
def jobs_count(queue = :paperclip)
Resque.size(queue)
end
end
end
9 changes: 3 additions & 6 deletions spec/integration/active_job_sidekiq_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
require 'spec_helper'
require 'sidekiq/api'
require 'sidekiq/testing'

describe "ActiveJob with Sidekiq backend" do
if defined? ActiveJob
before :each do
DelayedPaperclip.options[:background_job_class] = DelayedPaperclip::Jobs::ActiveJob
ActiveJob::Base.logger = nil
ActiveJob::Base.queue_adapter = :sidekiq
end

before :each do
Sidekiq::Queues["paperclip"].clear
end

Expand All @@ -31,7 +28,7 @@ def process_jobs
end
end

def jobs_count
Sidekiq::Queues["paperclip"].size
def jobs_count(queue = "paperclip")
Sidekiq::Queues[queue].size
end
end
2 changes: 1 addition & 1 deletion spec/integration/delayed_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def process_jobs
Delayed::Worker.new.work_off
end

def jobs_count
def jobs_count(queue = nil)
Delayed::Job.count
end

Expand Down
9 changes: 9 additions & 0 deletions spec/integration/examples/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,13 @@
end
end

describe "queue option" do
it "enqueues job with given queue name" do
reset_dummy :queue => "custom"

expect do
dummy.save!
end.to change { jobs_count("custom") }.by(1)
end
end
end
4 changes: 2 additions & 2 deletions spec/integration/resque_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def process_jobs
worker.process
end

def jobs_count
Resque.size(:paperclip)
def jobs_count(queue = :paperclip)
Resque.size(queue)
end
end
4 changes: 2 additions & 2 deletions spec/integration/sidekiq_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def process_jobs
end
end

def jobs_count
Sidekiq::Queues["paperclip"].size
def jobs_count(queue = "paperclip")
Sidekiq::Queues[queue].size
end
end

0 comments on commit ff68309

Please sign in to comment.