2014-08-26 16:08:49 -04:00
|
|
|
module ActiveJob
|
|
|
|
module QueueAdapters
|
2014-09-21 16:20:23 -04:00
|
|
|
# == Test adapter for Active Job
|
|
|
|
#
|
|
|
|
# The test adapter should be used only in testing. Along with
|
|
|
|
# <tt>ActiveJob::TestCase</tt> and <tt>ActiveJob::TestHelper</tt>
|
|
|
|
# it makes a great tool to test your Rails application.
|
|
|
|
#
|
|
|
|
# To use the test adapter set queue_adapter config to +:test+.
|
|
|
|
#
|
|
|
|
# Rails.application.config.active_job.queue_adapter = :test
|
2014-08-26 16:08:49 -04:00
|
|
|
class TestAdapter
|
2014-09-24 00:11:54 -04:00
|
|
|
class << self
|
|
|
|
attr_accessor(:perform_enqueued_jobs, :perform_enqueued_at_jobs, :filter)
|
|
|
|
attr_writer(:enqueued_jobs, :performed_jobs)
|
2014-08-26 16:08:49 -04:00
|
|
|
|
2014-09-24 00:11:54 -04:00
|
|
|
# Provides a store of all the enqueued jobs with the TestAdapter so you can check them.
|
|
|
|
def enqueued_jobs
|
|
|
|
@enqueued_jobs ||= []
|
|
|
|
end
|
2014-12-30 10:53:42 -05:00
|
|
|
|
2014-09-24 00:11:54 -04:00
|
|
|
# Provides a store of all the performed jobs with the TestAdapter so you can check them.
|
|
|
|
def performed_jobs
|
|
|
|
@performed_jobs ||= []
|
|
|
|
end
|
2014-08-26 16:08:49 -04:00
|
|
|
|
2014-09-24 00:11:54 -04:00
|
|
|
def enqueue(job) #:nodoc:
|
|
|
|
return if filtered?(job)
|
2014-08-26 16:08:49 -04:00
|
|
|
|
2014-09-24 00:11:54 -04:00
|
|
|
job_data = job_to_hash(job)
|
|
|
|
enqueue_or_perform(perform_enqueued_jobs, job, job_data)
|
|
|
|
end
|
2015-02-06 13:05:28 -05:00
|
|
|
|
2014-09-24 00:11:54 -04:00
|
|
|
def enqueue_at(job, timestamp) #:nodoc:
|
|
|
|
return if filtered?(job)
|
2014-08-26 16:08:49 -04:00
|
|
|
|
2014-09-24 00:11:54 -04:00
|
|
|
job_data = job_to_hash(job, at: timestamp)
|
|
|
|
enqueue_or_perform(perform_enqueued_at_jobs, job, job_data)
|
|
|
|
end
|
2015-02-06 13:05:28 -05:00
|
|
|
|
2014-09-24 00:11:54 -04:00
|
|
|
private
|
2015-02-06 13:05:28 -05:00
|
|
|
|
2014-09-24 00:11:54 -04:00
|
|
|
def job_to_hash(job, extras = {})
|
2015-02-24 05:23:05 -05:00
|
|
|
{ job: job.class, args: job.serialize.fetch('arguments'), queue: job.queue_name }.merge!(extras)
|
2014-09-24 00:11:54 -04:00
|
|
|
end
|
2015-02-06 13:05:28 -05:00
|
|
|
|
|
|
|
def enqueue_or_perform(perform, job, job_data)
|
|
|
|
if perform
|
|
|
|
performed_jobs << job_data
|
|
|
|
Base.execute job.serialize
|
|
|
|
else
|
|
|
|
enqueued_jobs << job_data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def filtered?(job)
|
|
|
|
filter && !Array(filter).include?(job.class)
|
|
|
|
end
|
2014-09-24 00:11:54 -04:00
|
|
|
end
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|