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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5132b8b0de9b2367ec64975fd45bb3f28a6e0e9d
4
- data.tar.gz: 9ed3b9b10a9bd8717421eb2054b95a93aa203e9f
3
+ metadata.gz: d2bba860770d4be5139149e0069cbb365c89759a
4
+ data.tar.gz: f5c3be4e786cef71c20449657dd3b8033ff0d44f
5
5
  SHA512:
6
- metadata.gz: 2da9918d434cc04329d3dfb0089373eac2762737f39b7b5a488dec2db45432b7412bcdf5188b7ec83d63e9b9c26cd3a02a60236719208f9c778be7d4b53825bf
7
- data.tar.gz: 732622b3d8e2777ddb3f4c3126f678230579759ebe8ed35fe6d5d5331d26b9a2d962fd59cd66683e078a8e87aee18711ee2fb34f8c7fed012911a0abd0cd8605
6
+ metadata.gz: db5990d551d0e973b8646b898d2c4a924d3b6724a0ded9eb7ab40b6ccb6d28baa8e994ce10926e73564ceba40b0c9ea6d342b877f36e1cb951852b9a2d88e0a0
7
+ data.tar.gz: 50024aa15719cc7e313186bbbc734eadbfee1b90297febc889ed68f6c583cae5af18059a05515f1b68756fd7ab1c0d9edfdf459ae0620fb44f86aac32055fad4
@@ -2,6 +2,10 @@
2
2
 
3
3
  Nothing
4
4
 
5
+ ## 0.3.0
6
+
7
+ * Add support for Active Job `TestAdapter` #19 [Hermann Mayer]
8
+
5
9
  ## 0.2.0
6
10
 
7
11
  * Add support for `resque`
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 `resque`.
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)
@@ -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?
@@ -6,6 +6,7 @@ module ActiveJob
6
6
  autoload :SidekiqAdapter
7
7
  autoload :DelayedJobAdapter
8
8
  autoload :ResqueAdapter
9
+ autoload :TestAdapter
9
10
  end
10
11
  end
11
12
  end
@@ -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
@@ -0,0 +1,14 @@
1
+ module ActiveJob
2
+ module QueueAdapters
3
+ class TestAdapter
4
+ def job_to_hash(job, extras = {})
5
+ {
6
+ id: job.job_id,
7
+ job: job.class,
8
+ args: job.serialize.fetch('arguments'),
9
+ queue: job.queue_name
10
+ }.merge!(extras)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,5 +1,5 @@
1
1
  module ActiveJob
2
2
  module Cancel
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  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.2.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-08 00:00:00.000000000 Z
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