carrierwave_backgrounder 0.4.2 → 1.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +2 -1
  3. data/.travis.yml +8 -2
  4. data/CHANGELOG.md +20 -0
  5. data/Gemfile +1 -1
  6. data/README.md +70 -44
  7. data/carrierwave_backgrounder.gemspec +7 -2
  8. data/lib/backgrounder/orm/activemodel.rb +7 -7
  9. data/lib/backgrounder/orm/base.rb +19 -13
  10. data/lib/backgrounder/orm/data_mapper.rb +1 -1
  11. data/lib/backgrounder/support/backends.rb +4 -44
  12. data/lib/backgrounder/version.rb +1 -1
  13. data/lib/backgrounder/workers/active_job/process_asset.rb +14 -0
  14. data/lib/backgrounder/workers/active_job/store_asset.rb +14 -0
  15. data/lib/backgrounder/workers/base.rb +15 -7
  16. data/lib/backgrounder/workers/class_methods.rb +14 -0
  17. data/lib/backgrounder/workers/process_asset.rb +2 -13
  18. data/lib/backgrounder/workers/process_asset_mixin.rb +37 -0
  19. data/lib/backgrounder/workers/store_asset.rb +2 -26
  20. data/lib/backgrounder/workers/store_asset_mixin.rb +38 -0
  21. data/lib/backgrounder/workers.rb +3 -0
  22. data/lib/carrierwave_backgrounder.rb +11 -10
  23. data/lib/generators/carrierwave_backgrounder/install_generator.rb +1 -1
  24. data/lib/generators/carrierwave_backgrounder/templates/config/initializers/carrierwave_backgrounder.rb +1 -6
  25. data/spec/backgrounder/orm/activemodel_spec.rb +2 -5
  26. data/spec/backgrounder/support/backends_spec.rb +9 -158
  27. data/spec/backgrounder/workers/process_asset_spec.rb +8 -6
  28. data/spec/backgrounder/workers/store_asset_spec.rb +20 -99
  29. data/spec/integrations/process_in_background_multi_upload_spec.rb +81 -0
  30. data/spec/integrations/process_in_background_spec.rb +75 -0
  31. data/spec/integrations/store_in_background_multi_upload_spec.rb +87 -0
  32. data/spec/integrations/store_in_background_spec.rb +86 -0
  33. data/spec/rails_helper.rb +27 -0
  34. data/spec/spec_helper.rb +2 -0
  35. data/spec/support/dummy_app/Gemfile +49 -0
  36. data/spec/support/dummy_app/README.md +24 -0
  37. data/spec/support/dummy_app/Rakefile +6 -0
  38. data/spec/support/dummy_app/app/controllers/application_controller.rb +2 -0
  39. data/spec/support/dummy_app/app/controllers/concerns/.keep +0 -0
  40. data/spec/support/dummy_app/app/jobs/application_job.rb +7 -0
  41. data/spec/support/dummy_app/app/models/admin.rb +10 -0
  42. data/spec/support/dummy_app/app/models/application_record.rb +3 -0
  43. data/spec/support/dummy_app/app/models/concerns/.keep +0 -0
  44. data/spec/support/dummy_app/app/models/user.rb +10 -0
  45. data/spec/support/dummy_app/app/uploaders/avatar_uploader.rb +67 -0
  46. data/spec/support/dummy_app/bin/bundle +114 -0
  47. data/spec/support/dummy_app/bin/rails +4 -0
  48. data/spec/support/dummy_app/bin/rake +4 -0
  49. data/spec/support/dummy_app/bin/setup +33 -0
  50. data/spec/support/dummy_app/config/application.rb +39 -0
  51. data/spec/support/dummy_app/config/boot.rb +3 -0
  52. data/spec/support/dummy_app/config/database.yml +25 -0
  53. data/spec/support/dummy_app/config/environment.rb +8 -0
  54. data/spec/support/dummy_app/config/environments/development.rb +65 -0
  55. data/spec/support/dummy_app/config/environments/production.rb +86 -0
  56. data/spec/support/dummy_app/config/environments/test.rb +61 -0
  57. data/spec/support/dummy_app/config/initializers/carrierwave_backgrounder.rb +4 -0
  58. data/spec/support/dummy_app/config/initializers/cors.rb +16 -0
  59. data/spec/support/dummy_app/config/initializers/filter_parameter_logging.rb +8 -0
  60. data/spec/support/dummy_app/config/initializers/inflections.rb +16 -0
  61. data/spec/support/dummy_app/config/locales/en.yml +33 -0
  62. data/spec/support/dummy_app/config/routes.rb +6 -0
  63. data/spec/support/dummy_app/config/storage.yml +34 -0
  64. data/spec/support/dummy_app/config.ru +6 -0
  65. data/spec/support/dummy_app/db/migrate/20230804214459_create_users.rb +9 -0
  66. data/spec/support/dummy_app/db/migrate/20230807165013_add_avatar_tmp_column_to_user.rb +5 -0
  67. data/spec/support/dummy_app/db/migrate/20230808233036_add_avatar_processing_flag_to_users.rb +5 -0
  68. data/spec/support/dummy_app/db/migrate/20230809215320_create_admins.rb +10 -0
  69. data/spec/support/dummy_app/db/migrate/20230810182011_add_images_to_users.rb +7 -0
  70. data/spec/support/dummy_app/db/migrate/20230811155811_add_images_to_admins.rb +6 -0
  71. data/spec/support/dummy_app/db/schema.rb +34 -0
  72. data/spec/support/dummy_app/db/seeds.rb +7 -0
  73. data/spec/support/dummy_app/lib/tasks/.keep +0 -0
  74. data/spec/support/dummy_app/log/.keep +0 -0
  75. data/spec/support/dummy_app/public/robots.txt +1 -0
  76. data/spec/support/dummy_app/storage/.keep +0 -0
  77. data/spec/support/dummy_app/tmp/.keep +0 -0
  78. data/spec/support/dummy_app/tmp/development_secret.txt +1 -0
  79. data/spec/support/dummy_app/tmp/images/.gitkeep +0 -0
  80. data/spec/support/dummy_app/tmp/pids/.keep +0 -0
  81. data/spec/support/dummy_app/tmp/storage/.keep +0 -0
  82. data/spec/support/dummy_app/vendor/.keep +0 -0
  83. data/spec/support/fixtures/images/test-1.jpg +0 -0
  84. data/spec/support/fixtures/images/test-2.jpg +0 -0
  85. data/spec/support/global_macros.rb +23 -0
  86. data/spec/support/mock_worker.rb +2 -0
  87. metadata +214 -18
  88. data/spec/support/backend_constants.rb +0 -58
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: ea7ccc20dfbd12f0d5ca49a22aed4df13af757fe
4
- data.tar.gz: 9483744462a3c7de8ec21c01c033fe5c7722c1b7
2
+ SHA256:
3
+ metadata.gz: eda9969a160f0cfc9714cb3643586d4e870b84ef63cedbba52272b2e34eef174
4
+ data.tar.gz: ba9f80b446ada55016553a08b31b3660d279691d59d1397f975c5a356ad26584
5
5
  SHA512:
