carrierwave_backgrounder 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -33,6 +33,15 @@ gem 'carrierwave_backgrounder'
33
33
 
34
34
  ## Getting Started
35
35
 
36
+ In an initializer:
37
+
38
+ ```ruby
39
+ CarrierWave::Backgrounder.configure do |c|
40
+ # :delayed_job, :girl_friday, :sidekiq, :qu, :resque, or :qc
41
+ c.backend = :delayed_job
42
+ end
43
+ ```
44
+
36
45
  In your CarrierWave uploader file:
37
46
 
38
47
  ```ruby
@@ -13,7 +13,7 @@ module CarrierWave
13
13
 
14
14
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
15
15
  def trigger_#{column}_background_processing?
16
- process_#{column}_upload != true && #{column}_changed?
16
+ process_#{column}_upload != true && (#{column}_changed? || remote_#{column}_url.present?)
17
17
  end
18
18
  RUBY
19
19
  end
@@ -23,7 +23,7 @@ module CarrierWave
23
23
 
24
24
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
25
25
  def trigger_#{column}_background_storage?
26
- process_#{column}_upload != true && #{column}_changed?
26
+ process_#{column}_upload != true && (#{column}_changed? || remote_#{column}_url.present?)
27
27
  end
28
28
  RUBY
29
29
  end
@@ -50,15 +50,7 @@ module CarrierWave
50
50
  end
51
51
 
52
52
  def enqueue_#{column}_background_job
53
- if defined? ::GirlFriday
54
- CARRIERWAVE_QUEUE << { :worker => #{worker}.new(self.class.name, id, #{column}.mounted_as) }
55
- elsif defined? ::Delayed::Job
56
- ::Delayed::Job.enqueue #{worker}.new(self.class.name, id, #{column}.mounted_as)
57
- elsif defined? ::Resque
58
- ::Resque.enqueue #{worker}, self.class.name, id, #{column}.mounted_as
59
- elsif defined? ::Qu
60
- ::Qu.enqueue #{worker}, self.class.name, id, #{column}.mounted_as
61
- end
53
+ CarrierWave::Backgrounder.enqueue_for_backend(#{worker}, self.class.name, id.to_s, #{column}.mounted_as)
62
54
  end
63
55
 
64
56
  def trigger_#{column}_background_processing?
@@ -98,7 +90,7 @@ module CarrierWave
98
90
 
99
91
  def write_#{column}_identifier
100
92
  super() and return if process_#{column}_upload
101
- self.#{column}_tmp = _mounter(:#{column}).cache_name
93
+ self.#{column}_tmp = _mounter(:#{column}).cache_name if _mounter(:#{column}).cache_name
102
94
  end
103
95
 
104
96
  def store_#{column}!
@@ -106,15 +98,7 @@ module CarrierWave
106
98
  end
107
99
 
108
100
  def enqueue_#{column}_background_job
109
- if defined? ::GirlFriday
110
- CARRIERWAVE_QUEUE << { :worker => #{worker}.new(self.class.name, id, #{column}.mounted_as) }
111
- elsif defined? ::Delayed::Job
112
- ::Delayed::Job.enqueue #{worker}.new(self.class.name, id, #{column}.mounted_as)
113
- elsif defined? ::Resque
114
- ::Resque.enqueue #{worker}, self.class.name, id, #{column}.mounted_as
115
- elsif defined? ::Qu
116
- ::Qu.enqueue #{worker}, self.class.name, id, #{column}.mounted_as
117
- end
101
+ CarrierWave::Backgrounder.enqueue_for_backend(#{worker}, self.class.name, id.to_s, #{column}.mounted_as)
118
102
  end
119
103
 
120
104
  def trigger_#{column}_background_storage?
@@ -10,7 +10,7 @@ module CarrierWave
10
10
 
11
11
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
12
12
  def trigger_#{column}_background_processing?
13
- process_#{column}_upload != true && #{column}_changed?
13
+ process_#{column}_upload != true && (#{column}_changed? || remote_#{column}_url.present?)
14
14
  end
15
15
  RUBY
16
16
  end
@@ -20,7 +20,7 @@ module CarrierWave
20
20
 
21
21
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
22
22
  def trigger_#{column}_background_storage?
23
- process_#{column}_upload != true && #{column}_changed?
23
+ process_#{column}_upload != true && (#{column}_changed? || remote_#{column}_url.present?)
24
24
  end
25
25
  RUBY
26
26
  end
@@ -30,4 +30,4 @@ module CarrierWave
30
30
  end # Backgrounder
31
31
  end # CarrierWave
32
32
 
33
- Mongoid::Document::ClassMethods.send(:include, ::CarrierWave::Backgrounder::ORM::Mongoid)
33
+ Mongoid::Document::ClassMethods.send(:include, ::CarrierWave::Backgrounder::ORM::Mongoid)
@@ -1,5 +1,5 @@
1
1
  module CarrierWave
2
2
  module Backgrounder
3
- VERSION = "0.0.7"
3
+ VERSION = "0.0.8"
4
4
  end
5
5
  end
@@ -3,24 +3,31 @@ module CarrierWave
3
3
  module Workers
4
4
 
5
5
  class ProcessAsset < Struct.new(:klass, :id, :column)
6
+ include ::Sidekiq::Worker if defined?(::Sidekiq)
6
7
  @queue = :process_asset
7
8
 
8
9
  def self.perform(*args)
9
10
  new(*args).perform
10
11
  end
11
12
 
12
- def perform
13
+ def perform(*args)
14
+ set_args(*args) unless args.empty?
13
15
  resource = klass.is_a?(String) ? klass.constantize : klass
14
16
  record = resource.find id
15
17
 
16
18
  if record
17
19
  record.send(:"process_#{column}_upload=", true)
18
20
  if record.send(:"#{column}").recreate_versions! && record.respond_to?(:"#{column}_processing")
19
- record.update_attribute :"#{column}_processing", nil
21
+ record.send :"#{column}_processing=", nil
22
+ record.save!
20
23
  end
21
24
  end
22
25
  end
23
26
 
27
+ def set_args(klass, id, column)
28
+ self.klass, self.id, self.column = klass, id, column
29
+ end
30
+
24
31
  end # ProcessAsset
25
32
 
26
33
  end # Workers
@@ -3,13 +3,15 @@ module CarrierWave
3
3
  module Workers
4
4
 
5
5
  class StoreAsset < Struct.new(:klass, :id, :column)
6
+ include ::Sidekiq::Worker if defined?(::Sidekiq)
6
7
  @queue = :store_asset
7
8
 
8
9
  def self.perform(*args)
9
10
  new(*args).perform
10
11
  end
11
12
 
12
- def perform
13
+ def perform(*args)
14
+ set_args(*args) unless args.empty?
13
15
  resource = klass.is_a?(String) ? klass.constantize : klass
14
16
  record = resource.find id
15
17
  if tmp = record.send(:"#{column}_tmp")
@@ -26,6 +28,10 @@ module CarrierWave
26
28
  end
27
29
  end
28
30
 
31
+ def set_args(klass, id, column)
32
+ self.klass, self.id, self.column = klass, id, column
33
+ end
34
+
29
35
  end # StoreAsset
30
36
 
31
37
  end # Workers
@@ -1,5 +1,6 @@
1
1
  module CarrierWave
2
2
  module Backgrounder
3
+
3
4
  Logger = Logger.new(STDOUT)
4
5
 
5
6
  autoload :Delay, 'backgrounder/delay'
@@ -8,6 +9,70 @@ module CarrierWave
8
9
  module ORM
9
10
  autoload :Base, 'backgrounder/orm/base'
10
11
  end
12
+
13
+ class << self
14
+ def backend=(value)
15
+ @backend = value
16
+ self.configure_backend
17
+ end
18
+
19
+ def backend
20
+ return @backend unless @backend.nil?
21
+ if available_backends.empty?
22
+ warn 'WARNING: No queue backends found to use for CarrierWave::Backgrounder'
23
+ elsif available_backends.size == 1
24
+ self.backend = available_backends.first
25
+ elsif available_backends.size > 1
26
+ warn 'WARNING: Multiple queue backends found for CarrierWave::Backgrounder. You need to set one explicitly.'
27
+ end
28
+ end
29
+
30
+ def configure
31
+ yield self
32
+ end
33
+
34
+ def available_backends
35
+ @available_backends ||= begin
36
+ backends = []
37
+ backends << :girl_friday if defined? ::GirlFriday
38
+ backends << :delayed_job if defined? ::Delayed::Job
39
+ backends << :resque if defined? ::Resque
40
+ backends << :qu if defined? ::Qu
41
+ backends << :sidekiq if defined? ::Sidekiq
42
+ backends << :qc if defined? ::QC
43
+ backends
44
+ end
45
+ end
46
+
47
+ def configure_backend
48
+ if backend == :girl_friday
49
+ require 'girl_friday'
50
+ @girl_friday_queue = GirlFriday::WorkQueue.new(:carrierwave) do |msg|
51
+ worker = msg[:worker]
52
+ worker.perform
53
+ end
54
+ end
55
+ end
56
+
57
+ def enqueue_for_backend(worker, class_name, subject_id, mounted_as)
58
+ case backend
59
+ when :girl_friday
60
+ @girl_friday_queue << { :worker => worker.new(self.class.name, subject_id, mounted_as) }
61
+ when :delayed_job
62
+ ::Delayed::Job.enqueue worker.new(class_name, subject_id, mounted_as)
63
+ when :resque
64
+ ::Resque.enqueue worker, class_name, subject_id, mounted_as
65
+ when :qu
66
+ ::Qu.enqueue worker, class_name, subject_id, column.mounted_as
67
+ when :sidekiq
68
+ ::Sidekiq::Client.enqueue worker, class_name, subject_id, mounted_as
69
+ when :qc
70
+ ::QC.enqueue "#{worker.name}.perform", class_name, subject_id, mounted_as.to_s
71
+ end
72
+ end
73
+
74
+ end
75
+
11
76
  end
12
77
  end
13
78
 
@@ -35,11 +100,3 @@ if defined?(Rails)
35
100
  end
36
101
  end
37
102
 
38
- if defined?(GirlFriday)
39
- require 'girl_friday'
40
-
41
- CARRIERWAVE_QUEUE = GirlFriday::WorkQueue.new(:carrierwave) do |msg|
42
- worker = msg[:worker]
43
- worker.perform
44
- end
45
- end
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+ require 'carrierwave_backgrounder'
3
+
4
+ describe CarrierWave::Backgrounder do
5
+
6
+ describe 'enumerating available backends' do
7
+
8
+ it 'detects GirlFriday' do
9
+ CarrierWave::Backgrounder.available_backends.should include(:girl_friday)
10
+ end
11
+ it 'detects Delayed::Job' do
12
+ CarrierWave::Backgrounder.available_backends.should include(:delayed_job)
13
+ end
14
+ it 'detects Resque' do
15
+ CarrierWave::Backgrounder.available_backends.should include(:resque)
16
+ end
17
+ it 'detects Qu' do
18
+ CarrierWave::Backgrounder.available_backends.should include(:qu)
19
+ end
20
+ it 'detects Sidekiq' do
21
+ CarrierWave::Backgrounder.available_backends.should include(:sidekiq)
22
+ end
23
+ it 'detects QC' do
24
+ CarrierWave::Backgrounder.available_backends.should include(:qc)
25
+ end
26
+ end
27
+
28
+ describe 'automatically setting backends' do
29
+
30
+ before do
31
+ CarrierWave::Backgrounder.instance_variable_set('@backend', nil)
32
+ end
33
+
34
+ it 'does not set a backend if none are available' do
35
+ suppress_warnings do
36
+ CarrierWave::Backgrounder.stubs(:available_backends).returns([])
37
+ CarrierWave::Backgrounder.backend.should be_nil
38
+ end
39
+ end
40
+ it 'sets a backend automatically if only one is available' do
41
+ CarrierWave::Backgrounder.stubs(:available_backends).returns([ :qu ])
42
+ CarrierWave::Backgrounder.backend.should eq(:qu)
43
+ end
44
+ it 'does not set a backend if more than one is available' do
45
+ suppress_warnings do
46
+ CarrierWave::Backgrounder.stubs(:available_backends).returns([:qu, :resque])
47
+ CarrierWave::Backgrounder.backend.should be_nil
48
+ end
49
+ end
50
+
51
+ it 'does not clobber a manually set backend' do
52
+ CarrierWave::Backgrounder.backend = :not_a_backend
53
+ CarrierWave::Backgrounder.backend.should eq(:not_a_backend)
54
+ end
55
+
56
+ it 'calls configure_backend when setting the backend' do
57
+ CarrierWave::Backgrounder.stubs(:available_backends).returns([ :qu ])
58
+ CarrierWave::Backgrounder.expects(:configure_backend).once
59
+ CarrierWave::Backgrounder.backend.should eq(:qu)
60
+ end
61
+
62
+ end
63
+
64
+
65
+
66
+ end
67
+
@@ -19,15 +19,27 @@ describe CarrierWave::Workers::ProcessAsset do
19
19
  end
20
20
 
21
21
  context "#perform" do
22
- it 'processes versions' do
22
+ it 'processes versions with image_processing column' do
23
23
  user.expects(:find).with('22').returns(user).once
24
24
  user.expects(:image).once.returns(image)
25
25
  user.expects(:process_image_upload=).with(true).once
26
26
 
27
27
  image.expects(:recreate_versions!).once.returns(true)
28
28
  user.expects(:respond_to?).with(:image_processing).once.returns(true)
29
- user.expects(:update_attribute).with(:image_processing, nil).once
29
+ user.expects(:image_processing=).with(nil).once
30
+ user.expects(:save!)
31
+ worker.perform
32
+ end
30
33
 
34
+ it 'processes versions without image_processing column' do
35
+ user.expects(:find).with('22').returns(user).once
36
+ user.expects(:image).once.returns(image)
37
+ user.expects(:process_image_upload=).with(true).once
38
+
39
+ image.expects(:recreate_versions!).once.returns(true)
40
+ user.expects(:respond_to?).with(:image_processing).once.returns(false)
41
+ user.expects(:image_processing=).never
42
+ user.expects(:save!).never
31
43
  worker.perform
32
44
  end
33
45
  end
@@ -1,7 +1,21 @@
1
1
  # encoding: utf-8
2
2
  require 'rubygems'
3
3
  require 'bundler/setup'
4
+ require 'support/backend_constants'
5
+ require 'logger'
6
+
7
+ module WarningSuppression
8
+ def suppress_warnings
9
+ original_verbosity = $VERBOSE
10
+ $VERBOSE = nil
11
+ result = yield
12
+ $VERBOSE = original_verbosity
13
+ return result
14
+ end
15
+ end
4
16
 
5
17
  RSpec.configure do |c|
6
18
  c.mock_with :mocha
7
- end
19
+ c.include WarningSuppression
20
+ end
21
+
@@ -0,0 +1,23 @@
1
+ # Fixture module declarations for backend detection testing
2
+
3
+ module GirlFriday
4
+ end
5
+
6
+ module Delayed
7
+ module Job
8
+ end
9
+ end
10
+
11
+ module Resque
12
+ end
13
+
14
+ module Qu
15
+ end
16
+
17
+ module Sidekiq
18
+ module Worker
19
+ end
20
+ end
21
+
22
+ module QC
23
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carrierwave_backgrounder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-30 00:00:00.000000000 Z
12
+ date: 2012-10-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: carrierwave
@@ -98,10 +98,12 @@ files:
98
98
  - lib/backgrounder/workers/process_asset.rb
99
99
  - lib/backgrounder/workers/store_asset.rb
100
100
  - lib/carrierwave_backgrounder.rb
101
+ - spec/backgrounder/backends_spec.rb
102
+ - spec/backgrounder/workers/fixtures/test.jpg
103
+ - spec/backgrounder/workers/process_asset_spec.rb
104
+ - spec/backgrounder/workers/store_asset_spec.rb
101
105
  - spec/spec_helper.rb
102
- - spec/workers/fixtures/test.jpg
103
- - spec/workers/process_asset_spec.rb
104
- - spec/workers/store_asset_spec.rb
106
+ - spec/support/backend_constants.rb
105
107
  homepage: ''
106
108
  licenses: []
107
109
  post_install_message:
@@ -114,21 +116,29 @@ required_ruby_version: !ruby/object:Gem::Requirement
114
116
  - - ! '>='
115
117
  - !ruby/object:Gem::Version
116
118
  version: '0'
119
+ segments:
120
+ - 0
121
+ hash: -3222022521039944985
117
122
  required_rubygems_version: !ruby/object:Gem::Requirement
118
123
  none: false
119
124
  requirements:
120
125
  - - ! '>='
121
126
  - !ruby/object:Gem::Version
122
127
  version: '0'
128
+ segments:
129
+ - 0
130
+ hash: -3222022521039944985
123
131
  requirements: []
124
132
  rubyforge_project: carrierwave_backgrounder
125
- rubygems_version: 1.8.21
133
+ rubygems_version: 1.8.24
126
134
  signing_key:
127
135
  specification_version: 3
128
136
  summary: Offload CarrierWave's image processing and storage to a background process
129
137
  using Delayed Job
130
138
  test_files:
139
+ - spec/backgrounder/backends_spec.rb
140
+ - spec/backgrounder/workers/fixtures/test.jpg
141
+ - spec/backgrounder/workers/process_asset_spec.rb
142
+ - spec/backgrounder/workers/store_asset_spec.rb
131
143
  - spec/spec_helper.rb
132
- - spec/workers/fixtures/test.jpg
133
- - spec/workers/process_asset_spec.rb
134
- - spec/workers/store_asset_spec.rb
144
+ - spec/support/backend_constants.rb