carrierwave_backgrounder 0.4.3 → 1.0.0.beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/.travis.yml +8 -4
- data/CHANGELOG.md +14 -0
- data/Gemfile +1 -1
- data/README.md +59 -67
- data/carrierwave_backgrounder.gemspec +7 -2
- data/lib/backgrounder/orm/activemodel.rb +7 -7
- data/lib/backgrounder/orm/base.rb +18 -12
- data/lib/backgrounder/support/backends.rb +1 -45
- data/lib/backgrounder/version.rb +1 -1
- data/lib/backgrounder/workers/active_job/process_asset.rb +14 -0
- data/lib/backgrounder/workers/active_job/store_asset.rb +14 -0
- data/lib/backgrounder/workers/process_asset.rb +2 -0
- data/lib/backgrounder/workers/process_asset_mixin.rb +16 -7
- data/lib/backgrounder/workers/store_asset.rb +2 -0
- data/lib/backgrounder/workers/store_asset_mixin.rb +14 -19
- data/lib/backgrounder/workers.rb +0 -2
- data/lib/carrierwave_backgrounder.rb +24 -10
- data/lib/generators/carrierwave_backgrounder/install_generator.rb +1 -1
- data/lib/generators/carrierwave_backgrounder/templates/config/initializers/carrierwave_backgrounder.rb +1 -7
- data/spec/backgrounder/orm/activemodel_spec.rb +2 -5
- data/spec/backgrounder/support/backends_spec.rb +0 -159
- data/spec/backgrounder/workers/process_asset_spec.rb +8 -6
- data/spec/backgrounder/workers/store_asset_spec.rb +20 -99
- data/spec/integrations/process_in_background_multi_upload_spec.rb +81 -0
- data/spec/integrations/process_in_background_spec.rb +75 -0
- data/spec/integrations/store_in_background_multi_upload_spec.rb +87 -0
- data/spec/integrations/store_in_background_spec.rb +86 -0
- data/spec/rails_helper.rb +27 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/support/dummy_app/Gemfile +49 -0
- data/spec/support/dummy_app/README.md +24 -0
- data/spec/support/dummy_app/Rakefile +6 -0
- data/spec/support/dummy_app/app/controllers/application_controller.rb +2 -0
- data/spec/support/dummy_app/app/controllers/concerns/.keep +0 -0
- data/spec/support/dummy_app/app/jobs/application_job.rb +7 -0
- data/spec/support/dummy_app/app/models/admin.rb +10 -0
- data/spec/support/dummy_app/app/models/application_record.rb +3 -0
- data/spec/support/dummy_app/app/models/concerns/.keep +0 -0
- data/spec/support/dummy_app/app/models/user.rb +10 -0
- data/spec/support/dummy_app/app/uploaders/avatar_uploader.rb +67 -0
- data/spec/support/dummy_app/bin/bundle +114 -0
- data/spec/support/dummy_app/bin/rails +4 -0
- data/spec/support/dummy_app/bin/rake +4 -0
- data/spec/support/dummy_app/bin/setup +33 -0
- data/spec/support/dummy_app/config/application.rb +39 -0
- data/spec/support/dummy_app/config/boot.rb +3 -0
- data/spec/support/dummy_app/config/database.yml +25 -0
- data/spec/support/dummy_app/config/environment.rb +8 -0
- data/spec/support/dummy_app/config/environments/development.rb +65 -0
- data/spec/support/dummy_app/config/environments/production.rb +86 -0
- data/spec/support/dummy_app/config/environments/test.rb +63 -0
- data/spec/support/dummy_app/config/initializers/carrierwave_backgrounder.rb +5 -0
- data/spec/support/dummy_app/config/initializers/cors.rb +16 -0
- data/spec/support/dummy_app/config/initializers/filter_parameter_logging.rb +8 -0
- data/spec/support/dummy_app/config/initializers/inflections.rb +16 -0
- data/spec/support/dummy_app/config/locales/en.yml +33 -0
- data/spec/support/dummy_app/config/routes.rb +6 -0
- data/spec/support/dummy_app/config/storage.yml +34 -0
- data/spec/support/dummy_app/config.ru +6 -0
- data/spec/support/dummy_app/db/migrate/20230804214459_create_users.rb +9 -0
- data/spec/support/dummy_app/db/migrate/20230807165013_add_avatar_tmp_column_to_user.rb +5 -0
- data/spec/support/dummy_app/db/migrate/20230808233036_add_avatar_processing_flag_to_users.rb +5 -0
- data/spec/support/dummy_app/db/migrate/20230809215320_create_admins.rb +10 -0
- data/spec/support/dummy_app/db/migrate/20230810182011_add_images_to_users.rb +7 -0
- data/spec/support/dummy_app/db/migrate/20230811155811_add_images_to_admins.rb +6 -0
- data/spec/support/dummy_app/db/schema.rb +34 -0
- data/spec/support/dummy_app/db/seeds.rb +7 -0
- data/spec/support/dummy_app/lib/tasks/.keep +0 -0
- data/spec/support/dummy_app/log/.keep +0 -0
- data/spec/support/dummy_app/public/robots.txt +1 -0
- data/spec/support/dummy_app/storage/.keep +0 -0
- data/spec/support/dummy_app/tmp/.keep +0 -0
- data/spec/support/dummy_app/tmp/development_secret.txt +1 -0
- data/spec/support/dummy_app/tmp/images/.gitkeep +0 -0
- data/spec/support/dummy_app/tmp/pids/.keep +0 -0
- data/spec/support/dummy_app/tmp/storage/.keep +0 -0
- data/spec/support/dummy_app/vendor/.keep +0 -0
- data/spec/support/fixtures/images/test-1.jpg +0 -0
- data/spec/support/fixtures/images/test-2.jpg +0 -0
- data/spec/support/global_macros.rb +29 -0
- data/spec/support/mock_worker.rb +2 -0
- metadata +203 -15
- data/spec/support/backend_constants.rb +0 -58
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 29bf0a9e2454432c72a9799addeb1f6f059fb910766d946bb999d8b616d9d078
|
|
4
|
+
data.tar.gz: 9ab201113526a8c1cb9529f5361b6023349f7f73959ad13c075c7468d68a86c1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f7dee9bd5893f749599fb804e40400f7d4b54cba1e0ea295962a9442cfbcb02b1d08fd643096d8d242adc2911e65cfcccb97e4f7ef23922d298cada2a2ec8575
|
|
7
|
+
data.tar.gz: e9465ab71a2fe0fc263121f9fc0aee60475cfe4a0f0bec2675d9194abceeaa6372272ed9bacf3867a671b7d4169c5bd51ab414897e06173c52f7aa3e67f700d8
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
language: ruby
|
|
2
2
|
|
|
3
3
|
rvm:
|
|
4
|
-
- 2.
|
|
5
|
-
-
|
|
6
|
-
-
|
|
7
|
-
- 2
|
|
4
|
+
- 2.7
|
|
5
|
+
- 3.0
|
|
6
|
+
- 3.1
|
|
7
|
+
- 3.2
|
|
8
8
|
- ruby-head
|
|
9
9
|
|
|
10
|
+
env:
|
|
11
|
+
- QUEUE_ADAPTER='sidekiq'
|
|
12
|
+
- QUEUE_ADAPTER='active_job'
|
|
13
|
+
|
|
10
14
|
before_install:
|
|
11
15
|
- yes | gem update --system --force
|
|
12
16
|
- gem install bundler
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,17 @@
|
|
|
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
|
|
1
15
|
|
|
2
16
|
## 0.4.3
|
|
3
17
|
|
data/Gemfile
CHANGED
data/README.md
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
# CarrierWave Backgrounder
|
|
2
2
|
|
|
3
|
-
[](https://app.travis-ci.com/lardawge/carrierwave_backgrounder)
|
|
4
4
|
[](https://codeclimate.com/github/lardawge/carrierwave_backgrounder)
|
|
5
5
|
|
|
6
|
-
|
|
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.
|
|
7
10
|
|
|
8
|
-
|
|
9
|
-
|
|
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.
|
|
10
15
|
|
|
11
16
|
## Background options
|
|
12
17
|
|
|
@@ -15,18 +20,17 @@ There are currently two offerings for backgrounding upload tasks which are as fo
|
|
|
15
20
|
```ruby
|
|
16
21
|
# This stores the original file with no processing/versioning.
|
|
17
22
|
# It will upload the original file to s3.
|
|
18
|
-
# This was developed to use where you do not have
|
|
23
|
+
# This was developed to use in cases where you do not have access to the cached location such as Heroku.
|
|
19
24
|
|
|
20
25
|
Backgrounder::ORM::Base::process_in_background
|
|
21
26
|
```
|
|
22
27
|
|
|
23
28
|
```ruby
|
|
24
|
-
# This does nothing to the file after it is cached which makes it
|
|
29
|
+
# This does nothing to the file after it is cached which makes it fast.
|
|
25
30
|
# It requires a column in the database which stores the cache location set by carrierwave so the background job can access it.
|
|
26
31
|
# The drawback to using this method is the need for a central location to store the cached files.
|
|
27
|
-
# Heroku may deploy workers on separate servers from where your dyno cached the files.
|
|
28
32
|
#
|
|
29
|
-
# 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.
|
|
30
34
|
|
|
31
35
|
Backgrounder::ORM::Base::store_in_background
|
|
32
36
|
```
|
|
@@ -35,6 +39,8 @@ Backgrounder::ORM::Base::store_in_background
|
|
|
35
39
|
|
|
36
40
|
These instructions assume you have previously set up [CarrierWave](https://github.com/jnicklas/carrierwave) and your queuing lib of choice.
|
|
37
41
|
|
|
42
|
+
Take a look at the [Rails app](spec/support/dummy_app) to see examples of setup.
|
|
43
|
+
|
|
38
44
|
In Rails, add the following your Gemfile:
|
|
39
45
|
|
|
40
46
|
```ruby
|
|
@@ -43,31 +49,41 @@ gem 'carrierwave_backgrounder'
|
|
|
43
49
|
|
|
44
50
|
Run the generator which will create an initializer in config/initializers.
|
|
45
51
|
```bash
|
|
46
|
-
|
|
52
|
+
rails g carrierwave_backgrounder:install
|
|
47
53
|
```
|
|
48
54
|
|
|
49
|
-
You can pass additional configuration options to
|
|
55
|
+
You can pass additional configuration options to Sidekiq:
|
|
50
56
|
|
|
51
57
|
```ruby
|
|
52
58
|
CarrierWave::Backgrounder.configure do |c|
|
|
53
|
-
c.backend :
|
|
59
|
+
c.backend :sidekiq, queue: :awesome_queue, size: 3
|
|
54
60
|
end
|
|
55
61
|
```
|
|
56
62
|
|
|
57
|
-
**IMPORTANT FOR SIDEKIQ BACKEND** -
|
|
63
|
+
**IMPORTANT FOR SIDEKIQ BACKEND** - carrierwave (default queue name) should be added to your queue list or it will not run:
|
|
58
64
|
|
|
59
65
|
```yml
|
|
60
66
|
:queues:
|
|
61
|
-
- [
|
|
67
|
+
- [carrierwave, 1]
|
|
62
68
|
- default
|
|
63
69
|
```
|
|
64
70
|
|
|
65
|
-
In your CarrierWave uploader file:
|
|
71
|
+
In your CarrierWave uploader file you will need to add a cache directory as well as change cache_storage to File:
|
|
66
72
|
|
|
67
73
|
```ruby
|
|
68
74
|
class AvatarUploader < CarrierWave::Uploader::Base
|
|
69
75
|
include ::CarrierWave::Backgrounder::Delay
|
|
70
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
|
+
|
|
71
87
|
#etc...
|
|
72
88
|
end
|
|
73
89
|
```
|
|
@@ -82,7 +98,9 @@ process_in_background :avatar
|
|
|
82
98
|
```
|
|
83
99
|
|
|
84
100
|
Optionally you can add a column to the database which will be set to `true` when
|
|
85
|
-
the background processing
|
|
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.
|
|
86
104
|
|
|
87
105
|
```ruby
|
|
88
106
|
add_column :users, :avatar_processing, :boolean, null: false, default: false
|
|
@@ -97,7 +115,7 @@ mount_uploader :avatar, AvatarUploader
|
|
|
97
115
|
store_in_background :avatar
|
|
98
116
|
```
|
|
99
117
|
|
|
100
|
-
Add a column to the model
|
|
118
|
+
Add a column to the model which will store the temp file location:
|
|
101
119
|
|
|
102
120
|
```ruby
|
|
103
121
|
add_column :users, :avatar_tmp, :string
|
|
@@ -115,23 +133,37 @@ If you need to process/store the upload immediately:
|
|
|
115
133
|
This must be set before you assign an upload:
|
|
116
134
|
|
|
117
135
|
```ruby
|
|
118
|
-
# In a controller
|
|
119
136
|
@user = User.new
|
|
120
137
|
@user.process_avatar_upload = true
|
|
121
138
|
@user.attributes = params[:user]
|
|
122
139
|
```
|
|
123
140
|
|
|
124
141
|
### Override worker
|
|
125
|
-
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
|
|
126
|
-
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:
|
|
127
143
|
|
|
128
144
|
```ruby
|
|
129
145
|
process_in_background :avatar, MyParanoidWorker
|
|
130
146
|
```
|
|
131
147
|
|
|
132
|
-
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`
|
|
133
158
|
|
|
134
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
|
+
|
|
135
167
|
class MyParanoidWorker < ::CarrierWave::Workers::ProcessAsset
|
|
136
168
|
# ...or subclass CarrierWave::Workers::StoreAsset if you're using store_in_background
|
|
137
169
|
|
|
@@ -143,29 +175,18 @@ class MyParanoidWorker < ::CarrierWave::Workers::ProcessAsset
|
|
|
143
175
|
end
|
|
144
176
|
```
|
|
145
177
|
|
|
146
|
-
### ActiveJob
|
|
147
|
-
Use overriden worker that inherits from ActiveJob::Base and includes relevant worker mixin:
|
|
148
178
|
```ruby
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
179
|
+
# ActiveJob Example
|
|
180
|
+
|
|
181
|
+
class User < ActiveRecord::Base
|
|
182
|
+
mount_uploader :avatar, AvatarUploader
|
|
183
|
+
process_in_background, :avatar, MyActiveJobWorker
|
|
184
|
+
end
|
|
152
185
|
|
|
186
|
+
class MyActiveJobWorker < ::CarrierWave::Workers::ActiveJob::StoreAsset
|
|
153
187
|
after_perform do
|
|
154
188
|
# your code here
|
|
155
189
|
end
|
|
156
|
-
|
|
157
|
-
# Sometimes job gets performed before the file is uploaded and ready.
|
|
158
|
-
# You can define how to handle that case by overriding `when_not_ready` method
|
|
159
|
-
# (by default it does nothing)
|
|
160
|
-
def when_not_ready
|
|
161
|
-
retry_job
|
|
162
|
-
end
|
|
163
|
-
end
|
|
164
|
-
```
|
|
165
|
-
Don't forget to set `active_job` as a backend in the config:
|
|
166
|
-
```ruby
|
|
167
|
-
CarrierWave::Backgrounder.configure do |c|
|
|
168
|
-
c.backend :active_job, queue: :carrierwave
|
|
169
190
|
end
|
|
170
191
|
```
|
|
171
192
|
|
|
@@ -174,35 +195,6 @@ We use the after_commit hook when using active_record. This creates a problem wh
|
|
|
174
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).
|
|
175
196
|
There are various other solutions in which case google is your friend.
|
|
176
197
|
|
|
177
|
-
### Uploaders mounted on mongoid embedded documents
|
|
178
|
-
The workers fetch the document with the mounted uploader using the model class name and id. Uploads on embedded documents
|
|
179
|
-
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
|
|
180
|
-
find method like this:
|
|
181
|
-
|
|
182
|
-
```ruby
|
|
183
|
-
class SomeRootDocument
|
|
184
|
-
include Mongoid::Document
|
|
185
|
-
|
|
186
|
-
embeds_many :embedded_documents
|
|
187
|
-
end
|
|
188
|
-
|
|
189
|
-
class EmbeddedDocument
|
|
190
|
-
include Mongoid::Document
|
|
191
|
-
|
|
192
|
-
embedded_in :some_root_document
|
|
193
|
-
|
|
194
|
-
mount_uploader :image, ImageUploader
|
|
195
|
-
process_in_background :image
|
|
196
|
-
|
|
197
|
-
def self.find(id)
|
|
198
|
-
bson_id = Moped::BSON::ObjectId.from_string(id) # needed for Mongoid 3
|
|
199
|
-
|
|
200
|
-
root = SomeRootDocument.where('embedded_documents._id' => bson_id).first
|
|
201
|
-
root.embedded_documents.find(id)
|
|
202
|
-
end
|
|
203
|
-
end
|
|
204
|
-
```
|
|
205
|
-
|
|
206
198
|
## License
|
|
207
199
|
|
|
208
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", ["
|
|
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.
|
|
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", :
|
|
14
|
-
|
|
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
|
-
|
|
23
|
+
remote_url_present? || # Remote upload support
|
|
25
24
|
send(:"#{column}_cache").present? # Form failure support
|
|
26
25
|
end
|
|
27
|
-
end
|
|
28
26
|
|
|
29
|
-
|
|
30
|
-
|
|
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,
|
|
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,19 +70,16 @@ module CarrierWave
|
|
|
69
70
|
# store_in_background :avatar, CustomWorker
|
|
70
71
|
# end
|
|
71
72
|
#
|
|
72
|
-
def store_in_background(column,
|
|
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
|
|
82
|
+
super and return if process_#{column}_upload || remove_#{column}
|
|
85
83
|
self.#{column}_tmp = #{column}_cache if #{column}_cache
|
|
86
84
|
end
|
|
87
85
|
|
|
@@ -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}
|
|
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,57 +26,13 @@ module CarrierWave
|
|
|
26
26
|
worker.perform_later(*args.map(&:to_s))
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
-
def enqueue_delayed_job(worker, *args)
|
|
30
|
-
worker_args = {}
|
|
31
|
-
if ::Delayed::Job.new.respond_to?(:queue)
|
|
32
|
-
worker_args[:queue] = queue_options[:queue] if queue_options[:queue]
|
|
33
|
-
worker_args[:priority] = queue_options[:priority] if queue_options[:priority]
|
|
34
|
-
::Delayed::Job.enqueue worker.new(*args), worker_args
|
|
35
|
-
else
|
|
36
|
-
worker_args[:priority] = queue_options[:priority] if queue_options[:priority]
|
|
37
|
-
::Delayed::Job.enqueue worker.new(*args), worker_args
|
|
38
|
-
if queue_options[:queue]
|
|
39
|
-
::Rails.logger.warn("Queue name given but no queue column exists for Delayed::Job")
|
|
40
|
-
end
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
def enqueue_resque(worker, *args)
|
|
45
|
-
worker.instance_variable_set('@queue', queue_options[:queue] || :carrierwave)
|
|
46
|
-
::Resque.enqueue worker, *args
|
|
47
|
-
end
|
|
48
|
-
|
|
49
29
|
def enqueue_sidekiq(worker, *args)
|
|
50
30
|
override_queue_name = worker.sidekiq_options['queue'] == 'default' || worker.sidekiq_options['queue'].nil?
|
|
51
31
|
args = sidekiq_queue_options(override_queue_name, 'class' => worker, 'args' => args.map(&:to_s))
|
|
52
32
|
worker.client_push(args)
|
|
53
33
|
end
|
|
54
34
|
|
|
55
|
-
|
|
56
|
-
@girl_friday_queue ||= GirlFriday::WorkQueue.new(queue_options.delete(:queue) || :carrierwave, queue_options) do |msg|
|
|
57
|
-
worker = msg[:worker]
|
|
58
|
-
worker.perform
|
|
59
|
-
end
|
|
60
|
-
@girl_friday_queue << { :worker => worker.new(*args) }
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
def enqueue_sucker_punch(worker, *args)
|
|
64
|
-
worker.new.async.perform(*args)
|
|
65
|
-
end
|
|
66
|
-
|
|
67
|
-
def enqueue_qu(worker, *args)
|
|
68
|
-
worker.instance_variable_set('@queue', queue_options[:queue] || :carrierwave)
|
|
69
|
-
::Qu.enqueue worker, *args
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
def enqueue_qc(worker, *args)
|
|
73
|
-
class_name, subject_id, mounted_as = args
|
|
74
|
-
::QC.enqueue "#{worker.name}.perform", class_name, subject_id, mounted_as.to_s
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
def enqueue_immediate(worker, *args)
|
|
78
|
-
worker.new(*args).perform
|
|
79
|
-
end
|
|
35
|
+
private
|
|
80
36
|
|
|
81
37
|
def sidekiq_queue_options(override_queue_name, args)
|
|
82
38
|
if override_queue_name && queue_options[:queue]
|
data/lib/backgrounder/version.rb
CHANGED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
require 'backgrounder/workers/process_asset_mixin'
|
|
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 'backgrounder/workers/store_asset_mixin'
|
|
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
|
|
@@ -11,17 +11,26 @@ module CarrierWave
|
|
|
11
11
|
|
|
12
12
|
def perform(*args)
|
|
13
13
|
record = super(*args)
|
|
14
|
+
asset = record.send(:"#{column}")
|
|
14
15
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
when_not_ready
|
|
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
22
|
end
|
|
23
23
|
end
|
|
24
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
|
|
25
34
|
end # ProcessAssetMixin
|
|
26
35
|
|
|
27
36
|
end # Workers
|
|
@@ -9,34 +9,29 @@ module CarrierWave
|
|
|
9
9
|
base.extend CarrierWave::Workers::ClassMethods
|
|
10
10
|
end
|
|
11
11
|
|
|
12
|
-
attr_reader :cache_path, :tmp_directory
|
|
13
|
-
|
|
14
12
|
def perform(*args)
|
|
15
13
|
record = super(*args)
|
|
16
14
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
else
|
|
27
|
-
when_not_ready
|
|
28
|
-
end
|
|
15
|
+
return unless record && record.send(:"#{column}_tmp")
|
|
16
|
+
|
|
17
|
+
record.send :"process_#{column}_upload=", true
|
|
18
|
+
record.send :"#{column}_cache=", record.send(:"#{column}_tmp") # Set the cache path
|
|
19
|
+
cache_assets! record.send(:"#{column}") # Trigger version creation
|
|
20
|
+
store_assets! record.send(:"#{column}") # Store the files
|
|
21
|
+
record.send :"#{column}_tmp=", nil
|
|
22
|
+
record.send :"#{column}_processing=", false if record.respond_to?(:"#{column}_processing")
|
|
23
|
+
record.save!
|
|
29
24
|
end
|
|
30
25
|
|
|
31
26
|
private
|
|
32
27
|
|
|
33
|
-
def
|
|
34
|
-
asset
|
|
35
|
-
cache_directory = File.expand_path(asset.cache_dir, asset.root)
|
|
36
|
-
@cache_path = File.join(cache_directory, asset_tmp)
|
|
37
|
-
@tmp_directory = File.join(cache_directory, asset_tmp.split("/").first)
|
|
28
|
+
def cache_assets!(asset)
|
|
29
|
+
asset.is_a?(Array) ? asset.map(&:cache!) : asset.cache!
|
|
38
30
|
end
|
|
39
31
|
|
|
32
|
+
def store_assets!(asset)
|
|
33
|
+
asset.is_a?(Array) ? asset.map(&:store!) : asset.store!
|
|
34
|
+
end
|
|
40
35
|
end # StoreAssetMixin
|
|
41
36
|
|
|
42
37
|
end # Workers
|
data/lib/backgrounder/workers.rb
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
require 'backgrounder/workers/base'
|
|
2
2
|
require 'backgrounder/workers/class_methods'
|
|
3
|
-
require 'backgrounder/workers/process_asset_mixin'
|
|
4
|
-
require 'backgrounder/workers/store_asset_mixin'
|
|
5
3
|
require 'backgrounder/workers/process_asset'
|
|
6
4
|
require 'backgrounder/workers/store_asset'
|