1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activejob/lib/active_job/core.rb

152 lines
5 KiB
Ruby
Raw Normal View History

2017-07-09 13:49:52 -04:00
# frozen_string_literal: true
2014-08-25 10:34:50 -04:00
module ActiveJob
# Provides general behavior that will be included into every Active Job
# object that inherits from ActiveJob::Base.
2014-08-25 10:34:50 -04:00
module Core
extend ActiveSupport::Concern
included do
# Job arguments
attr_accessor :arguments
attr_writer :serialized_arguments
# Timestamp when the job should be performed
attr_accessor :scheduled_at
# Job Identifier
attr_accessor :job_id
# Queue in which the job will reside.
2014-08-25 10:34:50 -04:00
attr_writer :queue_name
2015-03-18 05:48:26 -04:00
# Priority that the job will have (lower is more priority).
attr_writer :priority
# ID optionally provided by adapter
attr_accessor :provider_job_id
# Number of times this job has been executed (which increments on every retry, like after an exception).
attr_accessor :executions
# I18n.locale to be used during the job.
attr_accessor :locale
# Timezone to be used during the job.
attr_accessor :timezone
2014-08-25 10:34:50 -04:00
end
# These methods will be included into any Active Job object, adding
# helpers for de/serialization and creation of job instances.
2014-08-25 10:34:50 -04:00
module ClassMethods
# Creates a new job instance from a hash created with +serialize+
def deserialize(job_data)
job = job_data["job_class"].constantize.new
job.deserialize(job_data)
2014-08-25 10:34:50 -04:00
job
end
# Creates a job preconfigured with the given options. You can call
# perform_later with the job arguments to enqueue the job with the
# preconfigured options
#
# ==== Options
# * <tt>:wait</tt> - Enqueues the job with the specified delay
# * <tt>:wait_until</tt> - Enqueues the job at the time specified
# * <tt>:queue</tt> - Enqueues the job on the specified queue
2015-03-18 05:48:26 -04:00
# * <tt>:priority</tt> - Enqueues the job with the specified priority
2014-08-25 10:34:50 -04:00
#
# ==== Examples
#
# VideoJob.set(queue: :some_queue).perform_later(Video.last)
# VideoJob.set(wait: 5.minutes).perform_later(Video.last)
2014-09-27 02:07:24 -04:00
# VideoJob.set(wait_until: Time.now.tomorrow).perform_later(Video.last)
2014-08-25 10:34:50 -04:00
# VideoJob.set(queue: :some_queue, wait: 5.minutes).perform_later(Video.last)
2014-09-27 02:07:24 -04:00
# VideoJob.set(queue: :some_queue, wait_until: Time.now.tomorrow).perform_later(Video.last)
2015-03-18 05:48:26 -04:00
# VideoJob.set(queue: :some_queue, wait: 5.minutes, priority: 10).perform_later(Video.last)
def set(options = {})
2014-08-25 10:34:50 -04:00
ConfiguredJob.new(self, options)
end
end
# Creates a new job instance. Takes the arguments that will be
# passed to the perform method.
2014-08-25 10:34:50 -04:00
def initialize(*arguments)
@arguments = arguments
@job_id = SecureRandom.uuid
@queue_name = self.class.queue_name
2015-03-18 05:48:26 -04:00
@priority = self.class.priority
@executions = 0
2014-08-25 10:34:50 -04:00
end
# Returns a hash with the job data that can safely be passed to the
# queueing adapter.
def serialize
{
"job_class" => self.class.name,
"job_id" => job_id,
"provider_job_id" => provider_job_id,
"queue_name" => queue_name,
"priority" => priority,
"arguments" => serialize_arguments(arguments),
"executions" => executions,
"locale" => I18n.locale.to_s,
"timezone" => Time.zone.try(:name)
2014-08-25 10:34:50 -04:00
}
end
# Attaches the stored job data to the current instance. Receives a hash
# returned from +serialize+
#
# ==== Examples
#
# class DeliverWebhookJob < ActiveJob::Base
# attr_writer :attempt_number
#
# def attempt_number
# @attempt_number ||= 0
# end
#
# def serialize
# super.merge('attempt_number' => attempt_number + 1)
# end
#
# def deserialize(job_data)
# super
# self.attempt_number = job_data['attempt_number']
# end
#
# rescue_from(Timeout::Error) do |exception|
# raise exception if attempt_number > 5
# retry_job(wait: 10)
# end
# end
def deserialize(job_data)
self.job_id = job_data["job_id"]
self.provider_job_id = job_data["provider_job_id"]
self.queue_name = job_data["queue_name"]
self.priority = job_data["priority"]
self.serialized_arguments = job_data["arguments"]
self.executions = job_data["executions"]
self.locale = job_data["locale"] || I18n.locale.to_s
self.timezone = job_data["timezone"] || Time.zone.try(:name)
end
2014-08-25 10:34:50 -04:00
private
def deserialize_arguments_if_needed
if defined?(@serialized_arguments) && @serialized_arguments.present?
@arguments = deserialize_arguments(@serialized_arguments)
@serialized_arguments = nil
end
end
def serialize_arguments(serialized_args)
Arguments.serialize(serialized_args)
end
def deserialize_arguments(serialized_args)
Arguments.deserialize(serialized_args)
end
end
end