1
0
Fork 0
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:
Abdelkader Boudih 2014-05-18 20:59:12 +00:00
parent 68543de836
commit 91461dc7a9
8 changed files with 67 additions and 8 deletions

View file

@ -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
View 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

View file

@ -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

View file

@ -0,0 +1 @@
ActiveJob::Base.adapter = :inline

View file

@ -1,2 +1,2 @@
ActiveJob::Base.queue_adapter = ActiveJob::QueueAdapters::ResqueAdapter
ActiveJob::Base.adapter = :resque
Resque.inline = true

View file

@ -1,2 +1,2 @@
require 'sidekiq/testing/inline'
ActiveJob::Base.queue_adapter = ActiveJob::QueueAdapters::SidekiqAdapter
ActiveJob::Base.adapter = :sidekiq

View file

@ -1,2 +1,2 @@
require 'sucker_punch/testing/inline'
ActiveJob::Base.queue_adapter = ActiveJob::QueueAdapters::SuckerPunchAdapter
ActiveJob::Base.adapter = :sucker_punch

View 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