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

Commit ff68309

Browse files
author
Scott Carleton
committed
Merge pull request #169 from morgoth/set-queue-name
Allow to set queue name for background jobs
2 parents 8477e5c + c74726b commit ff68309

16 files changed

+75
-40
lines changed

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,19 @@ end
250250
@user.avatar.reprocess_without_delay!(:medium)
251251
````
252252

253+
#### Set queue name
254+
255+
You can set queue name for background job. By default it's called "paperclip".
256+
You can set it by changing global default options or by:
257+
258+
```
259+
class User < ActiveRecord::Base
260+
has_attached_file :avatar
261+
262+
process_in_background :avatar, queue: "default"
263+
end
264+
```
265+
253266
Defaults
254267
--------
255268

lib/delayed_paperclip.rb

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ def options
1111
@options ||= {
1212
:background_job_class => detect_background_task,
1313
:url_with_processing => true,
14-
:processing_image_url => nil
14+
:processing_image_url => nil,
15+
:queue => "paperclip"
1516
}
1617
end
1718

@@ -59,11 +60,9 @@ def process_in_background(name, options = {})
5960
:only_process => only_process_default,
6061
:url_with_processing => DelayedPaperclip.options[:url_with_processing],
6162
:processing_image_url => DelayedPaperclip.options[:processing_image_url],
62-
:queue => nil
63+
:queue => DelayedPaperclip.options[:queue]
6364
}.each do |option, default|
64-
6565
paperclip_definitions[name][:delayed][option] = options.key?(option) ? options[option] : default
66-
6766
end
6867

6968
# Sets callback
+3-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
module DelayedPaperclip
22
module Jobs
33
class ActiveJob < ActiveJob::Base
4-
queue_as :paperclip
5-
64
def self.enqueue_delayed_paperclip(instance_klass, instance_id, attachment_name)
7-
# ActiveJob currently does not support symbol arguments
8-
self.perform_later(instance_klass, instance_id, attachment_name.to_s)
5+
queue_name = instance_klass.constantize.paperclip_definitions[attachment_name][:delayed][:queue]
6+
set(:queue => queue_name).perform_later(instance_klass, instance_id, attachment_name.to_s)
97
end
108

119
def perform(instance_klass, instance_id, attachment_name)
1210
DelayedPaperclip.process_job(instance_klass, instance_id, attachment_name.to_sym)
1311
end
1412
end
1513
end
16-
end
14+
end

lib/delayed_paperclip/jobs/delayed_job.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ module DelayedPaperclip
44
module Jobs
55
class DelayedJob < Struct.new(:instance_klass, :instance_id, :attachment_name)
66

7-
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.
7+
# This is available in newer versions of DelayedJob. Using the newee Job api thus.
8+
if Gem.loaded_specs['delayed_job'].version >= Gem::Version.new("2.1.0")
89

910
def self.enqueue_delayed_paperclip(instance_klass, instance_id, attachment_name)
1011
::Delayed::Job.enqueue(
@@ -31,4 +32,4 @@ def perform
3132
end
3233
end
3334
end
34-
end
35+
end

lib/delayed_paperclip/jobs/resque.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
module DelayedPaperclip
44
module Jobs
55
class Resque
6-
@queue = :paperclip
7-
86
def self.enqueue_delayed_paperclip(instance_klass, instance_id, attachment_name)
7+
@queue = instance_klass.constantize.paperclip_definitions[attachment_name][:delayed][:queue]
98
::Resque.enqueue(self, instance_klass, instance_id, attachment_name)
109
end
1110

@@ -14,4 +13,4 @@ def self.perform(instance_klass, instance_id, attachment_name)
1413
end
1514
end
1615
end
17-
end
16+
end

lib/delayed_paperclip/jobs/sidekiq.rb

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@ module DelayedPaperclip
44
module Jobs
55
class Sidekiq
66
include ::Sidekiq::Worker
7-
sidekiq_options :queue => :paperclip
87

98
def self.enqueue_delayed_paperclip(instance_klass, instance_id, attachment_name)
9+
queue_name = instance_klass.constantize.paperclip_definitions[attachment_name][:delayed][:queue]
10+
# Sidekiq >= 4.1.0
11+
if respond_to?(:set)
12+
set(:queue => queue_name)
13+
else
14+
sidekiq_options :queue => queue_name
15+
end
1016
perform_async(instance_klass, instance_id, attachment_name)
1117
end
1218

spec/delayed_paperclip/attachment_spec.rb

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
require 'spec_helper'
22

33
describe DelayedPaperclip::Attachment do
4-
54
before :each do
65
DelayedPaperclip.options[:background_job_class] = DelayedPaperclip::Jobs::Resque
76
reset_dummy(dummy_options)
@@ -11,15 +10,14 @@
1110
let(:dummy) { Dummy.create }
1211

1312
describe "#delayed_options" do
14-
1513
it "returns the specific options for delayed paperclip" do
16-
dummy.image.delayed_options.should == {
14+
expect(dummy.image.delayed_options).to eq({
1715
:priority => 0,
1816
:only_process => [],
1917
:url_with_processing => true,
2018
:processing_image_url => nil,
21-
:queue => nil
22-
}
19+
:queue => "paperclip"
20+
})
2321
end
2422
end
2523

spec/delayed_paperclip/class_methods_spec.rb

+16-3
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,24 @@
1919
:only_process => [],
2020
:url_with_processing => true,
2121
:processing_image_url => nil,
22-
:queue => nil}
22+
:queue => "paperclip"}
2323
}
2424
}
2525
end
2626

