Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lib/delayed_paperclip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def options
}
end

def options= options
@options = @options.merge(options.symbolize_keys!)
end

def detect_background_task
return DelayedPaperclip::Jobs::DelayedJob if defined? ::Delayed::Job
return DelayedPaperclip::Jobs::Resque if defined? ::Resque
Expand All @@ -23,14 +27,14 @@ def processor
options[:background_job_class]
end

def enqueue(instance_klass, instance_id, attachment_name)
processor.enqueue_delayed_paperclip(instance_klass, instance_id, attachment_name)
def enqueue(instance_klass, instance_id, attachment_name, styles_to_reprocess = [] )
processor.enqueue_delayed_paperclip(instance_klass, instance_id, attachment_name, styles_to_reprocess)
end

def process_job(instance_klass, instance_id, attachment_name)
def process_job(instance_klass, instance_id, attachment_name, styles_to_reprocess = [])
instance_klass.constantize.find(instance_id).
send(attachment_name).
process_delayed!
process_delayed!(styles_to_reprocess)
end

end
Expand Down
9 changes: 7 additions & 2 deletions lib/delayed_paperclip/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,17 @@ def processing?
@instance.send(:"#{@name}_processing?")
end

def process_delayed!
def process_delayed! styles_to_reprocess
self.job_is_processing = true
self.post_processing = true

reprocess!
styles_to_reprocess.map!(&:to_sym)

fail "Attempting to reprocess unknown style(s) #{styles_to_reprocess - styles.keys}" unless (styles_to_reprocess - styles.keys).empty?

reprocess!(*( styles_to_reprocess.empty? ? styles.keys : styles_to_reprocess) )

self.post_processing = false
self.job_is_processing = false
end

Expand Down
14 changes: 7 additions & 7 deletions lib/delayed_paperclip/jobs/delayed_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@

module DelayedPaperclip
module Jobs
class DelayedJob < Struct.new(:instance_klass, :instance_id, :attachment_name)
class DelayedJob < Struct.new(:instance_klass, :instance_id, :attachment_name, :styles_to_reprocess)

if defined?(::Delayed::DeserializationError) # this is available in newer versions of DelayedJob. Using the newee Job api thus.

def self.enqueue_delayed_paperclip(instance_klass, instance_id, attachment_name)
def self.enqueue_delayed_paperclip(instance_klass, instance_id, attachment_name, styles_to_reprocess = [])
::Delayed::Job.enqueue(
:payload_object => new(instance_klass, instance_id, attachment_name),
:payload_object => new(instance_klass, instance_id, attachment_name, styles_to_reprocess),
:priority => instance_klass.constantize.attachment_definitions[attachment_name][:delayed][:priority].to_i
)
end

else

def self.enqueue_delayed_paperclip(instance_klass, instance_id, attachment_name)
def self.enqueue_delayed_paperclip(instance_klass, instance_id, attachment_name, styles_to_reprocess = [])
::Delayed::Job.enqueue(
new(instance_klass, instance_id, attachment_name),
new(instance_klass, instance_id, attachment_name, styles_to_reprocess),
instance_klass.constantize.attachment_definitions[attachment_name][:delayed][:priority].to_i
)
end

end

def perform
DelayedPaperclip.process_job(instance_klass, instance_id, attachment_name)
DelayedPaperclip.process_job(instance_klass, instance_id, attachment_name, styles_to_reprocess)
end
end
end
end
end
10 changes: 5 additions & 5 deletions lib/delayed_paperclip/jobs/resque.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
module DelayedPaperclip
module Jobs
class Resque
@queue = :paperclip
@queue = DelayedPaperclip.options[:queue] || :paperclip

def self.enqueue_delayed_paperclip(instance_klass, instance_id, attachment_name)
::Resque.enqueue(self, instance_klass, instance_id, attachment_name)
def self.enqueue_delayed_paperclip(instance_klass, instance_id, attachment_name, styles_to_reprocess = [])
::Resque.enqueue(self, instance_klass, instance_id, attachment_name, styles_to_reprocess)
end

def self.perform(instance_klass, instance_id, attachment_name)
DelayedPaperclip.process_job(instance_klass, instance_id, attachment_name)
def self.perform(instance_klass, instance_id, attachment_name, styles_to_reprocess = [])
DelayedPaperclip.process_job(instance_klass, instance_id, attachment_name, styles_to_reprocess)
end
end
end
Expand Down
1 change: 1 addition & 0 deletions test/base_delayed_paperclip_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def test_processed_true_when_delayed_jobs_completed

def test_post_processing_value_updated_for_reprocessing
Paperclip::Attachment.any_instance.expects(:post_processing=).with(true)
Paperclip::Attachment.any_instance.expects(:post_processing=).with(false)

dummy = Dummy.new(:image => File.open("#{RAILS_ROOT}/test/fixtures/12k.png"))
dummy.save!
Expand Down