mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Lazy-load adapters, fixes #6
This commit is contained in:
parent
68543de836
commit
91461dc7a9
8 changed files with 67 additions and 8 deletions
|
@ -1,9 +1,9 @@
|
|||
require 'active_job/errors'
|
||||
require 'active_job/queue_adapters/inline_adapter'
|
||||
require 'active_job/queue_adapters/resque_adapter'
|
||||
require 'active_job/queue_adapters/sidekiq_adapter'
|
||||
require 'active_job/queue_adapters/sucker_punch_adapter'
|
||||
require 'active_support/core_ext/string/inflections'
|
||||
|
||||
module ActiveJob
|
||||
|
||||
class Base
|
||||
cattr_accessor(:queue_adapter) { ActiveJob::QueueAdapters::InlineAdapter }
|
||||
cattr_accessor(:queue_base_name) { "active_jobs" }
|
||||
|
@ -13,10 +13,25 @@ module ActiveJob
|
|||
def enqueue(*args)
|
||||
queue_adapter.queue self, *args
|
||||
end
|
||||
|
||||
|
||||
def queue_as(part_name)
|
||||
self.queue_name = "#{queue_base_name}_#{part_name}"
|
||||
end
|
||||
|
||||
def adapter=(adapter_name)
|
||||
adapter_name = adapter_name.to_s
|
||||
unless %w(inline resque sidekiq sucker_punch).include?(adapter_name)
|
||||
fail ActiveJob::NotImplementedError
|
||||
end
|
||||
|
||||
begin
|
||||
require_relative "queue_adapters/#{adapter_name}_adapter"
|
||||
ActiveJob::Base.queue_adapter = "ActiveJob::QueueAdapters::#{adapter_name.camelize}Adapter".constantize
|
||||
rescue
|
||||
fail ActiveJob::Error.new("#{adapter_name} is missing")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
12
lib/active_job/errors.rb
Normal file
12
lib/active_job/errors.rb
Normal file
|
@ -0,0 +1,12 @@
|
|||
module ActiveJob
|
||||
|
||||
class NotImplementedError < ::NotImplementedError #:nodoc:
|
||||
end
|
||||
|
||||
class Error < ::StandardError #:nodoc:
|
||||
def initialize(message = nil)
|
||||
super(message)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
|
@ -2,7 +2,7 @@ require 'resque'
|
|||
|
||||
require 'active_support/core_ext/enumerable'
|
||||
require 'active_support/core_ext/array/access'
|
||||
require 'active_support/core_ext/string/inflections'
|
||||
|
||||
|
||||
|
||||
module ActiveJob
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
ActiveJob::Base.adapter = :inline
|
|
@ -1,2 +1,2 @@
|
|||
ActiveJob::Base.queue_adapter = ActiveJob::QueueAdapters::ResqueAdapter
|
||||
ActiveJob::Base.adapter = :resque
|
||||
Resque.inline = true
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
require 'sidekiq/testing/inline'
|
||||
ActiveJob::Base.queue_adapter = ActiveJob::QueueAdapters::SidekiqAdapter
|
||||
ActiveJob::Base.adapter = :sidekiq
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
require 'sucker_punch/testing/inline'
|
||||
ActiveJob::Base.queue_adapter = ActiveJob::QueueAdapters::SuckerPunchAdapter
|
||||
ActiveJob::Base.adapter = :sucker_punch
|
||||
|
|
31
test/cases/adapter_test.rb
Normal file
31
test/cases/adapter_test.rb
Normal file
|
@ -0,0 +1,31 @@
|
|||
require 'helper'
|
||||
|
||||
class AdapterTest < ActiveSupport::TestCase
|
||||
def setup
|
||||
@old_adapter = ActiveJob::Base.queue_adapter
|
||||
end
|
||||
|
||||
test 'should load inline adapter' do
|
||||
ActiveJob::Base.adapter = :inline
|
||||
assert_equal ActiveJob::QueueAdapters::InlineAdapter, ActiveJob::Base.queue_adapter
|
||||
end
|
||||
|
||||
test 'should load resque adapter' do
|
||||
ActiveJob::Base.adapter = :resque
|
||||
assert_equal ActiveJob::QueueAdapters::ResqueAdapter, ActiveJob::Base.queue_adapter
|
||||
end
|
||||
|
||||
test 'should load sidekiq adapter' do
|
||||
ActiveJob::Base.adapter = :sidekiq
|
||||
assert_equal ActiveJob::QueueAdapters::SidekiqAdapter, ActiveJob::Base.queue_adapter
|
||||
end
|
||||
|
||||
test 'should load sucker punch adapter' do
|
||||
ActiveJob::Base.adapter = :sucker_punch
|
||||
assert_equal ActiveJob::QueueAdapters::SuckerPunchAdapter, ActiveJob::Base.queue_adapter
|
||||
end
|
||||
|
||||
def teardown
|
||||
ActiveJob::Base.queue_adapter = @old_adapter
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue