2017-07-09 13:49:52 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:40:03 -04:00
|
|
|
|
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
|
2018-08-16 11:14:53 -04:00
|
|
|
attr_accessor(:perform_enqueued_jobs, :perform_enqueued_at_jobs, :filter, :reject, :queue)
|
2015-03-11 17:57:13 -04:00
|
|
|
attr_writer(:enqueued_jobs, :performed_jobs)
|
2014-08-26 16:08:49 -04:00
|
|
|
|
2015-03-11 17:57:13 -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
|
|
|
|
2015-03-11 17:57:13 -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
|
|
|
|
2015-03-11 17:57:13 -04:00
|
|
|
def enqueue(job) #:nodoc:
|
2017-03-15 12:46:28 -04:00
|
|
|
return if filtered?(job)
|
|
|
|
|
2015-03-11 17:57:13 -04:00
|
|
|
job_data = job_to_hash(job)
|
2018-08-09 11:03:43 -04:00
|
|
|
perform_or_enqueue(perform_enqueued_jobs, job, job_data)
|
2015-03-11 17:57:13 -04:00
|
|
|
end
|
2015-02-06 13:05:28 -05:00
|
|
|
|
2015-03-11 17:57:13 -04:00
|
|
|
def enqueue_at(job, timestamp) #:nodoc:
|
2017-03-15 12:46:28 -04:00
|
|
|
return if filtered?(job)
|
|
|
|
|
2015-03-11 17:57:13 -04:00
|
|
|
job_data = job_to_hash(job, at: timestamp)
|
2018-08-09 11:03:43 -04:00
|
|
|
perform_or_enqueue(perform_enqueued_at_jobs, job, job_data)
|
2015-03-11 17:57:13 -04:00
|
|
|
end
|
2015-02-06 13:05:28 -05:00
|
|
|
|
2015-03-11 17:57:13 -04:00
|
|
|
private
|
2016-08-06 13:55:02 -04:00
|
|
|
def job_to_hash(job, extras = {})
|
|
|
|
{ job: job.class, args: job.serialize.fetch("arguments"), queue: job.queue_name }.merge!(extras)
|
|
|
|
end
|
2015-02-06 13:05:28 -05:00
|
|
|
|
2018-08-09 11:03:43 -04:00
|
|
|
def perform_or_enqueue(perform, job, job_data)
|
2017-03-15 12:46:28 -04:00
|
|
|
if perform
|
2016-08-06 13:55:02 -04:00
|
|
|
performed_jobs << job_data
|
|
|
|
Base.execute job.serialize
|
2017-03-15 12:46:28 -04:00
|
|
|
else
|
|
|
|
enqueued_jobs << job_data
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
2015-02-06 13:05:28 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
def filtered?(job)
|
2018-08-16 11:14:53 -04:00
|
|
|
filtered_queue?(job) || filtered_job_class?(job)
|
|
|
|
end
|
|
|
|
|
|
|
|
def filtered_queue?(job)
|
|
|
|
if queue
|
|
|
|
job.queue_name != queue.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def filtered_job_class?(job)
|
2017-07-16 04:35:17 -04:00
|
|
|
if filter
|
2018-11-21 16:43:14 -05:00
|
|
|
!filter_as_proc(filter).call(job)
|
2017-07-16 04:35:17 -04:00
|
|
|
elsif reject
|
2018-11-21 16:43:14 -05:00
|
|
|
filter_as_proc(reject).call(job)
|
2017-07-16 04:35:17 -04:00
|
|
|
end
|
2016-08-06 13:55:02 -04:00
|
|
|
end
|
2018-11-21 16:43:14 -05:00
|
|
|
|
|
|
|
def filter_as_proc(filter)
|
|
|
|
return filter if filter.is_a?(Proc)
|
|
|
|
|
|
|
|
->(job) { Array(filter).include?(job.class) }
|
|
|
|
end
|
2014-08-26 16:08:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|