Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ module QueueAdapters | |
|
||
autoload :SidekiqAdapter | ||
autoload :DelayedJobAdapter | ||
autoload :ResqueAdapter | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require 'resque' | ||
|
||
module ActiveJob | ||
module Cancel | ||
module QueueAdapters | ||
class ResqueAdapter | ||
def cancel(job_id, queue_name) | ||
job = find_job(job_id, queue_name) | ||
|
||
if job | ||
Resque.redis.lrem(redis_key_for_queue(queue_name), 0, Resque.encode(job)) | ||
return true | ||
end | ||
|
||
false | ||
end | ||
|
||
private | ||
def find_job(job_id, queue_name) | ||
jobs = Resque.list_range(redis_key_for_queue(queue_name), 0, Resque.size(queue_name)) | ||
jobs = [jobs] if jobs.is_a?(Hash) | ||
|
||
jobs.find do |job| | ||
job["args"][0]["job_id"] == job_id | ||
end | ||
end | ||
|
||
def redis_key_for_queue(queue) | ||
"queue:#{queue}" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require 'test_helper' | ||
require 'resque' | ||
|
||
module ActiveJob::Cancel::QueueAdapters | ||
class ActiveJob::Cancel::QueueAdapters::ResqueAdapterTest< Minitest::Test | ||
def setup | ||
ActiveJob::Base.queue_adapter = :resque | ||
end | ||
|
||
def test_cancel_queued_job_with_instance_method | ||
assert_equal 0, Resque.size(HelloJob.queue_name) | ||
|
||
job = HelloJob.perform_later | ||
assert_equal 1, Resque.size(HelloJob.queue_name) | ||
|
||
job.cancel | ||
assert_equal 0, Resque.size(HelloJob.queue_name) | ||
ensure | ||
Resque.remove_queue(HelloJob.queue_name) | ||
end | ||
|
||
def test_cancel_with_class_method | ||
assert_equal 0, Resque.size(HelloJob.queue_name) | ||
|
||
job = HelloJob.perform_later | ||
assert_equal 1, Resque.size(HelloJob.queue_name) | ||
|
||
HelloJob.cancel(job.job_id) | ||
assert_equal 0, Resque.size(HelloJob.queue_name) | ||
ensure | ||
Resque.remove_queue(HelloJob.queue_name) | ||
end | ||
|
||
def test_cancel_with_invalid_id | ||
job = HelloJob.perform_later | ||
refute HelloJob.cancel(job.job_id.to_i + 1) | ||
assert_equal 1, Resque.size(HelloJob.queue_name) | ||
ensure | ||
Resque.remove_queue(HelloJob.queue_name) | ||
end | ||
end | ||
end |