6
- metadata.gz: a9ccbcd89a9f510723a6f512197cc3595ffe4bfa00a9ea21c3c1e6b2530fee1901b47670d1bf0e6e44427d40fd71c83064eab553f09c7e1d0b75cb3283d0291e
7
- data.tar.gz: 437d6b11a142e94ae83699672299bf1a3da800846f4465aa557027bb4026970619ee425b6fc63ca7e5e11687a9dfe417c08213ddbc9300ed826a8361a95f6e61
6
+ metadata.gz: 833f6072005f58a56b2d25d7cd19efd8598df6f98f66607212ed1eee7f4df4f6c46432ca8ca65dae34a348f27567007ef7f4e38459bb522bc0fb391495699441
7
+ data.tar.gz: 90365ff21d412eeb47da2ce084b93ad4eea0287592d472d326e4dcaabf0a50f0b2ebe9e0b172bb7bec3f57d099e8689d41393a5efa9fc0b2d5cae0902b2493f1
data/.gitignore CHANGED
@@ -1,5 +1,6 @@
1
1
  *.gem
2
2
  .bundle
3
+ *.sqlite3
3
4
  Gemfile.lock
4
5
  pkg/*
5
- vendor/*
6
+ vendor/*
data/.travis.yml CHANGED
@@ -1,10 +1,16 @@
1
1
  language: ruby
2
2
 
3
3
  rvm:
4
- - 1.9.3
5
- - 2.0.0
4
+ - 2.7
5
+ - 3.0
6
+ - 3.1
7
+ - 3.2
6
8
  - ruby-head
7
9
 
10
+ before_install:
11
+ - yes | gem update --system --force
12
+ - gem install bundler
13
+
8
14
  matrix:
9
15
  allow_failures:
10
16
  - rvm: ruby-head
data/CHANGELOG.md CHANGED
@@ -1,3 +1,23 @@
1
+ ## 1.0.0-beta
2
+
3
+ ### enhancements
4
+ * Add native support for ActiveJob [lardawge]
5
+ * Add support for multi-upload [lardawge]
6
+ * Add rails app for testing so we can replicate real world conditions [lardawge]
7
+
8
+ ### bugfixes
9
+ * Fix issue where a new job would be queued when a upload was removed
10
+
11
+ ### breaking changes
12
+ * Remove support for Rails prior to after_commit
13
+ * Remove support for queueing systems other than ActiveJob and Sidekiq.
14
+ This will simplify support and offload the heavy lifting to ActiveJob
15
+
16
+ ## 0.4.3
17
+
18
+ ### enhancements
19
+ * [#307] Add support for Sidekiq 7 [holstvoogd]
20
+ * [#278] Add sidekiq queue config [IlkhamGaysin]
1
21
 
2
22
  ## 0.4.2
3
23
 
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in carrierwave_backgrounder.gemspec
4
- gemspec
4
+ gemspec
data/README.md CHANGED
@@ -1,13 +1,17 @@
1
1
  # CarrierWave Backgrounder
2
2
 
3
- [![Build Status](https://secure.travis-ci.org/lardawge/carrierwave_backgrounder.png)](http://travis-ci.org/lardawge/carrierwave_backgrounder)
3
+ [![Build Status](https://app.travis-ci.com/lardawge/carrierwave_backgrounder.svg?branch=master)](https://app.travis-ci.com/lardawge/carrierwave_backgrounder)
4
4
  [![Code Climate](https://codeclimate.com/github/lardawge/carrierwave_backgrounder.png)](https://codeclimate.com/github/lardawge/carrierwave_backgrounder)
5
- [![Still Maintained](http://stillmaintained.com/lardawge/carrierwave_backgrounder.png)](http://stillmaintained.com/lardawge/carrierwave_backgrounder)
6
5
 
7
- I like CarrierWave. That being said, I don't like tying up app instances waiting for images to process.
6
+ NOTICE: Version 1.0.0 contains breaking changes if you are coming from an earlier version.
7
+ The most notible change is the removal of queue backend options other than active_job and sidekiq.
8
+ If you are using other backends, switch over to active_job which should support your preference.
9
+ If you are using Sidekiq, there is nothing to change.
8
10
 
9
- This gem addresses that by offloading processing or storage/processing to a background task.
10
- We currently support Delayed Job, Resque, Sidekiq, SuckerPunch, Girl Friday, Qu, and Queue Classic.
11
+ I am a fan of CarrierWave. That being said, I don't like tying up requests waiting for images to process.
12
+
13
+ This gem addresses that by offloading processing or storaging/processing to a background task.
14
+ We currently support ActiveJob and Sidekiq.
11
15
 
12
16
  ## Background options
13
17
 
@@ -16,18 +20,17 @@ There are currently two offerings for backgrounding upload tasks which are as fo
16
20
  ```ruby
17
21
  # This stores the original file with no processing/versioning.
18
22
  # It will upload the original file to s3.
19
- # This was developed to use where you do not have control over the cache location such as Heroku.
23
+ # This was developed to use in cases where you do not have access to the cached location such as Heroku.
20
24
 
21
25
  Backgrounder::ORM::Base::process_in_background
22
26
  ```
23
27
 
24
28
  ```ruby
25
- # This does nothing to the file after it is cached which makes it super fast.
29
+ # This does nothing to the file after it is cached which makes it fast.
26
30
  # It requires a column in the database which stores the cache location set by carrierwave so the background job can access it.
27
31
  # The drawback to using this method is the need for a central location to store the cached files.
28
- # Heroku may deploy workers on separate servers from where your dyno cached the files.
29
32
  #
30
- # IMPORTANT: Only use this method if you have full control over your tmp storage directory.
33
+ # IMPORTANT: Only use this method if you have full control over your tmp storage directory and can mount it on every deployed server.
31
34
 
32
35
  Backgrounder::ORM::Base::store_in_background
33
36
  ```
@@ -36,6 +39,8 @@ Backgrounder::ORM::Base::store_in_background
36
39
 
37
40
  These instructions assume you have previously set up [CarrierWave](https://github.com/jnicklas/carrierwave) and your queuing lib of choice.
38
41
 
42
+ Take a look at the [Rails app](spec/support/dummy_app) to see examples of setup.
43
+
39
44
  In Rails, add the following your Gemfile:
40
45
 
41
46
  ```ruby
@@ -44,23 +49,41 @@ gem 'carrierwave_backgrounder'
44
49
 
45
50
  Run the generator which will create an initializer in config/initializers.
46
51
  ```bash
47
- rails g carrierwave_backgrounder:install
52
+ rails g carrierwave_backgrounder:install
48
53
  ```
49
54
 
50
- You can pass additional configuration options to Girl Friday and Sidekiq:
55
+ You can pass additional configuration options to Sidekiq:
51
56
 
52
57
  ```ruby
53
58
  CarrierWave::Backgrounder.configure do |c|
54
- c.backend :girl_friday, queue: :awesome_queue, size: 3, store: GirlFriday::Store::Redis
59
+ c.backend :sidekiq, queue: :awesome_queue, size: 3
55
60
  end
56
61
  ```
57
62
 
58
- In your CarrierWave uploader file:
63
+ **IMPORTANT FOR SIDEKIQ BACKEND** - carrierwave (default queue name) should be added to your queue list or it will not run:
64
+
65
+ ```yml
66
+ :queues:
67
+ - [carrierwave, 1]
68
+ - default
69
+ ```
70
+
71
+ In your CarrierWave uploader file you will need to add a cache directory as well as change cache_storage to File:
59
72
 
60
73
  ```ruby
61
74
  class AvatarUploader < CarrierWave::Uploader::Base
62
75
  include ::CarrierWave::Backgrounder::Delay
63
76
 
77
+ # This is required if you are using S3 or any other remote storage.
78
+ # CarrierWave's default is to store the cached file remotely which is slow and uses bandwidth.
79
+ # By setting this to File, it will only store on saving of the record.
80
+ cache_storage CarrierWave::Storage::File
81
+
82
+ # It is recommended to set this to a persisted location if you are using `::store_in_background`.
83
+ def cache_dir
84
+ "path/that/persists"
85
+ end
86
+
64
87
  #etc...
65
88
  end
66
89
  ```
@@ -75,7 +98,9 @@ process_in_background :avatar
75
98
  ```
76
99
 
77
100
  Optionally you can add a column to the database which will be set to `true` when
78
- the background processing is started and to `false` when the background processing is complete.
101
+ the background processing has started and to `false` when the background processing is complete.
102
+
103
+ This is set to true in the after_commit hook when the job is created. It is very useful if you are waiting to notify the user of completion.
79
104
 
80
105
  ```ruby
81
106
  add_column :users, :avatar_processing, :boolean, null: false, default: false
@@ -90,7 +115,7 @@ mount_uploader :avatar, AvatarUploader
90
115
  store_in_background :avatar
91
116
  ```
92
117
 
93
- Add a column to the model you want to background which will store the temp file location:
118
+ Add a column to the model which will store the temp file location:
94
119
 
95
120
  ```ruby
96
121
  add_column :users, :avatar_tmp, :string
@@ -108,23 +133,37 @@ If you need to process/store the upload immediately:
108
133
  This must be set before you assign an upload:
109
134
 
110
135
  ```ruby
111
- # In a controller
112
136
  @user = User.new
113
137
  @user.process_avatar_upload = true
114
138
  @user.attributes = params[:user]
115
139
  ```
116
140
 
117
141
  ### Override worker
118
- To override the worker in cases where additional methods need to be called or you have app specific requirements, pass the worker class as the
119
- second argument:
142
+ To override the worker in cases where additional methods need to be called or you have app specific requirements, pass the worker class as the second argument:
120
143
 
121
144
  ```ruby
122
145
  process_in_background :avatar, MyParanoidWorker
123
146
  ```
124
147
 
125
- Then create a worker that subclasses carrierwave_backgrounder's worker:
148
+ Then create a worker that subclasses carrierwave_backgrounder's worker.
149
+ Each method, #store_in_background and #process_in_background has there own worker.
150
+
151
+ #### For Sidekiq
152
+ `process_in_background` subclass `::CarrierWave::Workers::ProcessAsset`
153
+ `store_in_background` subclass `::CarrierWave::Workers::StoreAsset`
154
+
155
+ #### For ActiveJob
156
+ `process_in_background` subclass `::CarrierWave::Workers::ActiveJob::ProcessAsset`
157
+ `store_in_background` subclass `::CarrierWave::Workers::ActiveJob::StoreAsset`
126
158
 
127
159
  ```ruby
160
+ # Sidekiq Example
161
+
162
+ class User < ActiveRecord::Base
163
+ mount_uploader :avatar, AvatarUploader
164
+ process_in_background, :avatar, MyParanoidWorker
165
+ end
166
+
128
167
  class MyParanoidWorker < ::CarrierWave::Workers::ProcessAsset
129
168
  # ...or subclass CarrierWave::Workers::StoreAsset if you're using store_in_background
130
169
 
@@ -135,40 +174,27 @@ class MyParanoidWorker < ::CarrierWave::Workers::ProcessAsset
135
174
  # other hooks you might care about
136
175
  end
137
176
  ```
138
- ### Testing with Rspec
139
- We use the after_commit hook when using active_record. This creates a problem when testing with Rspec because after_commit never gets fired
140
- if you're using trasactional fixtures. One solution to the problem is to use the [TestAfterCommit gem](https://github.com/grosser/test_after_commit).
141
- There are various other solutions in which case google is your friend.
142
-
143
- ### Uploaders mounted on mongoid embedded documents
144
- The workers fetch the document with the mounted uploader using the model class name and id. Uploads on embedded documents
145
- cannot be obtained this way. If the position of the document in the root document structure is known, a workaround is to override the embedded models
146
- find method like this:
147
177
 
148
178
  ```ruby
149
- class SomeRootDocument
150
- include Mongoid::Document
179
+ # ActiveJob Example
151
180
 
152
- embeds_many :embedded_documents
181
+ class User < ActiveRecord::Base
182
+ mount_uploader :avatar, AvatarUploader
183
+ process_in_background, :avatar, MyActiveJobWorker
153
184
  end
154
185
 
155
- class EmbeddedDocument
156
- include Mongoid::Document
157
-
158
- embedded_in :some_root_document
159
-
160
- mount_uploader :image, ImageUploader
161
- process_in_background :image
162
-
163
- def self.find(id)
164
- bson_id = Moped::BSON::ObjectId.from_string(id) # needed for Mongoid 3
165
-
166
- root = SomeRootDocument.where('embedded_documents._id' => bson_id).first
167
- root.embedded_documents.find(id)
186
+ class MyActiveJobWorker < ::CarrierWave::Workers::ActiveJob::StoreAsset
187
+ after_perform do
188
+ # your code here
168
189
  end
169
190
  end
170
191
  ```
171
192
 
193
+ ### Testing with Rspec
194
+ We use the after_commit hook when using active_record. This creates a problem when testing with Rspec because after_commit never gets fired
195
+ if you're using transactional fixtures. One solution to the problem is to use the [TestAfterCommit gem](https://github.com/grosser/test_after_commit).
196
+ There are various other solutions in which case google is your friend.
197
+
172
198
  ## License
173
199
 
174
200
  Copyright (c) 2011 Larry Sprock
@@ -17,8 +17,13 @@ Gem::Specification.new do |s|
17
17
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
18
  s.require_paths = ["lib"]
19
19
 
20
- s.add_dependency "carrierwave", ["~> 0.5"]
20
+ s.add_dependency "carrierwave", ["> 2.0", "< 4.0"]
21
+ s.add_dependency "rails", ["> 6.0", "< 8.0"]
21
22
 
22
- s.add_development_dependency "rspec", ["~> 3.1.0"]
23
+ s.add_development_dependency "rspec", ["~> 3.12"]
23
24
  s.add_development_dependency "rake"
25
+ s.add_development_dependency "rspec-rails"
26
+ s.add_development_dependency "sqlite3"
27
+ s.add_development_dependency "sidekiq"
28
+ s.add_development_dependency "pry"
24
29
  end
@@ -10,8 +10,8 @@ module CarrierWave
10
10
  private
11
11
 
12
12
  def _define_shared_backgrounder_methods(mod, column, worker)
13
- before_save :"set_#{column}_processing", :if => :"enqueue_#{column}_background_job?"
14
- send _supported_callback, :"enqueue_#{column}_background_job", :if => :"enqueue_#{column}_background_job?"
13
+ before_save :"set_#{column}_processing", if: :"enqueue_#{column}_background_job?"
14
+ after_commit :"enqueue_#{column}_background_job", if: :"enqueue_#{column}_background_job?"
15
15
 
16
16
  super
17
17
 
@@ -19,15 +19,15 @@ module CarrierWave
19
19
  options = self.class.uploader_options[column] || {}
20
20
  serialization_column = options[:mount_on] || column
21
21
 
22
- send(:"#{serialization_column}_changed?") || # after_save support
23
22
  previous_changes.has_key?(:"#{serialization_column}") || # after_commit support
24
- send(:"remote_#{column}_url").present? || # Remote upload support
23
+ remote_url_present? || # Remote upload support
25
24
  send(:"#{column}_cache").present? # Form failure support
26
25
  end
27
- end
28
26
 
29
- def _supported_callback
30
- respond_to?(:after_commit) ? :after_commit : :after_save
27
+ define_method :remote_url_present? do
28
+ !!(send(:"remote_#{column}_url").present? if respond_to?(:"remote_#{column}_url")) || # Remote upload support for a single file
29
+ !!(send(:"remote_#{column}_urls").present? if respond_to?(:"remote_#{column}_urls")) # Remote upload support for multiple files
30
+ end
31
31
  end
32
32
  end # ActiveModel
33
33
 
@@ -35,16 +35,17 @@ module CarrierWave
35
35
  # which can be used to check if processing is complete.
36
36
  #
37
37
  # def self.up
38
- # add_column :users, :avatar_processing, :boolean
38
+ # add_column :users, :avatar_processing, :boolean, null: false, default: false
39
39
  # end
40
40
  #
41
- def process_in_background(column, worker=::CarrierWave::Workers::ProcessAsset)
41
+ def process_in_background(column, worker_klass=nil)
42
42
  attr_accessor :"process_#{column}_upload"
43
43
 
44
+ worker = worker_klass || "#{CarrierWave::Backgrounder.worker_klass}::ProcessAsset"
44
45
  mod = Module.new
45
46
  include mod
46
47
 
47
- _define_shared_backgrounder_methods(mod, column, worker)
48
+ _define_shared_backgrounder_methods(mod, column, worker )
48
49
  end
49
50
 
50
51
  ##
@@ -69,20 +70,17 @@ module CarrierWave
69
70
  # store_in_background :avatar, CustomWorker
70
71
  # end
71
72
  #
72
- def store_in_background(column, worker=::CarrierWave::Workers::StoreAsset)
73
+ def store_in_background(column, worker_klass=nil)
73
74
  attr_accessor :"process_#{column}_upload"
74
75
 
76
+ worker = worker_klass || "#{CarrierWave::Backgrounder.worker_klass}::StoreAsset"
77
+
75
78
  mod = Module.new
76
79
  include mod
77
80
  mod.class_eval <<-RUBY, __FILE__, __LINE__ + 1
78
- def remove_#{column}=(value)
79
- super
80
- self.process_#{column}_upload = true
81
- end
82
-
83
81
  def write_#{column}_identifier
84
- super and return if process_#{column}_upload
85
- self.#{column}_tmp = _mounter(:#{column}).cache_name if _mounter(:#{column}).cache_name
82
+ super and return if process_#{column}_upload || remove_#{column}
83
+ self.#{column}_tmp = #{column}_cache if #{column}_cache
86
84
  end
87
85
 
88
86
  def store_#{column}!
@@ -105,11 +103,19 @@ module CarrierWave
105
103
  end
106
104
 
107
105
  def enqueue_#{column}_background_job?
108
- !remove_#{column}? && !process_#{column}_upload && #{column}_updated?
106
+ !remove_#{column}? && !process_#{column}_upload && #{column}_present? && #{column}_updated?
107
+ end
108
+
109
+ def #{column}_mounted_as
110
+ #{column}.is_a?(Array) ? #{column}.first.mounted_as : #{column}.mounted_as
111
+ end
112
+
113
+ def #{column}_present?
114
+ #{column}.is_a?(Array) ? #{column}.present? : #{column}.file.present?
109
115
  end
110
116
 
111
117
  def enqueue_#{column}_background_job
112
- CarrierWave::Backgrounder.enqueue_for_backend(#{worker}, self.class.name, id.to_s, #{column}.mounted_as)
118
+ CarrierWave::Backgrounder.enqueue_for_backend(#{worker}, self.class.name, id.to_s, #{column}_mounted_as)
113
119
  end
114
120
  RUBY
115
121
  end
@@ -26,7 +26,7 @@ module CarrierWave
26
26
 
27
27
  def write_#{column}_identifier
28
28
  super and return if process_#{column}_upload
29
- self.#{column}_tmp = _mounter(:#{column}).cache_name
29
+ self.#{column}_tmp = #{column}_cache
30
30
  end
31
31
  RUBY
32
32
  end
@@ -22,57 +22,17 @@ module CarrierWave
22
22
 
23
23
  private
24
24
 
25
- def enqueue_delayed_job(worker, *args)
26
- worker_args = {}
27
- if ::Delayed::Job.new.respond_to?(:queue)
28
- worker_args[:queue] = queue_options[:queue] if queue_options[:queue]
29
- worker_args[:priority] = queue_options[:priority] if queue_options[:priority]
30
- ::Delayed::Job.enqueue worker.new(*args), worker_args
31
- else
32
- worker_args[:priority] = queue_options[:priority] if queue_options[:priority]
33
- ::Delayed::Job.enqueue worker.new(*args), worker_args
34
- if queue_options[:queue]
35
- ::Rails.logger.warn("Queue name given but no queue column exists for Delayed::Job")
36
- end
37
- end
38
- end
39
-
40
- def enqueue_resque(worker, *args)
41
- worker.instance_variable_set('@queue', queue_options[:queue] || :carrierwave)
42
- ::Resque.enqueue worker, *args
25
+ def enqueue_active_job(worker, *args)
26
+ worker.perform_later(*args.map(&:to_s))
43
27
  end
44
28
 
45
29
  def enqueue_sidekiq(worker, *args)
46
30
  override_queue_name = worker.sidekiq_options['queue'] == 'default' || worker.sidekiq_options['queue'].nil?
47
- args = sidekiq_queue_options(override_queue_name, 'class' => worker, 'args' => args)
31
+ args = sidekiq_queue_options(override_queue_name, 'class' => worker, 'args' => args.map(&:to_s))
48
32
  worker.client_push(args)
49
33
  end
50
34
 
51
- def enqueue_girl_friday(worker, *args)
52
- @girl_friday_queue ||= GirlFriday::WorkQueue.new(queue_options.delete(:queue) || :carrierwave, queue_options) do |msg|
53
- worker = msg[:worker]
54
- worker.perform
55
- end
56
- @girl_friday_queue << { :worker => worker.new(*args) }
57
- end
58
-
59
- def enqueue_sucker_punch(worker, *args)
60
- worker.new.async.perform(*args)
61
- end
62
-
63
- def enqueue_qu(worker, *args)
64
- worker.instance_variable_set('@queue', queue_options[:queue] || :carrierwave)
65
- ::Qu.enqueue worker, *args
66
- end
67
-
68
- def enqueue_qc(worker, *args)
69
- class_name, subject_id, mounted_as = args
70
- ::QC.enqueue "#{worker.name}.perform", class_name, subject_id, mounted_as.to_s
71
- end
72
-
73
- def enqueue_immediate(worker, *args)
74
- worker.new(*args).perform
75
- end
35
+ private
76
36
 
77
37
  def sidekiq_queue_options(override_queue_name, args)
78
38
  if override_queue_name && queue_options[:queue]
@@ -1,5 +1,5 @@
1
1
  module CarrierWave
2
2
  module Backgrounder
3
- VERSION = "0.4.2"
3
+ VERSION = "1.0.0.beta"
4
4
  end
5
5
  end
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+ require active_job
3
+
4
+ module CarrierWave
5
+ module Workers
6
+ module ActiveJob
7
+
8
+ class ProcessAsset < ::ActiveJob::Base
9
+ include CarrierWave::Workers::ProcessAssetMixin
10
+ end
11
+
12
+ end # ActiveJob
13
+ end # Workers
14
+ end # Backgrounder
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+ require active_job
3
+
4
+ module CarrierWave
5
+ module Workers
6
+ module ActiveJob
7
+
8
+ class StoreAsset < ::ActiveJob::Base
9
+ include CarrierWave::Workers::StoreAssetMixin
10
+ end
11
+
12
+ end # ActiveJob
13
+ end # Workers
14
+ end # Backgrounder
@@ -1,14 +1,18 @@
1
+ # encoding: utf-8
1
2
  module CarrierWave
2
3
  module Workers
3
- class Base < Struct.new(:klass, :id, :column)
4
4
 
5
- def self.perform(*args)
6
- new(*args).perform
5
+ module Base
6
+ attr_accessor :klass, :id, :column, :record
7
+
8
+ def initialize(*args)
9
+ super(*args) unless self.class.superclass == Object
10
+ set_args(*args) if args.present?
7
11
  end
8
12
 
9
13
  def perform(*args)
10
14
  set_args(*args) if args.present?
11
- constantized_resource.find id
15
+ self.record = constantized_resource.find id
12
16
  rescue *not_found_errors
13
17
  end
14
18
 
@@ -29,6 +33,10 @@ module CarrierWave
29
33
  klass.is_a?(String) ? klass.constantize : klass
30
34
  end
31
35
 
32
- end
33
- end
34
- end
36
+ def when_not_ready
37
+ end
38
+
39
+ end # Base
40
+
41
+ end # Workers
42
+ end # CarrierWave
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+ module CarrierWave
3
+ module Workers
4
+
5
+ module ClassMethods
6
+
7
+ def perform(*args)
8
+ new(*args).perform
9
+ end
10
+
11
+ end # ClassMethods
12
+
13
+ end # Workers
14
+ end # Backgrounder
@@ -2,19 +2,8 @@
2
2
  module CarrierWave
3
3
  module Workers
4
4
 
5
- class ProcessAsset < Base
6
-
7
- def perform(*args)
8
- record = super(*args)
9
-
10
- if record && record.send(:"#{column}").present?
11
- record.send(:"process_#{column}_upload=", true)
12
- if record.send(:"#{column}").recreate_versions! && record.respond_to?(:"#{column}_processing")
13
- record.update_attribute :"#{column}_processing", false
14
- end
15
- end
16
- end
17
-
5
+ class ProcessAsset
6
+ include CarrierWave::Workers::ProcessAssetMixin
18
7
  end # ProcessAsset
19
8
 
20
9
  end # Workers
@@ -0,0 +1,37 @@
1
+ # encoding: utf-8
2
+ module CarrierWave
3
+ module Workers
4
+
5
+ module ProcessAssetMixin
6
+ include CarrierWave::Workers::Base
7
+
8
+ def self.included(base)
9
+ base.extend CarrierWave::Workers::ClassMethods
10
+ end
11
+
12
+ def perform(*args)
13
+ record = super(*args)
14
+ asset = record.send(:"#{column}")
15
+
16
+ return unless record && asset_present?(asset)
17
+
18
+ recreate_asset_versions!(asset)
19
+
20
+ if record.respond_to?(:"#{column}_processing")
21
+ record.update_attribute :"#{column}_processing", false
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def recreate_asset_versions!(asset)
28
+ asset.is_a?(Array) ? asset.map(&:recreate_versions!) : asset.recreate_versions!
29
+ end
30
+
31
+ def asset_present?(asset)
32
+ asset.is_a?(Array) ? asset.present? : asset.file.present?
33
+ end
34
+ end # ProcessAssetMixin
35
+
36
+ end # Workers
37
+ end # Backgrounder
@@ -2,32 +2,8 @@
2
2
  module CarrierWave
3
3
  module Workers
4
4
 
5
- class StoreAsset < Base
6
- attr_reader :cache_path, :tmp_directory
7
-
8
- def perform(*args)
9
- record = super(*args)
10
-
11
- if record && record.send(:"#{column}_tmp")
12
- store_directories(record)
13
- record.send :"process_#{column}_upload=", true
14
- record.send :"#{column}_tmp=", nil
15
- record.send :"#{column}_processing=", false if record.respond_to?(:"#{column}_processing")
16
- File.open(cache_path) { |f| record.send :"#{column}=", f }
17
- if record.save!
18
- FileUtils.rm_r(tmp_directory, :force => true)
19
- end
20
- end
21
- end
22
-
23
- private
24
-
25
- def store_directories(record)
26
- asset, asset_tmp = record.send(:"#{column}"), record.send(:"#{column}_tmp")
27
- cache_directory = File.expand_path(asset.cache_dir, asset.root)
28
- @cache_path = File.join(cache_directory, asset_tmp)
29
- @tmp_directory = File.join(cache_directory, asset_tmp.split("/").first)
30
- end
5
+ class StoreAsset
6
+ include CarrierWave::Workers::StoreAssetMixin
31
7
  end # StoreAsset
32
8
 
33
9
  end # Workers