aboutsummaryrefslogtreecommitdiffstats
path: root/activejob
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2014-08-29 14:54:08 -0700
committerDavid Heinemeier Hansson <david@loudthinking.com>2014-08-29 14:54:08 -0700
commit7475b43cdbbbf7456e798210cb97ef20f2225166 (patch)
tree04ae517943ccc476ca0a8b9b3bdbb21949a558c1 /activejob
parent6a23bf0f4c33151e0cec0648e271dc6f5ab3f686 (diff)
parent4445478df311a74797d8dc4945c40662f9c1442a (diff)
downloadrails-7475b43cdbbbf7456e798210cb97ef20f2225166.tar.gz
rails-7475b43cdbbbf7456e798210cb97ef20f2225166.tar.bz2
rails-7475b43cdbbbf7456e798210cb97ef20f2225166.zip
Merge branch 'master' of github.com:rails/rails
Diffstat (limited to 'activejob')
-rw-r--r--activejob/README.md2
-rw-r--r--activejob/activejob.gemspec1
-rw-r--r--activejob/lib/active_job.rb3
-rw-r--r--activejob/lib/active_job/arguments.rb15
-rw-r--r--activejob/lib/active_job/callbacks.rb4
-rw-r--r--activejob/lib/active_job/logging.rb8
-rw-r--r--activejob/lib/active_job/queue_adapter.rb1
-rw-r--r--activejob/lib/active_job/queue_adapters.rb16
-rw-r--r--activejob/lib/active_job/queue_adapters/backburner_adapter.rb3
-rw-r--r--activejob/lib/active_job/queue_adapters/que_adapter.rb2
-rw-r--r--activejob/lib/active_job/version.rb2
-rw-r--r--activejob/lib/rails/generators/job/job_generator.rb6
-rw-r--r--activejob/test/adapters/que.rb2
-rw-r--r--activejob/test/cases/parameters_test.rb6
-rw-r--r--activejob/test/helper.rb30
-rw-r--r--activejob/test/jobs/gid_job.rb2
-rw-r--r--activejob/test/jobs/hello_job.rb2
-rw-r--r--activejob/test/jobs/rescue_job.rb2
-rw-r--r--activejob/test/support/job_buffer.rb19
-rw-r--r--activejob/test/support/que/inline.rb9
20 files changed, 88 insertions, 47 deletions
diff --git a/activejob/README.md b/activejob/README.md
index e48070bcfc..1f300fcf62 100644
--- a/activejob/README.md
+++ b/activejob/README.md
@@ -2,7 +2,7 @@
Active Job is a framework for declaring jobs and making them run on a variety
of queueing backends. These jobs can be everything from regularly scheduled
-clean-ups, billing charges, or mailings. Anything that can be chopped up into
+clean-ups, to billing charges, to mailings. Anything that can be chopped up into
small units of work and run in parallel, really.
It also serves as the backend for ActionMailer's #deliver_later functionality
diff --git a/activejob/activejob.gemspec b/activejob/activejob.gemspec
index c74daa5045..9944b2a1bd 100644
--- a/activejob/activejob.gemspec
+++ b/activejob/activejob.gemspec
@@ -18,5 +18,6 @@ Gem::Specification.new do |s|
s.files = Dir['CHANGELOG.md', 'MIT-LICENSE', 'README.md', 'lib/**/*']
s.require_path = 'lib'
+ s.add_dependency 'activesupport', version
s.add_dependency 'globalid', '>= 0.2.3'
end
diff --git a/activejob/lib/active_job.rb b/activejob/lib/active_job.rb
index ef92406725..29123170b8 100644
--- a/activejob/lib/active_job.rb
+++ b/activejob/lib/active_job.rb
@@ -30,4 +30,5 @@ module ActiveJob
extend ActiveSupport::Autoload
autoload :Base
-end \ No newline at end of file
+ autoload :QueueAdapters
+end
diff --git a/activejob/lib/active_job/arguments.rb b/activejob/lib/active_job/arguments.rb
index 369e716912..e54f4afbc7 100644
--- a/activejob/lib/active_job/arguments.rb
+++ b/activejob/lib/active_job/arguments.rb
@@ -1,4 +1,7 @@
module ActiveJob
+ # Raised when an exception is raised during job arguments deserialization.
+ #
+ # Wraps the original exception raised as +original_exception+.
class DeserializationError < StandardError
attr_reader :original_exception
@@ -9,6 +12,14 @@ module ActiveJob
end
end
+ # Raised when an unsupported argument type is being set as job argument. We
+ # currently support NilClass, Fixnum, Float, String, TrueClass, FalseClass,
+ # Bignum and object that can be represented as GlobalIDs (ex: Active Record).
+ # Also raised if you set the key for a Hash something else than a string or
+ # a symbol.
+ class SerializationError < ArgumentError
+ end
+
module Arguments
extend self
TYPE_WHITELIST = [ NilClass, Fixnum, Float, String, TrueClass, FalseClass, Bignum ]
@@ -33,7 +44,7 @@ module ActiveJob
when Hash
Hash[ argument.map { |key, value| [ serialize_hash_key(key), serialize_argument(value) ] } ]
else
- raise "Unsupported argument type: #{argument.class.name}"
+ raise SerializationError.new("Unsupported argument type: #{argument.class.name}")
end
end
@@ -55,7 +66,7 @@ module ActiveJob
when String, Symbol
key.to_s
else
- raise "Unsupported hash key type: #{key.class.name}"
+ raise SerializationError.new("Unsupported hash key type: #{key.class.name}")
end
end
end
diff --git a/activejob/lib/active_job/callbacks.rb b/activejob/lib/active_job/callbacks.rb
index af92031bc9..cafa3438c0 100644
--- a/activejob/lib/active_job/callbacks.rb
+++ b/activejob/lib/active_job/callbacks.rb
@@ -3,8 +3,8 @@ require 'active_support/callbacks'
module ActiveJob
# = Active Job Callbacks
#
- # Active Job provides hooks during the lifecycle of a job. Callbacks allows you to trigger
- # logic during the lifecycle of a job. Available callbacks:
+ # Active Job provides hooks during the lifecycle of a job. Callbacks allow you
+ # to trigger logic during the lifecycle of a job. Available callbacks are:
#
# * <tt>before_enqueue</tt>
# * <tt>around_enqueue</tt>
diff --git a/activejob/lib/active_job/logging.rb b/activejob/lib/active_job/logging.rb
index d9e544acf5..ae098a80f3 100644
--- a/activejob/lib/active_job/logging.rb
+++ b/activejob/lib/active_job/logging.rb
@@ -52,19 +52,19 @@ module ActiveJob
class LogSubscriber < ActiveSupport::LogSubscriber
def enqueue(event)
- info "Enqueued #{event.payload[:job].name} (Job ID: #{event.payload[:job_id]}) to #{queue_name(event)}" + args_info(event)
+ info { "Enqueued #{event.payload[:job].name} (Job ID: #{event.payload[:job_id]}) to #{queue_name(event)}" + args_info(event) }
end
def enqueue_at(event)
- info "Enqueued #{event.payload[:job].name} (Job ID: #{event.payload[:job_id]}) to #{queue_name(event)} at #{enqueued_at(event)}" + args_info(event)
+ info { "Enqueued #{event.payload[:job].name} (Job ID: #{event.payload[:job_id]}) to #{queue_name(event)} at #{enqueued_at(event)}" + args_info(event) }
end
def perform_start(event)
- info "Performing #{event.payload[:job].name} from #{queue_name(event)}" + args_info(event)
+ info { "Performing #{event.payload[:job].name} from #{queue_name(event)}" + args_info(event) }
end
def perform(event)
- info "Performed #{event.payload[:job].name} from #{queue_name(event)} in #{event.duration.round(2).to_s}ms"
+ info { "Performed #{event.payload[:job].name} from #{queue_name(event)} in #{event.duration.round(2).to_s}ms" }
end
private
diff --git a/activejob/lib/active_job/queue_adapter.rb b/activejob/lib/active_job/queue_adapter.rb
index 8f2f8b86ea..13c23abce4 100644
--- a/activejob/lib/active_job/queue_adapter.rb
+++ b/activejob/lib/active_job/queue_adapter.rb
@@ -17,7 +17,6 @@ module ActiveJob
private
def load_adapter(name)
- require "active_job/queue_adapters/#{name}_adapter"
"ActiveJob::QueueAdapters::#{name.to_s.camelize}Adapter".constantize
end
end
diff --git a/activejob/lib/active_job/queue_adapters.rb b/activejob/lib/active_job/queue_adapters.rb
new file mode 100644
index 0000000000..007068ff0a
--- /dev/null
+++ b/activejob/lib/active_job/queue_adapters.rb
@@ -0,0 +1,16 @@
+module ActiveJob
+ module QueueAdapters
+ extend ActiveSupport::Autoload
+
+ autoload :InlineAdapter
+ autoload :BackburnerAdapter
+ autoload :DelayedJobAdapter
+ autoload :QuAdapter
+ autoload :QueAdapter
+ autoload :QueueClassicAdapter
+ autoload :ResqueAdapter
+ autoload :SidekiqAdapter
+ autoload :SneakersAdapter
+ autoload :SuckerPunchAdapter
+ end
+end
diff --git a/activejob/lib/active_job/queue_adapters/backburner_adapter.rb b/activejob/lib/active_job/queue_adapters/backburner_adapter.rb
index 6fe2d4eb53..8ebee36e45 100644
--- a/activejob/lib/active_job/queue_adapters/backburner_adapter.rb
+++ b/activejob/lib/active_job/queue_adapters/backburner_adapter.rb
@@ -9,7 +9,8 @@ module ActiveJob
end
def enqueue_at(job, timestamp, *args)
- raise NotImplementedError
+ delay = timestamp - Time.current.to_f
+ Backburner::Worker.enqueue JobWrapper, [ job.name, *args ], queue: job.queue_name, delay: delay
end
end
diff --git a/activejob/lib/active_job/queue_adapters/que_adapter.rb b/activejob/lib/active_job/queue_adapters/que_adapter.rb
index 0b87deb4e0..9c84c74f83 100644
--- a/activejob/lib/active_job/queue_adapters/que_adapter.rb
+++ b/activejob/lib/active_job/queue_adapters/que_adapter.rb
@@ -9,7 +9,7 @@ module ActiveJob
end
def enqueue_at(job, timestamp, *args)
- raise NotImplementedError
+ JobWrapper.enqueue job.name, *args, queue: job.queue_name, run_at: Time.at(timestamp)
end
end
diff --git a/activejob/lib/active_job/version.rb b/activejob/lib/active_job/version.rb
index 7e646fa3c4..971ba9fe0c 100644
--- a/activejob/lib/active_job/version.rb
+++ b/activejob/lib/active_job/version.rb
@@ -1,7 +1,7 @@
require_relative 'gem_version'
module ActiveJob
- # Returns the version of the currently loaded ActiveJob as a <tt>Gem::Version</tt>
+ # Returns the version of the currently loaded Active Job as a <tt>Gem::Version</tt>
def self.version
gem_version
end
diff --git a/activejob/lib/rails/generators/job/job_generator.rb b/activejob/lib/rails/generators/job/job_generator.rb
index 78a9c27606..979ffcb748 100644
--- a/activejob/lib/rails/generators/job/job_generator.rb
+++ b/activejob/lib/rails/generators/job/job_generator.rb
@@ -7,12 +7,14 @@ module Rails
class_option :queue, type: :string, default: 'default', desc: 'The queue name for the generated job'
+ check_class_collision suffix: 'Job'
+
+ hook_for :test_framework
+
def self.default_generator_root
File.dirname(__FILE__)
end
- check_class_collision suffix: 'Job'
-
def create_job_file
template 'job.rb', File.join('app/jobs', class_path, "#{file_name}_job.rb")
end
diff --git a/activejob/test/adapters/que.rb b/activejob/test/adapters/que.rb
index 640061bf54..e6abc57457 100644
--- a/activejob/test/adapters/que.rb
+++ b/activejob/test/adapters/que.rb
@@ -1,2 +1,4 @@
+require 'support/que/inline'
+
ActiveJob::Base.queue_adapter = :que
Que.mode = :sync
diff --git a/activejob/test/cases/parameters_test.rb b/activejob/test/cases/parameters_test.rb
index 76e8a8059a..78853c51e1 100644
--- a/activejob/test/cases/parameters_test.rb
+++ b/activejob/test/cases/parameters_test.rb
@@ -19,7 +19,7 @@ class ParameterSerializationTest < ActiveSupport::TestCase
assert_equal [ [ 1 ] ], ActiveJob::Arguments.serialize([ [ 1 ] ])
assert_equal [ 1_000_000_000_000_000_000_000 ], ActiveJob::Arguments.serialize([ 1_000_000_000_000_000_000_000 ])
- err = assert_raises RuntimeError do
+ err = assert_raises ActiveJob::SerializationError do
ActiveJob::Arguments.serialize([ 1, self ])
end
assert_equal "Unsupported argument type: #{self.class.name}", err.message
@@ -31,14 +31,14 @@ class ParameterSerializationTest < ActiveSupport::TestCase
end
test 'should dive deep into arrays or hashes and raise exception on complex objects' do
- err = assert_raises RuntimeError do
+ err = assert_raises ActiveJob::SerializationError do
ActiveJob::Arguments.serialize([ 1, [self] ])
end
assert_equal "Unsupported argument type: #{self.class.name}", err.message
end
test 'shoud dive deep into hashes and allow raise exception on not string/symbol keys' do
- err = assert_raises RuntimeError do
+ err = assert_raises ActiveJob::SerializationError do
ActiveJob::Arguments.serialize([ [ { 1 => 2 } ] ])
end
assert_equal "Unsupported hash key type: Fixnum", err.message
diff --git a/activejob/test/helper.rb b/activejob/test/helper.rb
index ca67700273..85094387ef 100644
--- a/activejob/test/helper.rb
+++ b/activejob/test/helper.rb
@@ -10,41 +10,15 @@ def sidekiq?
@adapter == 'sidekiq'
end
-def rubinius?
- RUBY_ENGINE == 'rbx'
-end
-
def ruby_193?
RUBY_VERSION == '1.9.3' && RUBY_ENGINE != 'java'
end
-#Sidekiq don't work with MRI 1.9.3
-#Travis uses rbx 2.6 which don't support unicode characters in methods.
-#Remove the check when Travis change to rbx 2.7+
-exit if sidekiq? && (ruby_193? || rubinius?)
+# Sidekiq doesn't work with MRI 1.9.3
+exit if sidekiq? && ruby_193?
require "adapters/#{@adapter}"
require 'active_support/testing/autorun'
ActiveJob::Base.logger.level = Logger::DEBUG
-
-module JobBuffer
- class << self
- def clear
- @buffer = []
- end
-
- def add(value)
- @buffer << value
- end
-
- def values
- @buffer
- end
-
- def last_value
- @buffer.last
- end
- end
-end
diff --git a/activejob/test/jobs/gid_job.rb b/activejob/test/jobs/gid_job.rb
index 35c2366ec4..e485bfa2dd 100644
--- a/activejob/test/jobs/gid_job.rb
+++ b/activejob/test/jobs/gid_job.rb
@@ -1,3 +1,5 @@
+require_relative '../support/job_buffer'
+
class GidJob < ActiveJob::Base
def perform(person)
JobBuffer.add("Person with ID: #{person.id}")
diff --git a/activejob/test/jobs/hello_job.rb b/activejob/test/jobs/hello_job.rb
index 4c6256af0d..022fa58e4a 100644
--- a/activejob/test/jobs/hello_job.rb
+++ b/activejob/test/jobs/hello_job.rb
@@ -1,3 +1,5 @@
+require_relative '../support/job_buffer'
+
class HelloJob < ActiveJob::Base
def perform(greeter = "David")
JobBuffer.add("#{greeter} says hello")
diff --git a/activejob/test/jobs/rescue_job.rb b/activejob/test/jobs/rescue_job.rb
index e9cb37d1c4..6b6e74e9d0 100644
--- a/activejob/test/jobs/rescue_job.rb
+++ b/activejob/test/jobs/rescue_job.rb
@@ -1,3 +1,5 @@
+require_relative '../support/job_buffer'
+
class RescueJob < ActiveJob::Base
class OtherError < StandardError; end
diff --git a/activejob/test/support/job_buffer.rb b/activejob/test/support/job_buffer.rb
new file mode 100644
index 0000000000..620cb5288d
--- /dev/null
+++ b/activejob/test/support/job_buffer.rb
@@ -0,0 +1,19 @@
+module JobBuffer
+ class << self
+ def clear
+ values.clear
+ end
+
+ def add(value)
+ values << value
+ end
+
+ def values
+ @values ||= []
+ end
+
+ def last_value
+ values.last
+ end
+ end
+end
diff --git a/activejob/test/support/que/inline.rb b/activejob/test/support/que/inline.rb
new file mode 100644
index 0000000000..2e210acb6b
--- /dev/null
+++ b/activejob/test/support/que/inline.rb
@@ -0,0 +1,9 @@
+require 'que'
+
+Que::Job.class_eval do
+ class << self; alias_method :original_enqueue, :enqueue; end
+ def self.enqueue(*args)
+ args.pop if args.last.is_a?(Hash)
+ self.run(*args)
+ end
+end