diff --git a/delayed_paperclip.gemspec b/delayed_paperclip.gemspec index 8be11da..52880ab 100644 --- a/delayed_paperclip.gemspec +++ b/delayed_paperclip.gemspec @@ -12,6 +12,7 @@ Gem::Specification.new do |s| s.homepage = %q{http://github.com/jrgifford/delayed_paperclip} s.add_dependency 'paperclip', [">= 3.3"] + s.add_dependency 'activejob', ">= 4.2" s.add_development_dependency 'mocha' s.add_development_dependency "rspec", '< 3.0' diff --git a/lib/delayed_paperclip.rb b/lib/delayed_paperclip.rb index a5e96b2..b1d2904 100644 --- a/lib/delayed_paperclip.rb +++ b/lib/delayed_paperclip.rb @@ -1,4 +1,4 @@ -require 'delayed_paperclip/jobs' +require 'delayed_paperclip/jobs/active_job' require 'delayed_paperclip/attachment' require 'delayed_paperclip/url_generator' require 'delayed_paperclip/railtie' @@ -6,23 +6,15 @@ module DelayedPaperclip class << self - def options @options ||= { - :background_job_class => detect_background_task, + :background_job_class => DelayedPaperclip::Jobs::ActiveJob, :url_with_processing => true, :processing_image_url => nil, :queue => "paperclip" } end - def detect_background_task - return DelayedPaperclip::Jobs::ActiveJob if defined? ::ActiveJob::Base - return DelayedPaperclip::Jobs::DelayedJob if defined? ::Delayed::Job - return DelayedPaperclip::Jobs::Resque if defined? ::Resque - return DelayedPaperclip::Jobs::Sidekiq if defined? ::Sidekiq - end - def processor options[:background_job_class] end diff --git a/lib/delayed_paperclip/jobs.rb b/lib/delayed_paperclip/jobs.rb deleted file mode 100644 index cf07d24..0000000 --- a/lib/delayed_paperclip/jobs.rb +++ /dev/null @@ -1,8 +0,0 @@ -module DelayedPaperclip - module Jobs - autoload :ActiveJob, 'delayed_paperclip/jobs/active_job' - autoload :DelayedJob, 'delayed_paperclip/jobs/delayed_job' - autoload :Resque, 'delayed_paperclip/jobs/resque' - autoload :Sidekiq, 'delayed_paperclip/jobs/sidekiq' - end -end diff --git a/lib/delayed_paperclip/jobs/delayed_job.rb b/lib/delayed_paperclip/jobs/delayed_job.rb deleted file mode 100644 index 3e6ba1c..0000000 --- a/lib/delayed_paperclip/jobs/delayed_job.rb +++ /dev/null @@ -1,46 +0,0 @@ -require 'delayed_job' -require 'active_support/deprecation' - -module DelayedPaperclip - module Jobs - class DelayedJob < Struct.new(:instance_klass, :instance_id, :attachment_name) - - # This is available in newer versions of DelayedJob. Using the newee Job api thus. - if Gem.loaded_specs['delayed_job'].version >= Gem::Version.new("2.1.0") - - def self.enqueue_delayed_paperclip(instance_klass, instance_id, attachment_name) - ActiveSupport::Deprecation.warn(<<-MESSAGE) -Using DelayedJob adapter for delayed_paperclip is deprecated and will be removed in version 3.0.0. -Please use ActiveJob adapter. - MESSAGE - - ::Delayed::Job.enqueue( - :payload_object => new(instance_klass, instance_id, attachment_name), - :priority => instance_klass.constantize.paperclip_definitions[attachment_name][:delayed][:priority].to_i, - :queue => instance_klass.constantize.paperclip_definitions[attachment_name][:delayed][:queue] - ) - end - - else - - def self.enqueue_delayed_paperclip(instance_klass, instance_id, attachment_name) - ActiveSupport::Deprecation.warn(<<-MESSAGE) -Using DelayedJob adapter for delayed_paperclip is deprecated and will be removed in version 3.0.0. -Please use ActiveJob adapter. - MESSAGE - - ::Delayed::Job.enqueue( - new(instance_klass, instance_id, attachment_name), - instance_klass.constantize.paperclip_definitions[attachment_name][:delayed][:priority].to_i, - instance_klass.constantize.paperclip_definitions[attachment_name][:delayed][:queue] - ) - end - - end - - def perform - DelayedPaperclip.process_job(instance_klass, instance_id, attachment_name) - end - end - end -end diff --git a/lib/delayed_paperclip/jobs/resque.rb b/lib/delayed_paperclip/jobs/resque.rb deleted file mode 100644 index 5528aed..0000000 --- a/lib/delayed_paperclip/jobs/resque.rb +++ /dev/null @@ -1,22 +0,0 @@ -require 'resque' -require 'active_support/deprecation' - -module DelayedPaperclip - module Jobs - class Resque - def self.enqueue_delayed_paperclip(instance_klass, instance_id, attachment_name) - @queue = instance_klass.constantize.paperclip_definitions[attachment_name][:delayed][:queue] - ::Resque.enqueue(self, instance_klass, instance_id, attachment_name) - - ActiveSupport::Deprecation.warn(<<-MESSAGE) -Using Resque adapter for delayed_paperclip is deprecated and will be removed in version 3.0.0. -Please use ActiveJob adapter. - MESSAGE - end - - def self.perform(instance_klass, instance_id, attachment_name) - DelayedPaperclip.process_job(instance_klass, instance_id, attachment_name) - end - end - end -end diff --git a/lib/delayed_paperclip/jobs/sidekiq.rb b/lib/delayed_paperclip/jobs/sidekiq.rb deleted file mode 100644 index 80aede0..0000000 --- a/lib/delayed_paperclip/jobs/sidekiq.rb +++ /dev/null @@ -1,30 +0,0 @@ -require 'sidekiq/worker' -require 'active_support/deprecation' - -module DelayedPaperclip - module Jobs - class Sidekiq - include ::Sidekiq::Worker - - def self.enqueue_delayed_paperclip(instance_klass, instance_id, attachment_name) - ActiveSupport::Deprecation.warn(<<-MESSAGE) -Using Sidekiq adapter for delayed_paperclip is deprecated and will be removed in version 3.0.0. -Please use ActiveJob adapter. - MESSAGE - - queue_name = instance_klass.constantize.paperclip_definitions[attachment_name][:delayed][:queue] - # Sidekiq >= 4.1.0 - if respond_to?(:set) - set(:queue => queue_name) - else - sidekiq_options :queue => queue_name - end - perform_async(instance_klass, instance_id, attachment_name) - end - - def perform(instance_klass, instance_id, attachment_name) - DelayedPaperclip.process_job(instance_klass, instance_id, attachment_name) - end - end - end -end diff --git a/spec/delayed_paperclip/attachment_spec.rb b/spec/delayed_paperclip/attachment_spec.rb index b2a5739..f84a838 100644 --- a/spec/delayed_paperclip/attachment_spec.rb +++ b/spec/delayed_paperclip/attachment_spec.rb @@ -2,7 +2,7 @@ describe DelayedPaperclip::Attachment do before :each do - DelayedPaperclip.options[:background_job_class] = DelayedPaperclip::Jobs::Resque + DelayedPaperclip.options[:background_job_class] = DelayedPaperclip::Jobs::ActiveJob reset_dummy(dummy_options) end diff --git a/spec/delayed_paperclip/class_methods_spec.rb b/spec/delayed_paperclip/class_methods_spec.rb index 8118167..c778fca 100644 --- a/spec/delayed_paperclip/class_methods_spec.rb +++ b/spec/delayed_paperclip/class_methods_spec.rb @@ -2,7 +2,7 @@ describe DelayedPaperclip::ClassMethods do before :each do - DelayedPaperclip.options[:background_job_class] = DelayedPaperclip::Jobs::Resque + DelayedPaperclip.options[:background_job_class] = DelayedPaperclip::Jobs::ActiveJob reset_dummy(with_processed: false) end diff --git a/spec/delayed_paperclip/instance_methods_spec.rb b/spec/delayed_paperclip/instance_methods_spec.rb index ec7ee03..25b7766 100644 --- a/spec/delayed_paperclip/instance_methods_spec.rb +++ b/spec/delayed_paperclip/instance_methods_spec.rb @@ -2,7 +2,7 @@ describe DelayedPaperclip::InstanceMethods do before :each do - DelayedPaperclip.options[:background_job_class] = DelayedPaperclip::Jobs::Resque + DelayedPaperclip.options[:background_job_class] = DelayedPaperclip::Jobs::ActiveJob reset_dummy end diff --git a/spec/delayed_paperclip/url_generator_spec.rb b/spec/delayed_paperclip/url_generator_spec.rb index 0fcd437..54fee52 100644 --- a/spec/delayed_paperclip/url_generator_spec.rb +++ b/spec/delayed_paperclip/url_generator_spec.rb @@ -2,7 +2,7 @@ describe DelayedPaperclip::UrlGenerator do before :each do - DelayedPaperclip.options[:background_job_class] = DelayedPaperclip::Jobs::Resque + DelayedPaperclip.options[:background_job_class] = DelayedPaperclip::Jobs::ActiveJob reset_dummy(dummy_options) end diff --git a/spec/delayed_paperclip_spec.rb b/spec/delayed_paperclip_spec.rb index 2cb4f23..8a39626 100644 --- a/spec/delayed_paperclip_spec.rb +++ b/spec/delayed_paperclip_spec.rb @@ -8,12 +8,12 @@ context "with Resque adapter" do before :each do - DelayedPaperclip.options[:background_job_class] = DelayedPaperclip::Jobs::Resque + DelayedPaperclip.options[:background_job_class] = DelayedPaperclip::Jobs::ActiveJob end describe ".options" do it ".options returns basic options" do - DelayedPaperclip.options.should == {:background_job_class => DelayedPaperclip::Jobs::Resque, + DelayedPaperclip.options.should == {:background_job_class => DelayedPaperclip::Jobs::ActiveJob, :url_with_processing => true, :processing_image_url => nil, :queue => "paperclip"} @@ -22,13 +22,13 @@ describe ".processor" do it ".processor returns processor" do - DelayedPaperclip.processor.should == DelayedPaperclip::Jobs::Resque + DelayedPaperclip.processor.should == DelayedPaperclip::Jobs::ActiveJob end end describe ".enqueue" do it "delegates to processor" do - DelayedPaperclip::Jobs::Resque.expects(:enqueue_delayed_paperclip).with("Dummy", 1, :image) + DelayedPaperclip::Jobs::ActiveJob.expects(:enqueue_delayed_paperclip).with("Dummy", 1, :image) DelayedPaperclip.enqueue("Dummy", 1, :image) end end diff --git a/spec/integration/base_delayed_paperclip_spec.rb b/spec/integration/base_delayed_paperclip_spec.rb index 1127c06..7cd767a 100644 --- a/spec/integration/base_delayed_paperclip_spec.rb +++ b/spec/integration/base_delayed_paperclip_spec.rb @@ -2,7 +2,7 @@ describe "Base Delayed Paperclip Integration" do before :each do - DelayedPaperclip.options[:background_job_class] = DelayedPaperclip::Jobs::Resque + DelayedPaperclip.options[:background_job_class] = DelayedPaperclip::Jobs::ActiveJob Resque.remove_queue(:paperclip) end diff --git a/spec/integration/delayed_job_spec.rb b/spec/integration/delayed_job_spec.rb deleted file mode 100644 index 7581311..0000000 --- a/spec/integration/delayed_job_spec.rb +++ /dev/null @@ -1,63 +0,0 @@ -require 'spec_helper' -require 'delayed_job' - -Delayed::Worker.backend = :active_record - -describe "Delayed Job" do - before :each do - DelayedPaperclip.options[:background_job_class] = DelayedPaperclip::Jobs::DelayedJob - build_delayed_jobs - end - - let(:dummy) { Dummy.new(:image => File.open("#{ROOT}/fixtures/12k.png")) } - - describe "integration tests" do - include_examples "base usage" - end - - describe "perform job" do - before :each do - DelayedPaperclip.options[:url_with_processing] = true - reset_dummy - end - - it "performs a job" do - dummy.image = File.open("#{ROOT}/fixtures/12k.png") - Paperclip::Attachment.any_instance.expects(:reprocess!) - dummy.save! - Delayed::Job.last.payload_object.perform - end - - it "is deprecated" do - ActiveSupport::Deprecation.expects(:warn) - - dummy.image = File.open("#{ROOT}/fixtures/12k.png") - Paperclip::Attachment.any_instance.expects(:reprocess!) - dummy.save! - Delayed::Job.last.payload_object.perform - end - end - - def process_jobs - Delayed::Worker.new.work_off - end - - def jobs_count(queue = nil) - Delayed::Job.count - end - - def build_delayed_jobs - ActiveRecord::Base.connection.create_table :delayed_jobs, :force => true do |table| - table.integer :priority, :default => 0 # Allows some jobs to jump to the front of the queue - table.integer :attempts, :default => 0 # Provides for retries, but still fail eventually. - table.text :handler # YAML-encoded string of the object that will do work - table.string :last_error # reason for last failure (See Note below) - table.datetime :run_at # When to run. Could be Time.now for immediately, or sometime in the future. - table.datetime :locked_at # Set when a client is working on this object - table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead) - table.string :locked_by # Who is working on this object (if locked) - table.string :queue - table.timestamps null: true - end - end -end diff --git a/spec/integration/resque_spec.rb b/spec/integration/resque_spec.rb deleted file mode 100644 index 008f9fe..0000000 --- a/spec/integration/resque_spec.rb +++ /dev/null @@ -1,47 +0,0 @@ -require 'spec_helper' -require 'resque' - -describe "Resque" do - before :each do - DelayedPaperclip.options[:background_job_class] = DelayedPaperclip::Jobs::Resque - Resque.remove_queue(:paperclip) - end - - let(:dummy) { Dummy.new(:image => File.open("#{ROOT}/fixtures/12k.png")) } - - describe "integration tests" do - include_examples "base usage" - end - - describe "perform job" do - before :each do - DelayedPaperclip.options[:url_with_processing] = true - reset_dummy - end - - it "performs a job" do - dummy.image = File.open("#{ROOT}/fixtures/12k.png") - Paperclip::Attachment.any_instance.expects(:reprocess!) - dummy.save! - DelayedPaperclip::Jobs::Resque.perform(dummy.class.name, dummy.id, :image) - end - - it "is deprecated" do - ActiveSupport::Deprecation.expects(:warn) - - dummy.image = File.open("#{ROOT}/fixtures/12k.png") - Paperclip::Attachment.any_instance.expects(:reprocess!) - dummy.save! - DelayedPaperclip::Jobs::Resque.perform(dummy.class.name, dummy.id, :image) - end - end - - def process_jobs - worker = Resque::Worker.new(:paperclip) - worker.process - end - - def jobs_count(queue = :paperclip) - Resque.size(queue) - end -end diff --git a/spec/integration/sidekiq_spec.rb b/spec/integration/sidekiq_spec.rb deleted file mode 100644 index 3ceb805..0000000 --- a/spec/integration/sidekiq_spec.rb +++ /dev/null @@ -1,54 +0,0 @@ -require 'spec_helper' -require 'sidekiq/testing' - -describe "Sidekiq" do - before :each do - Sidekiq.logger.level = Logger::ERROR - DelayedPaperclip.options[:background_job_class] = DelayedPaperclip::Jobs::Sidekiq - Sidekiq::Queues["paperclip"].clear - end - - let(:dummy) { Dummy.new(:image => File.open("#{ROOT}/fixtures/12k.png")) } - - describe "integration tests" do - include_examples "base usage" - end - - describe "perform job" do - before :each do - DelayedPaperclip.options[:url_with_processing] = true - reset_dummy - end - - it "performs a job" do - dummy.image = File.open("#{ROOT}/fixtures/12k.png") - Paperclip::Attachment.any_instance.expects(:reprocess!) - dummy.save! - DelayedPaperclip::Jobs::Sidekiq.new.perform(dummy.class.name, dummy.id, :image) - end - - it "is deprecated" do - ActiveSupport::Deprecation.expects(:warn) - - dummy.image = File.open("#{ROOT}/fixtures/12k.png") - Paperclip::Attachment.any_instance.expects(:reprocess!) - dummy.save! - DelayedPaperclip::Jobs::Sidekiq.new.perform(dummy.class.name, dummy.id, :image) - end - end - - def process_jobs - Sidekiq::Queues["paperclip"].each do |job| - worker = job["class"].constantize.new - args = job["args"] - begin - worker.perform(*args) - rescue # Assume sidekiq handle exception properly - end - end - end - - def jobs_count(queue = "paperclip") - Sidekiq::Queues[queue].size - end -end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ad8d234..e62a324 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,10 +5,8 @@ require 'rspec' require 'fakeredis/rspec' require 'mocha/api' -begin - require 'active_job' -rescue LoadError -end + +require 'active_job' begin require 'pry' @@ -28,9 +26,6 @@ ActiveRecord::Base.raise_in_transactional_callbacks = true end -require "active_support/deprecation" -ActiveSupport::Deprecation.silenced = true - # Connect to sqlite ActiveRecord::Base.establish_connection( "adapter" => "sqlite3", @@ -57,7 +52,7 @@ def reset_global_default_options DelayedPaperclip.options.merge!({ - :background_job_class => DelayedPaperclip::Jobs::Resque, + :background_job_class => DelayedPaperclip::Jobs::ActiveJob, :url_with_processing => true, :processing_image_url => nil })