2014-05-22 13:35:45 -04:00
|
|
|
require 'active_job/arguments'
|
2014-05-19 06:06:09 -04:00
|
|
|
|
|
|
|
module ActiveJob
|
|
|
|
module Enqueuing
|
2014-05-22 13:33:23 -04:00
|
|
|
extend ActiveSupport::Concern
|
2014-05-30 19:19:30 -04:00
|
|
|
|
2014-05-22 13:33:23 -04:00
|
|
|
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
|
|
|
|
# are not supported.
|
|
|
|
#
|
2014-05-30 19:19:30 -04:00
|
|
|
# Returns an instance of the job class queued with args available in
|
2014-05-22 13:33:23 -04:00
|
|
|
# Job#arguments.
|
|
|
|
def enqueue(*args)
|
|
|
|
new(args).tap do |job|
|
|
|
|
job.run_callbacks :enqueue do
|
2014-05-30 19:19:30 -04:00
|
|
|
queue_adapter.enqueue self, job.job_id, *Arguments.serialize(args)
|
2014-05-22 13:33:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-05-19 18:27:28 -04:00
|
|
|
|
2014-05-22 13:33:23 -04:00
|
|
|
# Enqueue a job to be performed at +interval+ from now.
|
|
|
|
#
|
|
|
|
# enqueue_in(1.week, "mike")
|
|
|
|
#
|
2014-05-30 19:19:30 -04:00
|
|
|
# Returns an instance of the job class queued with args available in
|
2014-05-22 13:33:23 -04:00
|
|
|
# Job#arguments and the timestamp in Job#enqueue_at.
|
|
|
|
def enqueue_in(interval, *args)
|
|
|
|
enqueue_at interval.seconds.from_now, *args
|
|
|
|
end
|
|
|
|
|
|
|
|
# Enqueue a job to be performed at an explicit point in time.
|
|
|
|
#
|
|
|
|
# enqueue_at(Date.tomorrow.midnight, "mike")
|
|
|
|
#
|
2014-05-30 19:19:30 -04:00
|
|
|
# Returns an instance of the job class queued with args available in
|
2014-05-22 13:33:23 -04:00
|
|
|
# Job#arguments and the timestamp in Job#enqueue_at.
|
|
|
|
def enqueue_at(timestamp, *args)
|
|
|
|
new(args).tap do |job|
|
|
|
|
job.enqueued_at = timestamp
|
2014-05-19 18:27:28 -04:00
|
|
|
|
2014-05-22 13:33:23 -04:00
|
|
|
job.run_callbacks :enqueue do
|
2014-05-30 19:19:30 -04:00
|
|
|
queue_adapter.enqueue_at self, timestamp.to_f, job.job_id, *Arguments.serialize(args)
|
2014-05-22 13:33:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-05-19 18:27:28 -04:00
|
|
|
end
|
2014-05-30 19:19:30 -04:00
|
|
|
|
2014-05-22 13:33:23 -04:00
|
|
|
included do
|
|
|
|
attr_accessor :arguments
|
|
|
|
attr_accessor :enqueued_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(arguments = nil)
|
|
|
|
@arguments = arguments
|
|
|
|
end
|
2014-05-30 19:19:30 -04:00
|
|
|
|
2014-05-22 14:35:02 -04:00
|
|
|
def retry_now
|
|
|
|
self.class.enqueue *arguments
|
|
|
|
end
|
2014-05-30 19:19:30 -04:00
|
|
|
|
2014-05-22 14:35:02 -04:00
|
|
|
def retry_in(interval)
|
|
|
|
self.class.enqueue_in interval, *arguments
|
|
|
|
end
|
|
|
|
|
|
|
|
def retry_at(timestamp)
|
|
|
|
self.class.enqueue_at timestamp, *arguments
|
|
|
|
end
|
2014-05-19 06:06:09 -04:00
|
|
|
end
|
2014-05-19 16:13:40 -04:00
|
|
|
end
|