-
Notifications
You must be signed in to change notification settings - Fork 9
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
4 changed files
with
126 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Ordered Job | ||
# | ||
# Ensures that only one job for a given queue | ||
# will be running on any worker at a given time | ||
# | ||
# Examples: | ||
# | ||
# class TestJob | ||
# include Resque::Integration | ||
# | ||
# unique { |company_id, param1| [company_id] } | ||
# ordered max_iterations: 10 | ||
# | ||
# def self.execute(company_id, param1) | ||
# heavy_lifting_work | ||
# end | ||
# end | ||
# | ||
module Resque | ||
module Integration | ||
module Ordered | ||
ARGS_EXPIRATION = 1.week | ||
|
||
def self.extended(base) | ||
base.extend ClassMethods | ||
|
||
base.singleton_class.class_eval do | ||
attr_accessor :max_iterations | ||
|
||
alias_method_chain :enqueue, :ordered | ||
end | ||
end | ||
|
||
module ClassMethods | ||
def enqueue_with_ordered(*args) | ||
meta = enqueue_without_ordered(*args) | ||
|
||
encoded_args = Resque.encode(args) | ||
args_key = ordered_queue_key(meta.meta_id) | ||
Resque.redis.rpush(args_key, encoded_args) | ||
Resque.redis.expire(args_key, ARGS_EXPIRATION) | ||
|
||
meta | ||
end | ||
|
||
def perform(meta_id, *) | ||
args_key = ordered_queue_key(meta_id) | ||
i = 1 | ||
while job_args = Resque.redis.lpop(args_key) | ||
execute(*Resque.decode(job_args)) | ||
|
||
i += 1 | ||
return continue if max_iterations && i > max_iterations | ||
end | ||
end | ||
|
||
def ordered_queue_size(meta_id) | ||
Resque.redis.llen(ordered_queue_key(meta_id)).to_i | ||
end | ||
|
||
def ordered_queue_key(meta_id) | ||
"ordered:#{meta_id}" | ||
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,41 @@ | ||
require "spec_helper" | ||
|
||
describe Resque::Integration::Ordered do | ||
class TestJob | ||
include Resque::Integration | ||
|
||
unique { |company_id, param1| [company_id] } | ||
ordered max_iterations: 2 | ||
end | ||
|
||
it "push args to separate queue" do | ||
meta = TestJob.enqueue(1, 10) | ||
TestJob.enqueue(1, 20) | ||
|
||
args_key = TestJob.ordered_queue_key(meta.meta_id) | ||
expect(TestJob).to be_enqueued(1) | ||
expect(TestJob.ordered_queue_size(meta.meta_id)).to eq 2 | ||
end | ||
|
||
it "execute jobs by each args" do | ||
meta = TestJob.enqueue(1, 10) | ||
TestJob.enqueue(1, 20) | ||
|
||
expect(TestJob).to receive(:execute).with(1, 10).ordered | ||
expect(TestJob).to receive(:execute).with(1, 20).ordered | ||
|
||
TestJob.perform(meta.meta_id) | ||
end | ||
|
||
it "reenqueue job after max iterations reached" do | ||
meta = TestJob.enqueue(1, 10) | ||
TestJob.enqueue(1, 20) | ||
TestJob.enqueue(1, 30) | ||
|
||
expect(TestJob).to receive(:execute).with(1, 10).ordered | ||
expect(TestJob).to receive(:execute).with(1, 20).ordered | ||
expect(TestJob).to_not receive(:execute).with(1, 30).ordered | ||
|
||
TestJob.perform(meta.meta_id) | ||
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