/
test_adapter.rb
48 lines (42 loc) · 1.45 KB
/
test_adapter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
require 'active_job'
module ActiveJob
module QueueAdapters
# Unfortunately we need to monkey patch the Rails TestAdapter class,
# because it does not save the job id on the enqueued_jobs array. We rely
# on a persisted id to fulfill the canceling of any given job id.
class TestAdapter
def initialize
if Gem::Requirement.new('~> 5.0').satisfied_by? ActiveJob.version
require 'active_job/cancel/queue_adapters/test_adapter/rails_5'
elsif Gem::Requirement.new('~> 4.2').satisfied_by? ActiveJob.version
require 'active_job/cancel/queue_adapters/test_adapter/rails_4'
end
super
end
end
end
module Cancel
module QueueAdapters
class TestAdapter
def cancel(job_id, queue_name)
original_count = adapter.enqueued_jobs.count
adapter.enqueued_jobs = reject_job_from_enqueued_jobs(job_id)
(original_count == adapter.enqueued_jobs.count) ? false : true
end
def cancel_by(opts, queue_name)
unless opts[:provider_job_id]
raise ArgumentError, 'Please specify ":provider_job_id"'
end
self.cancel(opts[:provider_job_id], queue_name)
end
private
def adapter
ActiveJob::Base.queue_adapter
end
def reject_job_from_enqueued_jobs(job_id)
adapter.enqueued_jobs.reject { |job| job[:id] == job_id }
end
end
end
end
end