Skip to content

Commit

Permalink
Merge 442b38b into f505406
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack12816 committed Nov 11, 2017
2 parents f505406 + 442b38b commit 5223ae7
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
@@ -1,6 +1,6 @@
# ActiveJob::Cancel

`activejob-cancel` provides cancel method to Active Job. Currently it supports only `Sidekiq`, `Delayed Job` and `resque`.
`activejob-cancel` provides cancel method to Active Job. Currently it supports only `Sidekiq`, `Delayed Job`, `resque` and the ActiveJob `TestAdapter`.

[![Build Status](https://travis-ci.org/y-yagi/activejob-cancel.svg?branch=master)](https://travis-ci.org/y-yagi/activejob-cancel)
[![Gem Version](https://badge.fury.io/rb/activejob-cancel.svg)](http://badge.fury.io/rb/activejob-cancel)
Expand Down
3 changes: 2 additions & 1 deletion lib/active_job/cancel.rb
@@ -1,13 +1,14 @@
require 'active_support'
require 'active_job'
require 'active_job/cancel/queue_adapters'
require 'active_job/cancel/queue_adapters/test_adapter'
require 'active_job/cancel/version'

module ActiveJob
module Cancel
extend ActiveSupport::Concern

SUPPORTED_ADAPTERS = %w(Sidekiq DelayedJob Resque).freeze
SUPPORTED_ADAPTERS = %w(Sidekiq DelayedJob Resque Test).freeze

def cancel
if self.class.can_cancel?
Expand Down
1 change: 1 addition & 0 deletions lib/active_job/cancel/queue_adapters.rb
Expand Up @@ -6,6 +6,7 @@ module QueueAdapters
autoload :SidekiqAdapter
autoload :DelayedJobAdapter
autoload :ResqueAdapter
autoload :TestAdapter
end
end
end
47 changes: 47 additions & 0 deletions lib/active_job/cancel/queue_adapters/test_adapter.rb
@@ -0,0 +1,47 @@
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 job_to_hash(job, extras = {})
{
id: job.job_id,
job: job.class,
args: job.serialize.fetch('arguments'),
queue: job.queue_name
}.merge!(extras)
end
end
end

module Cancel
module QueueAdapters
class TestAdapter
def cancel(job_id, queue_name)
original_count = adapter.enqueued_jobs.count
adapter.enqueued_jobs = filter_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 filter_enqueued_jobs(job_id)
adapter.enqueued_jobs.reject { |job| job[:id] == job_id }
end
end
end
end
end
113 changes: 113 additions & 0 deletions test/queue_adapters/test_adapter_test.rb
@@ -0,0 +1,113 @@
require 'test_helper'

module ActiveJob::Cancel::QueueAdapters
class ActiveJob::Cancel::QueueAdapters::TestAdapterTest< Minitest::Test
def setup
ActiveJob::Base.queue_adapter = :test
@hello_job_queue_name = HelloJob.queue_name.call
@fail_job_queue_name = FailJob.queue_name.call
end

def teardown
end

def test_cancel_queued_job_with_instance_method
assert_equal 0, queue.size

job = HelloJob.perform_later
assert_equal 1, queue.size

job.cancel
assert_equal 0, queue.size
ensure
queue.clear
end

def test_cancel_queued_job_with_class_method
assert_equal 0, queue.size

job = HelloJob.perform_later
assert_equal 1, queue.size

HelloJob.cancel(job.job_id)
assert_equal 0, queue.size
ensure
queue.clear
end

def test_cancel_scheduled_job_with_instance_method
assert_equal 0, queue.size

job = HelloJob.set(wait: 30.seconds).perform_later
assert_equal 1, queue.size

job.cancel
assert_equal 0, queue.size
ensure
queue.clear
end

def test_cancel_scheduled_job_with_class_method
assert_equal 0, queue.size

job = HelloJob.set(wait: 30.seconds).perform_later
assert_equal 1, queue.size

HelloJob.cancel(job.job_id)
assert_equal 0, queue.size
ensure
queue.map(&:delete)
end

def test_cancel_with_invalid_id
job = HelloJob.perform_later

HelloJob.cancel(job.job_id.to_i + 1)
assert_equal 1, queue.size
ensure
queue.clear
end

def test_cancel_by_with_invalid_parameters
assert_raises(ArgumentError) { HelloJob.cancel_by(id: 1) }
end

def test_cancel_queued_job_with_provider_job_id
assert_equal 0, queue.size

HelloJob.perform_later
assert_equal 1, queue.size

HelloJob.cancel_by(provider_job_id: queue.map.first[:id])
assert_equal 0, queue.size
ensure
queue.clear
end

def test_cancel_scheduled_job_with_provider_job_id
assert_equal 0, queue.size

HelloJob.set(wait: 30.seconds).perform_later
assert_equal 1, queue.size

HelloJob.cancel_by(provider_job_id: queue.map.first[:id])
assert_equal 0, queue.size
ensure
queue.map(&:delete)
end

def test_cancel_by_with_invalid_job_id
HelloJob.perform_later

refute HelloJob.cancel_by(provider_job_id: queue.map.first[:id].to_i + 1)
assert_equal 1, queue.size
ensure
queue.clear
end

private
def queue
ActiveJob::Base.queue_adapter.enqueued_jobs
end
end
end

0 comments on commit 5223ae7

Please sign in to comment.