activejob-cancel 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +1 -1
- data/lib/active_job/cancel.rb +2 -1
- data/lib/active_job/cancel/queue_adapters.rb +1 -0
- data/lib/active_job/cancel/queue_adapters/test_adapter.rb +48 -0
- data/lib/active_job/cancel/queue_adapters/test_adapter/rails_4.rb +25 -0
- data/lib/active_job/cancel/queue_adapters/test_adapter/rails_5.rb +14 -0
- data/lib/active_job/cancel/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d2bba860770d4be5139149e0069cbb365c89759a
|
4
|
+
data.tar.gz: f5c3be4e786cef71c20449657dd3b8033ff0d44f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: db5990d551d0e973b8646b898d2c4a924d3b6724a0ded9eb7ab40b6ccb6d28baa8e994ce10926e73564ceba40b0c9ea6d342b877f36e1cb951852b9a2d88e0a0
|
7
|
+
data.tar.gz: 50024aa15719cc7e313186bbbc734eadbfee1b90297febc889ed68f6c583cae5af18059a05515f1b68756fd7ab1c0d9edfdf459ae0620fb44f86aac32055fad4
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# ActiveJob::Cancel
|
2
2
|
|
3
|
-
`activejob-cancel` provides cancel method to Active Job. Currently it supports only `Sidekiq`, `Delayed Job` and `
|
3
|
+
`activejob-cancel` provides cancel method to Active Job. Currently it supports only `Sidekiq`, `Delayed Job`, `resque` and the Active Job `TestAdapter`.
|
4
4
|
|
5
5
|
[![Build Status](https://travis-ci.org/y-yagi/activejob-cancel.svg?branch=master)](https://travis-ci.org/y-yagi/activejob-cancel)
|
6
6
|
[![Gem Version](https://badge.fury.io/rb/activejob-cancel.svg)](http://badge.fury.io/rb/activejob-cancel)
|
data/lib/active_job/cancel.rb
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
require 'active_support'
|
2
2
|
require 'active_job'
|
3
3
|
require 'active_job/cancel/queue_adapters'
|
4
|
+
require 'active_job/cancel/queue_adapters/test_adapter'
|
4
5
|
require 'active_job/cancel/version'
|
5
6
|
|
6
7
|
module ActiveJob
|
7
8
|
module Cancel
|
8
9
|
extend ActiveSupport::Concern
|
9
10
|
|
10
|
-
SUPPORTED_ADAPTERS = %w(Sidekiq DelayedJob Resque).freeze
|
11
|
+
SUPPORTED_ADAPTERS = %w(Sidekiq DelayedJob Resque Test).freeze
|
11
12
|
|
12
13
|
def cancel
|
13
14
|
if self.class.can_cancel?
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'active_job'
|
2
|
+
|
3
|
+
module ActiveJob
|
4
|
+
module QueueAdapters
|
5
|
+
# Unfortunately we need to monkey patch the Rails TestAdapter class,
|
6
|
+
# because it does not save the job id on the enqueued_jobs array. We rely
|
7
|
+
# on a persisted id to fulfill the canceling of any given job id.
|
8
|
+
class TestAdapter
|
9
|
+
def initialize
|
10
|
+
if Gem::Requirement.new('~> 5.0').satisfied_by? ActiveJob.version
|
11
|
+
require 'active_job/cancel/queue_adapters/test_adapter/rails_5'
|
12
|
+
elsif Gem::Requirement.new('~> 4.2').satisfied_by? ActiveJob.version
|
13
|
+
require 'active_job/cancel/queue_adapters/test_adapter/rails_4'
|
14
|
+
end
|
15
|
+
|
16
|
+
super
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module Cancel
|
22
|
+
module QueueAdapters
|
23
|
+
class TestAdapter
|
24
|
+
def cancel(job_id, queue_name)
|
25
|
+
original_count = adapter.enqueued_jobs.count
|
26
|
+
adapter.enqueued_jobs = reject_job_from_enqueued_jobs(job_id)
|
27
|
+
(original_count == adapter.enqueued_jobs.count) ? false : true
|
28
|
+
end
|
29
|
+
|
30
|
+
def cancel_by(opts, queue_name)
|
31
|
+
unless opts[:provider_job_id]
|
32
|
+
raise ArgumentError, 'Please specify ":provider_job_id"'
|
33
|
+
end
|
34
|
+
self.cancel(opts[:provider_job_id], queue_name)
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
def adapter
|
39
|
+
ActiveJob::Base.queue_adapter
|
40
|
+
end
|
41
|
+
|
42
|
+
def reject_job_from_enqueued_jobs(job_id)
|
43
|
+
adapter.enqueued_jobs.reject { |job| job[:id] == job_id }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module ActiveJob
|
2
|
+
module QueueAdapters
|
3
|
+
class TestAdapter
|
4
|
+
alias original_enqueue enqueue
|
5
|
+
alias original_enqueue_at enqueue_at
|
6
|
+
|
7
|
+
def fixup_last_job(job)
|
8
|
+
list = perform_enqueued_jobs ? performed_jobs : enqueued_jobs
|
9
|
+
list.last[:id] = job.job_id
|
10
|
+
end
|
11
|
+
|
12
|
+
def enqueue(job)
|
13
|
+
result = original_enqueue(job)
|
14
|
+
fixup_last_job(job)
|
15
|
+
result
|
16
|
+
end
|
17
|
+
|
18
|
+
def enqueue_at(job, timestamp)
|
19
|
+
result = original_enqueue_at(job, timestamp)
|
20
|
+
fixup_last_job(job)
|
21
|
+
result
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activejob-cancel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yuji Yaginuma
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-12-
|
11
|
+
date: 2017-12-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activejob
|
@@ -219,6 +219,9 @@ files:
|
|
219
219
|
- lib/active_job/cancel/queue_adapters/delayed_job_adapter.rb
|
220
220
|
- lib/active_job/cancel/queue_adapters/resque_adapter.rb
|
221
221
|
- lib/active_job/cancel/queue_adapters/sidekiq_adapter.rb
|
222
|
+
- lib/active_job/cancel/queue_adapters/test_adapter.rb
|
223
|
+
- lib/active_job/cancel/queue_adapters/test_adapter/rails_4.rb
|
224
|
+
- lib/active_job/cancel/queue_adapters/test_adapter/rails_5.rb
|
222
225
|
- lib/active_job/cancel/version.rb
|
223
226
|
- lib/activejob/cancel.rb
|
224
227
|
homepage: https://github.com/y-yagi/activejob-cancel
|