From 3ed69cd5f54c6eda7ee8664aa14c53205ecbb6c5 Mon Sep 17 00:00:00 2001 From: Cristian Bica Date: Tue, 12 Aug 2014 13:53:46 +0300 Subject: Fixed failing tests; Load active_job in railtie; Renamed generator to job --- activejob/lib/active_job/railtie.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'activejob/lib/active_job') diff --git a/activejob/lib/active_job/railtie.rb b/activejob/lib/active_job/railtie.rb index 08555d1d77..16736374af 100644 --- a/activejob/lib/active_job/railtie.rb +++ b/activejob/lib/active_job/railtie.rb @@ -1,4 +1,5 @@ -require 'active_model/railtie' +require 'active_job' +require 'rails' module ActiveJob # = Active Job Railtie @@ -7,4 +8,4 @@ module ActiveJob ActiveSupport.on_load(:active_job) { self.logger = ::Rails.logger } end end -end \ No newline at end of file +end -- cgit v1.2.3 From 67f8b6b2bc7e7eca8723996b1303c3fafa5ed39b Mon Sep 17 00:00:00 2001 From: Abdelkader Boudih Date: Tue, 12 Aug 2014 18:24:19 +0000 Subject: Added ActionMailer::DeliverLater --- activejob/lib/active_job/logging.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activejob/lib/active_job') diff --git a/activejob/lib/active_job/logging.rb b/activejob/lib/active_job/logging.rb index d913aee03d..d20bc3efce 100644 --- a/activejob/lib/active_job/logging.rb +++ b/activejob/lib/active_job/logging.rb @@ -7,7 +7,7 @@ module ActiveJob included do cattr_accessor(:logger) { ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT)) } - around_enqueue do |job, block, _| + around_enqueue do |_, block, _| tag_logger do block.call end @@ -17,7 +17,7 @@ module ActiveJob tag_logger(job.class.name, job.job_id) do payload = {adapter: job.class.queue_adapter, job: job.class, args: job.arguments} ActiveSupport::Notifications.instrument("perform_start.active_job", payload.dup) - ActiveSupport::Notifications.instrument("perform.active_job", payload) do |payload| + ActiveSupport::Notifications.instrument("perform.active_job", payload) do block.call end end -- cgit v1.2.3 From 8147e22a4d09a159a2084fb7ac5c2cfa527a9422 Mon Sep 17 00:00:00 2001 From: Abdelkader Boudih Date: Thu, 14 Aug 2014 22:54:56 +0000 Subject: Update resque_adapter.rb --- activejob/lib/active_job/queue_adapters/resque_adapter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activejob/lib/active_job') diff --git a/activejob/lib/active_job/queue_adapters/resque_adapter.rb b/activejob/lib/active_job/queue_adapters/resque_adapter.rb index b228825f07..30a51485bd 100644 --- a/activejob/lib/active_job/queue_adapters/resque_adapter.rb +++ b/activejob/lib/active_job/queue_adapters/resque_adapter.rb @@ -7,7 +7,7 @@ begin rescue LoadError begin require 'resque_scheduler' - rescue LoadError + rescue LoadError => e $stderr.puts 'The ActiveJob resque adapter requires resque-scheduler. Please add it to your Gemfile and run bundle install' raise e end -- cgit v1.2.3 From 94ae25ecd5635f7f97a2e53afa8e3f82c408435d Mon Sep 17 00:00:00 2001 From: Cristian Bica Date: Fri, 15 Aug 2014 23:32:08 +0300 Subject: ActiveJob: Reworked queue_base_name as default_queue_name + Allow configure ActiveJob from app.config.active_job --- activejob/lib/active_job/queue_name.rb | 8 ++++---- activejob/lib/active_job/railtie.rb | 14 +++++++++++++- 2 files changed, 17 insertions(+), 5 deletions(-) (limited to 'activejob/lib/active_job') diff --git a/activejob/lib/active_job/queue_name.rb b/activejob/lib/active_job/queue_name.rb index 859ddad034..c2186d9fe9 100644 --- a/activejob/lib/active_job/queue_name.rb +++ b/activejob/lib/active_job/queue_name.rb @@ -3,16 +3,16 @@ module ActiveJob extend ActiveSupport::Concern module ClassMethods - mattr_accessor(:queue_base_name) { "active_jobs" } + mattr_accessor(:default_queue_name) { "default" } def queue_as(part_name) - self.queue_name = "#{queue_base_name}_#{part_name}" + self.queue_name = part_name.to_s.presence || default_queue_name end end included do class_attribute :queue_name - self.queue_name = queue_base_name + self.queue_name = default_queue_name end end -end \ No newline at end of file +end diff --git a/activejob/lib/active_job/railtie.rb b/activejob/lib/active_job/railtie.rb index 16736374af..7c1dd8f275 100644 --- a/activejob/lib/active_job/railtie.rb +++ b/activejob/lib/active_job/railtie.rb @@ -4,8 +4,20 @@ require 'rails' module ActiveJob # = Active Job Railtie class Railtie < Rails::Railtie # :nodoc: - initializer 'active_job' do + config.active_job = ActiveSupport::OrderedOptions.new + + initializer 'active_job.logger' do ActiveSupport.on_load(:active_job) { self.logger = ::Rails.logger } end + + initializer "active_job.set_configs" do |app| + options = app.config.active_job + options.queue_adapter ||= :inline + + ActiveSupport.on_load(:active_job) do + options.each { |k,v| send("#{k}=", v) } + end + end + end end -- cgit v1.2.3 From b2cabb7aceac9e2db0a9cc4fea8a4ef50d4ea132 Mon Sep 17 00:00:00 2001 From: Cristian Bica Date: Sat, 16 Aug 2014 01:31:39 +0300 Subject: Added docs for AJ::Callbacks; Added AJ to docs build map --- activejob/lib/active_job/callbacks.rb | 112 ++++++++++++++++++++++++++++++++-- 1 file changed, 108 insertions(+), 4 deletions(-) (limited to 'activejob/lib/active_job') diff --git a/activejob/lib/active_job/callbacks.rb b/activejob/lib/active_job/callbacks.rb index c69e4a3b55..af92031bc9 100644 --- a/activejob/lib/active_job/callbacks.rb +++ b/activejob/lib/active_job/callbacks.rb @@ -1,40 +1,144 @@ 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: + # + # * before_enqueue + # * around_enqueue + # * after_enqueue + # * before_perform + # * around_perform + # * after_perform + # module Callbacks extend ActiveSupport::Concern include ActiveSupport::Callbacks - + included do define_callbacks :perform define_callbacks :enqueue end - + module ClassMethods + # Defines a callback that will get called right before the + # job's perform method is executed. + # + # class VideoProcessJob < ActiveJob::Base + # queue_as :default + # + # before_perform do |job| + # UserMailer.notify_video_started_processing(job.arguments.first) + # end + # + # def perform(video_id) + # Video.find(video_id).process + # end + # end + # def before_perform(*filters, &blk) set_callback(:perform, :before, *filters, &blk) end + # Defines a callback that will get called right after the + # job's perform method has finished. + # + # class VideoProcessJob < ActiveJob::Base + # queue_as :default + # + # after_perform do |job| + # UserMailer.notify_video_processed(job.arguments.first) + # end + # + # def perform(video_id) + # Video.find(video_id).process + # end + # end + # def after_perform(*filters, &blk) set_callback(:perform, :after, *filters, &blk) end + # Defines a callback that will get called around the job's perform method. + # + # class VideoProcessJob < ActiveJob::Base + # queue_as :default + # + # around_perform do |job, block| + # UserMailer.notify_video_started_processing(job.arguments.first) + # block.call + # UserMailer.notify_video_processed(job.arguments.first) + # end + # + # def perform(video_id) + # Video.find(video_id).process + # end + # end + # def around_perform(*filters, &blk) set_callback(:perform, :around, *filters, &blk) end - + # Defines a callback that will get called right before the + # job is enqueued. + # + # class VideoProcessJob < ActiveJob::Base + # queue_as :default + # + # before_enqueue do |job| + # $statsd.increment "enqueue-video-job.try" + # end + # + # def perform(video_id) + # Video.find(video_id).process + # end + # end + # def before_enqueue(*filters, &blk) set_callback(:enqueue, :before, *filters, &blk) end + # Defines a callback that will get called right after the + # job is enqueued. + # + # class VideoProcessJob < ActiveJob::Base + # queue_as :default + # + # after_enqueue do |job| + # $statsd.increment "enqueue-video-job.success" + # end + # + # def perform(video_id) + # Video.find(video_id).process + # end + # end + # def after_enqueue(*filters, &blk) set_callback(:enqueue, :after, *filters, &blk) end + # Defines a callback that will get called before and after the + # job is enqueued. + # + # class VideoProcessJob < ActiveJob::Base + # queue_as :default + # + # around_enqueue do |job, block| + # $statsd.time "video-job.process" do + # block.call + # end + # end + # + # def perform(video_id) + # Video.find(video_id).process + # end + # end + # def around_enqueue(*filters, &blk) set_callback(:enqueue, :around, *filters, &blk) end end end -end \ No newline at end of file +end -- cgit v1.2.3 From fdc7dbc5b239f44243b9a973197ca7c031bc6313 Mon Sep 17 00:00:00 2001 From: Abdelkader Boudih Date: Fri, 15 Aug 2014 09:50:45 +0000 Subject: [ActiveJob] require files in logging.rb --- activejob/lib/active_job/logging.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'activejob/lib/active_job') diff --git a/activejob/lib/active_job/logging.rb b/activejob/lib/active_job/logging.rb index d20bc3efce..d9e544acf5 100644 --- a/activejob/lib/active_job/logging.rb +++ b/activejob/lib/active_job/logging.rb @@ -1,4 +1,6 @@ require 'active_support/core_ext/string/filters' +require 'active_support/tagged_logging' +require 'active_support/logger' module ActiveJob module Logging -- cgit v1.2.3 From 3954fdf5f325719143df12860f65d778350d12ed Mon Sep 17 00:00:00 2001 From: Abdelkader Boudih Date: Fri, 15 Aug 2014 10:04:06 +0000 Subject: [ActiveJob] Convert ActiveJob::Arguments into module --- activejob/lib/active_job/arguments.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'activejob/lib/active_job') diff --git a/activejob/lib/active_job/arguments.rb b/activejob/lib/active_job/arguments.rb index ef6a3fce1d..8486a9e918 100644 --- a/activejob/lib/active_job/arguments.rb +++ b/activejob/lib/active_job/arguments.rb @@ -2,19 +2,20 @@ require 'active_model/global_locator' require 'active_model/global_identification' module ActiveJob - class Arguments + module Arguments + extend self TYPE_WHITELIST = [ NilClass, Fixnum, Float, String, TrueClass, FalseClass, Bignum ] - def self.serialize(arguments) + def serialize(arguments) arguments.map { |argument| serialize_argument(argument) } end - def self.deserialize(arguments) + def deserialize(arguments) arguments.map { |argument| deserialize_argument(argument) } end private - def self.serialize_argument(argument) + def serialize_argument(argument) case argument when ActiveModel::GlobalIdentification argument.global_id.to_s @@ -29,7 +30,7 @@ module ActiveJob end end - def self.deserialize_argument(argument) + def deserialize_argument(argument) case argument when Array deserialize(argument) @@ -40,7 +41,7 @@ module ActiveJob end end - def self.serialize_hash_key(key) + def serialize_hash_key(key) case key when String, Symbol key.to_s -- cgit v1.2.3 From b06d91924ffeb07c5ca6a3d96eff288267ee80c6 Mon Sep 17 00:00:00 2001 From: Abdelkader Boudih Date: Fri, 15 Aug 2014 10:11:58 +0000 Subject: [ActiveJob] remove ruby warnings --- activejob/lib/active_job/enqueuing.rb | 2 +- activejob/lib/active_job/execution.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'activejob/lib/active_job') diff --git a/activejob/lib/active_job/enqueuing.rb b/activejob/lib/active_job/enqueuing.rb index e3ac11ba97..c00aab40da 100644 --- a/activejob/lib/active_job/enqueuing.rb +++ b/activejob/lib/active_job/enqueuing.rb @@ -57,7 +57,7 @@ module ActiveJob end def retry_now - self.class.enqueue *arguments + self.class.enqueue(*arguments) end def retry_in(interval) diff --git a/activejob/lib/active_job/execution.rb b/activejob/lib/active_job/execution.rb index 78ada3d908..181d049616 100644 --- a/activejob/lib/active_job/execution.rb +++ b/activejob/lib/active_job/execution.rb @@ -14,14 +14,14 @@ module ActiveJob self.arguments = Arguments.deserialize(serialized_args) run_callbacks :perform do - perform *arguments + perform(*arguments) end rescue => exception rescue_with_handler(exception) || raise(exception) end def perform(*) - raise NotImplementedError + fail NotImplementedError end end end -- cgit v1.2.3 From 59221cc4f1b8f87553455aad26905c4f28b424f8 Mon Sep 17 00:00:00 2001 From: Cristian Bica Date: Sat, 16 Aug 2014 15:49:03 +0300 Subject: [ActiveJob] make the resque-scheduler optional --- activejob/lib/active_job/queue_adapters/resque_adapter.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'activejob/lib/active_job') diff --git a/activejob/lib/active_job/queue_adapters/resque_adapter.rb b/activejob/lib/active_job/queue_adapters/resque_adapter.rb index 30a51485bd..384aa0c4cc 100644 --- a/activejob/lib/active_job/queue_adapters/resque_adapter.rb +++ b/activejob/lib/active_job/queue_adapters/resque_adapter.rb @@ -7,9 +7,8 @@ begin rescue LoadError begin require 'resque_scheduler' - rescue LoadError => e - $stderr.puts 'The ActiveJob resque adapter requires resque-scheduler. Please add it to your Gemfile and run bundle install' - raise e + rescue LoadError + false end end @@ -22,6 +21,10 @@ module ActiveJob end def enqueue_at(job, timestamp, *args) + unless Resque.respond_to?(:enqueue_at_with_queue) + raise NotImplementedError, "To be able to schedule jobs with Resque you need the " \ + "resque-scheduler gem. Please add it to your Gemfile and run bundle install" + end Resque.enqueue_at_with_queue job.queue_name, timestamp, JobWrapper, job.name, *args end end -- cgit v1.2.3 From 2f7b239fca6630e49ba8ad9df6fc7db25e1080f0 Mon Sep 17 00:00:00 2001 From: Abdelkader Boudih Date: Sun, 17 Aug 2014 01:06:30 +0000 Subject: [ActiveJob] Use globalid gem --- activejob/lib/active_job/arguments.rb | 7 ++----- activejob/lib/active_job/enqueuing.rb | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) (limited to 'activejob/lib/active_job') diff --git a/activejob/lib/active_job/arguments.rb b/activejob/lib/active_job/arguments.rb index 8486a9e918..b7572b0e29 100644 --- a/activejob/lib/active_job/arguments.rb +++ b/activejob/lib/active_job/arguments.rb @@ -1,6 +1,3 @@ -require 'active_model/global_locator' -require 'active_model/global_identification' - module ActiveJob module Arguments extend self @@ -17,7 +14,7 @@ module ActiveJob private def serialize_argument(argument) case argument - when ActiveModel::GlobalIdentification + when GlobalID::Identification argument.global_id.to_s when *TYPE_WHITELIST argument @@ -37,7 +34,7 @@ module ActiveJob when Hash Hash[ argument.map { |key, value| [ key, deserialize_argument(value) ] } ].with_indifferent_access else - ActiveModel::GlobalLocator.locate(argument) || argument + GlobalID::Locator.locate(argument) || argument end end diff --git a/activejob/lib/active_job/enqueuing.rb b/activejob/lib/active_job/enqueuing.rb index c00aab40da..3d00d51867 100644 --- a/activejob/lib/active_job/enqueuing.rb +++ b/activejob/lib/active_job/enqueuing.rb @@ -7,7 +7,7 @@ module ActiveJob module ClassMethods # Push a job onto the queue. The arguments must be legal JSON types # (string, int, float, nil, true, false, hash or array) or - # ActiveModel::GlobalIdentication instances. Arbitrary Ruby objects + # GlobalID::Identification instances. Arbitrary Ruby objects # are not supported. # # Returns an instance of the job class queued with args available in -- cgit v1.2.3 From 9a3426220145cf8862324f204eece64f3a6a4634 Mon Sep 17 00:00:00 2001 From: Abdelkader Boudih Date: Sun, 17 Aug 2014 19:16:43 +0000 Subject: [ActiveJob] Add deserialize_arguments method to job --- activejob/lib/active_job/execution.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'activejob/lib/active_job') diff --git a/activejob/lib/active_job/execution.rb b/activejob/lib/active_job/execution.rb index 181d049616..0e7b5bdd72 100644 --- a/activejob/lib/active_job/execution.rb +++ b/activejob/lib/active_job/execution.rb @@ -11,7 +11,7 @@ module ActiveJob def execute(job_id, *serialized_args) self.job_id = job_id - self.arguments = Arguments.deserialize(serialized_args) + self.arguments = deserialize_arguments(serialized_args) run_callbacks :perform do perform(*arguments) @@ -23,5 +23,11 @@ module ActiveJob def perform(*) fail NotImplementedError end + + private + def deserialize_arguments(serialized_args) + Arguments.deserialize(serialized_args) + end + end end -- cgit v1.2.3 From 3faa61ede58aa29400e4e062a799c61913ae213f Mon Sep 17 00:00:00 2001 From: Cristian Bica Date: Sun, 17 Aug 2014 23:48:44 +0300 Subject: [ActiveJob] raise DeserializationError when got an error deserializing --- activejob/lib/active_job/arguments.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'activejob/lib/active_job') diff --git a/activejob/lib/active_job/arguments.rb b/activejob/lib/active_job/arguments.rb index b7572b0e29..369e716912 100644 --- a/activejob/lib/active_job/arguments.rb +++ b/activejob/lib/active_job/arguments.rb @@ -1,4 +1,14 @@ module ActiveJob + class DeserializationError < StandardError + attr_reader :original_exception + + def initialize(e) + super ("Error while trying to deserialize arguments: #{e.message}") + @original_exception = e + set_backtrace e.backtrace + end + end + module Arguments extend self TYPE_WHITELIST = [ NilClass, Fixnum, Float, String, TrueClass, FalseClass, Bignum ] @@ -36,6 +46,8 @@ module ActiveJob else GlobalID::Locator.locate(argument) || argument end + rescue => e + raise DeserializationError.new(e) end def serialize_hash_key(key) -- cgit v1.2.3 From 080296be61105f4f941441a506a6a9fe5c562772 Mon Sep 17 00:00:00 2001 From: Abdelkader Boudih Date: Sun, 17 Aug 2014 23:17:38 +0000 Subject: [ActiveJob] require global_id/railtie --- activejob/lib/active_job/railtie.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activejob/lib/active_job') diff --git a/activejob/lib/active_job/railtie.rb b/activejob/lib/active_job/railtie.rb index 7c1dd8f275..6538ac1b30 100644 --- a/activejob/lib/active_job/railtie.rb +++ b/activejob/lib/active_job/railtie.rb @@ -1,5 +1,5 @@ +require 'global_id/railtie' require 'active_job' -require 'rails' module ActiveJob # = Active Job Railtie -- cgit v1.2.3