27+
it "allows to set queue name" do
28+
Dummy.process_in_background(:image, :queue => "custom")
29+
expect(Dummy.paperclip_definitions).to eq({ :image => {
30+
:delayed => {
31+
:priority => 0,
32+
:only_process => [],
33+
:url_with_processing => true,
34+
:processing_image_url => nil,
35+
:queue => "custom"}
36+
}
37+
})
38+
end
39+
2740
context "with processing_image_url" do
2841
before :each do
2942
Dummy.process_in_background(:image, processing_image_url: "/processing/url")
@@ -36,7 +49,7 @@
3649
:only_process => [],
3750
:url_with_processing => true,
3851
:processing_image_url => "/processing/url",
39-
:queue => nil}
52+
:queue => "paperclip"}
4053
}
4154
}
4255
end
@@ -56,7 +69,7 @@
5669
:only_process => [:small, :large],
5770
:url_with_processing => true,
5871
:processing_image_url => nil,
59-
:queue => nil}
72+
:queue => "paperclip"}
6073
}
6174
}
6275
end

spec/delayed_paperclip/instance_methods_spec.rb

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
it "clears instance variables" do
2424
dummy.instance_variable_set(:@_enqued_for_processing, ['foo'])
2525
dummy.instance_variable_set(:@_enqued_for_processing_with_processing, ['image'])
26+
dummy.expects(:enqueue_post_processing_for).with('foo')
2627
dummy.enqueue_delayed_processing
2728
dummy.instance_variable_get(:@_enqued_for_processing).should == []
2829
dummy.instance_variable_get(:@_enqued_for_processing_with_processing).should == []

spec/delayed_paperclip_spec.rb

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
it ".options returns basic options" do
1616
DelayedPaperclip.options.should == {:background_job_class => DelayedPaperclip::Jobs::Resque,
1717
:url_with_processing => true,
18-
:processing_image_url => nil}
18+
:processing_image_url => nil,
19+
:queue => "paperclip"}
1920
end
2021
end
2122

@@ -60,14 +61,14 @@
6061
end
6162

6263
it "returns paperclip options regardless of version" do
63-
Dummy.paperclip_definitions.should == {:image => { :styles => { :thumbnail => "25x25" },
64+
expect(Dummy.paperclip_definitions).to eq({:image => { :styles => { :thumbnail => "25x25" },
6465
:delayed => { :priority => 0,
6566
:only_process => [],
6667
:url_with_processing => true,
6768
:processing_image_url => nil,
68-
:queue => nil}
69+
:queue => "paperclip"}
6970
}
70-
}
71+
})
7172
end
7273
end
7374
end

spec/integration/active_job_resque_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ def process_jobs
2121
worker.process
2222
end
2323

24-
def jobs_count
25-
Resque.size(:paperclip)
24+
def jobs_count(queue = :paperclip)
25+
Resque.size(queue)
2626
end
2727
end
2828
end

spec/integration/active_job_sidekiq_spec.rb

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
require 'spec_helper'
2-
require 'sidekiq/api'
2+
require 'sidekiq/testing'
33

44
describe "ActiveJob with Sidekiq backend" do
55
if defined? ActiveJob
66
before :each do
77
DelayedPaperclip.options[:background_job_class] = DelayedPaperclip::Jobs::ActiveJob
88
ActiveJob::Base.logger = nil
99
ActiveJob::Base.queue_adapter = :sidekiq
10-
end
11-
12-
before :each do
1310
Sidekiq::Queues["paperclip"].clear
1411
end
1512

@@ -31,7 +28,7 @@ def process_jobs
3128
end
3229
end
3330

34-
def jobs_count
35-
Sidekiq::Queues["paperclip"].size
31+
def jobs_count(queue = "paperclip")
32+
Sidekiq::Queues[queue].size
3633
end
3734
end

spec/integration/delayed_job_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def process_jobs
3333
Delayed::Worker.new.work_off
3434
end
3535

36-
def jobs_count
36+
def jobs_count(queue = nil)
3737
Delayed::Job.count
3838
end
3939

spec/integration/examples/base.rb

+9
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,13 @@
312312
end
313313
end
314314

315+
describe "queue option" do
316+
it "enqueues job with given queue name" do
317+
reset_dummy :queue => "custom"
318+
319+
expect do
320+
dummy.save!
321+
end.to change { jobs_count("custom") }.by(1)
322+
end
323+
end
315324
end

spec/integration/resque_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def process_jobs
3232
worker.process
3333
end
3434

35-
def jobs_count
36-
Resque.size(:paperclip)
35+
def jobs_count(queue = :paperclip)
36+
Resque.size(queue)
3737
end
3838
end

spec/integration/sidekiq_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def process_jobs
3939
end
4040
end
4141

42-
def jobs_count
43-
Sidekiq::Queues["paperclip"].size
42+
def jobs_count(queue = "paperclip")
43+
Sidekiq::Queues[queue].size
4444
end
4545
end

0 commit comments

Comments
 (0)