Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5db4e7f
commit 1e237b4
Showing
33 changed files
with
441 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
module ActiveJob | ||
class ConfiguredJob #:nodoc: | ||
def initialize(job_class, options={}) | ||
@options = options | ||
@options[:in] = @options.delete(:wait) if @options[:wait] | ||
@options[:at] = @options.delete(:wait_until) if @options[:wait_until] | ||
@job_class = job_class | ||
end | ||
|
||
def perform_now(*args) | ||
@job_class.new(*args).perform_now | ||
end | ||
|
||
def perform_later(*args) | ||
@job_class.new(*args).enqueue @options | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
module ActiveJob | ||
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 on which the job should be run on. | ||
attr_writer :queue_name | ||
end | ||
|
||
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.job_id = job_data['job_id'] | ||
job.queue_name = job_data['queue_name'] | ||
job.serialized_arguments = job_data['arguments'] | ||
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 | ||
# | ||
# ==== Examples | ||
# | ||
# VideoJob.set(queue: :some_queue).perform_later(Video.last) | ||
# VideoJob.set(wait: 5.minutes).perform_later(Video.last) | ||
# VideoJob.set(wait_until: Time.tomorroe).perform_later(Video.last) | ||
# VideoJob.set(queue: :some_queue, wait: 5.minutes).perform_later(Video.last) | ||
# VideoJob.set(queue: :some_queue, wait_until: Time.tomorroe).perform_later(Video.last) | ||
def set(options={}) | ||
ConfiguredJob.new(self, options) | ||
end | ||
end | ||
|
||
# Creates a new job instance. Takes as arguments the arguments that | ||
# will be passed to the perform method. | ||
def initialize(*arguments) | ||
@arguments = arguments | ||
@job_id = SecureRandom.uuid | ||
@queue_name = self.class.queue_name | ||
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, | ||
'queue_name' => queue_name, | ||
'arguments' => serialize_arguments(arguments) | ||
} | ||
end | ||
|
||
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 | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.