Skip to content

Commit

Permalink
Add ActiveJob support
Browse files Browse the repository at this point in the history
  • Loading branch information
roterski committed Sep 26, 2015
1 parent 2174c5c commit 51b4003
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 46 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,33 @@ class MyParanoidWorker < ::CarrierWave::Workers::ProcessAsset
# other hooks you might care about
end
```

### ActiveJob
Use overriden worker that inherits from ActiveJob::Base and includes relevant worker mixin:
```ruby
class MyActiveJobWorker < ActiveJob::Base
include ::CarrierWave::Workers::ProcessAssetMixin
# ... or include ::CarrierWave::Workers::StoreAssetMixin

after_perform do
# your code here
end

# Sometimes job gets performed before the file is uploaded and ready.
# You can define how to handle that case by overriding `when_not_ready` method
# (by default it does nothing)
def when_not_ready
retry_job
end
end
```
Don't forget to set `active_job` as a backend in the config:
```ruby
CarrierWave::Backgrounder.configure do |c|
c.backend :active_job, queue: :carrierwave
end
```

### Testing with Rspec
We use the after_commit hook when using active_record. This creates a problem when testing with Rspec because after_commit never gets fired
if you're using trasactional fixtures. One solution to the problem is to use the [TestAfterCommit gem](https://github.com/grosser/test_after_commit).
Expand Down
4 changes: 4 additions & 0 deletions lib/backgrounder/support/backends.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def enqueue_for_backend(worker, class_name, subject_id, mounted_as)

private

def enqueue_active_job(worker, *args)
worker.perform_later(*args.map(&:to_s))
end

def enqueue_delayed_job(worker, *args)
worker_args = {}
if ::Delayed::Job.new.respond_to?(:queue)
Expand Down
3 changes: 3 additions & 0 deletions lib/backgrounder/workers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require 'backgrounder/workers/base'
require 'backgrounder/workers/class_methods'
require 'backgrounder/workers/process_asset_mixin'
require 'backgrounder/workers/store_asset_mixin'
require 'backgrounder/workers/process_asset'
require 'backgrounder/workers/store_asset'
22 changes: 15 additions & 7 deletions lib/backgrounder/workers/base.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
# encoding: utf-8
module CarrierWave
module Workers
class Base < Struct.new(:klass, :id, :column)

def self.perform(*args)
new(*args).perform
module Base
attr_accessor :klass, :id, :column, :record

def initialize(*args)
super(*args) unless self.class.superclass == Object
set_args(*args) if args.present?
end

def perform(*args)
set_args(*args) if args.present?
constantized_resource.find id
self.record = constantized_resource.find id
rescue *not_found_errors
end

Expand All @@ -29,6 +33,10 @@ def constantized_resource
klass.is_a?(String) ? klass.constantize : klass
end

end
end
end
def when_not_ready
end

end # Base

end # Workers
end # CarrierWave
14 changes: 14 additions & 0 deletions lib/backgrounder/workers/class_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# encoding: utf-8
module CarrierWave
module Workers

module ClassMethods

def perform(*args)
new(*args).perform
end

end # ClassMethods

end # Workers
end # Backgrounder
15 changes: 2 additions & 13 deletions lib/backgrounder/workers/process_asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,8 @@
module CarrierWave
module Workers

class ProcessAsset < Base

def perform(*args)
record = super(*args)

if record && record.send(:"#{column}").present?
record.send(:"process_#{column}_upload=", true)
if record.send(:"#{column}").recreate_versions! && record.respond_to?(:"#{column}_processing")
record.update_attribute :"#{column}_processing", false
end
end
end

class ProcessAsset
include CarrierWave::Workers::ProcessAssetMixin
end # ProcessAsset

end # Workers
Expand Down
28 changes: 28 additions & 0 deletions lib/backgrounder/workers/process_asset_mixin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# encoding: utf-8
module CarrierWave
module Workers

module ProcessAssetMixin
include CarrierWave::Workers::Base

def self.included(base)
base.extend CarrierWave::Workers::ClassMethods
end

def perform(*args)
record = super(*args)

if record && record.send(:"#{column}").present?
record.send(:"process_#{column}_upload=", true)
if record.send(:"#{column}").recreate_versions! && record.respond_to?(:"#{column}_processing")
record.update_attribute :"#{column}_processing", false
end
else
when_not_ready
end
end

end # ProcessAssetMixin

end # Workers
end # Backgrounder
28 changes: 2 additions & 26 deletions lib/backgrounder/workers/store_asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,8 @@
module CarrierWave
module Workers

class StoreAsset < Base
attr_reader :cache_path, :tmp_directory

def perform(*args)
record = super(*args)

if record && record.send(:"#{column}_tmp")
store_directories(record)
record.send :"process_#{column}_upload=", true
record.send :"#{column}_tmp=", nil
record.send :"#{column}_processing=", false if record.respond_to?(:"#{column}_processing")
File.open(cache_path) { |f| record.send :"#{column}=", f }
if record.save!
FileUtils.rm_r(tmp_directory, :force => true)
end
end
end

private

def store_directories(record)
asset, asset_tmp = record.send(:"#{column}"), record.send(:"#{column}_tmp")
cache_directory = File.expand_path(asset.cache_dir, asset.root)
@cache_path = File.join(cache_directory, asset_tmp)
@tmp_directory = File.join(cache_directory, asset_tmp.split("/").first)
end
class StoreAsset
include CarrierWave::Workers::StoreAssetMixin
end # StoreAsset

end # Workers
Expand Down
43 changes: 43 additions & 0 deletions lib/backgrounder/workers/store_asset_mixin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# encoding: utf-8
module CarrierWave
module Workers

module StoreAssetMixin
include CarrierWave::Workers::Base

def self.included(base)
base.extend CarrierWave::Workers::ClassMethods
end

attr_reader :cache_path, :tmp_directory

def perform(*args)
record = super(*args)

if record && record.send(:"#{column}_tmp")
store_directories(record)
record.send :"process_#{column}_upload=", true
record.send :"#{column}_tmp=", nil
record.send :"#{column}_processing=", false if record.respond_to?(:"#{column}_processing")
File.open(cache_path) { |f| record.send :"#{column}=", f }
if record.save!
FileUtils.rm_r(tmp_directory, :force => true)
end
else
when_not_ready
end
end

private

def store_directories(record)
asset, asset_tmp = record.send(:"#{column}"), record.send(:"#{column}_tmp")
cache_directory = File.expand_path(asset.cache_dir, asset.root)
@cache_path = File.join(cache_directory, asset_tmp)
@tmp_directory = File.join(cache_directory, asset_tmp.split("/").first)
end

end # StoreAssetMixin

end # Workers
end # Backgrounder
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CarrierWave::Backgrounder.configure do |c|
c.backend :delayed_job, queue: :carrierwave
# c.backend :active_job, queue: :carrierwave
# c.backend :resque, queue: :carrierwave
# c.backend :sidekiq, queue: :carrierwave
# c.backend :girl_friday, queue: :carrierwave
Expand Down
10 changes: 10 additions & 0 deletions spec/backgrounder/support/backends_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ module CarrierWave::Backgrounder
describe '#enqueue_for_backend' do
let!(:worker) { MockWorker.new('FakeClass', 1, :image) }

context 'active_job' do
let(:args) { ['FakeClass', 1, :image] }

it 'invokes perform_later with string arguments' do
expect(MockWorker).to receive(:perform_later).with('FakeClass', '1', 'image')
mock_module.backend :active_job
mock_module.enqueue_for_backend(MockWorker, *args)
end
end

context 'delayed_job' do
before do
@mock_worker = Class.new do
Expand Down

0 comments on commit 51b4003

Please sign in to comment.