Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resque.set_schedule feature #129

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,47 @@ describe "#recalculate" do
end
```

Resque-scheduler
-----------------------------------------------------

For usage with resque-scheduler gem, you may want to those methods
`Resque.set_schedule(name, config)` and dequeuing `Resque.remove_schedule(name)`

Let's see simple example, when you need to check if it was scheduled or not.
The first example will only match for args option in config.
The second context with cron, adds more precise for matching. If you need not only to verify
whether it was scheduled, but also cron tab for performing action, you should chain `with_cron` method.
```ruby
describe "#schedule" do
before do
ResqueSpec.reset! # or you can use ResqueSpec.reset_by_name!(name), which is more precise
end

let(:config) do
{
args: ["person_recalculation", [1, 2, 3]
cron: "0 * * * *" # hourly
}
end

it "adds an entry to the named schedule queue with args" do
Resque.set_schedule("person_recalculation", config)

expect("person_recalculation").to have_set_schedule([1, 2, 3])
end

context "with_cron" do
it "adds an entry to the named schedule queue with cron tab check" do
Resque.set_schedule("person_recalculation", config)

expect("person_recalculation").to have_set_schedule([1, 2, 3]).with_cron("0 * * * *")
end
end
end

```


Turning off ResqueSpec and calling directly to Resque
-----------------------------------------------------

Expand Down
12 changes: 12 additions & 0 deletions lib/resque_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ def perform_next(queue_name)
perform(queue_name, queue_by_name(queue_name).shift)
end

def set_schedule_with_queue(name, config)
store(name, config)
end

def remove_schedule_from_queue(name)
reset_by_name!(name)
end

def perform_all(queue_name)
queue = queue_by_name(queue_name)
until queue.empty?
Expand Down Expand Up @@ -65,6 +73,10 @@ def reset!
self.inline = false
end

def reset_by_name!(name)
queues[name.to_s] = []
end

private

def name_from_instance_var(klass)
Expand Down
54 changes: 54 additions & 0 deletions lib/resque_spec/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,60 @@ def schedule_queue_for(actual)
end
end

def scheduled_jobs_for(name)
ResqueSpec.queue_by_name(name)
end
end

RSpec::Matchers.define :have_set_schedule do |*expected_args|
include ScheduleQueueHelper

chain :with_cron do |cron_rule|
@cron = cron_rule
@cron_rule_info = "with cron #@cron"
end

match do |actual|
scheduled_jobs_for(actual).any? do |entry|
args_match = match_args(expected_args, entry[:args])

cron_matches = if @cron
entry[:cron] == @cron
else
true
end

args_match && cron_matches
end
end

match_when_negated do |actual|
return true if scheduled_jobs_for(actual).empty?

scheduled_jobs_for(actual).any? do |entry|
args_match = match_args(expected_args, entry[:args])

cron_matches = if @cron
entry[:cron] == @cron
else
true
end

!(args_match && cron_matches)
end
end

failure_message do |actual|
["expected that schedule queue #{actual} would have [#{expected_args.join(', ')}] scheduled", @cron_rule_info].join(' ')
end

failure_message_when_negated do |actual|
["expected that schedule queue #{actual} would not have [#{expected_args.join(', ')}] scheduled", @cron_rule_info].join(' ')
end

description do
"have set schedule"
end
end

RSpec::Matchers.define :have_scheduled do |*expected_args|
Expand Down
23 changes: 23 additions & 0 deletions lib/resque_spec/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,28 @@ def self.extended(klass)
alias :enqueue_at_with_queue_without_resque_spec :enqueue_at_with_queue
alias :enqueue_in_with_queue_without_resque_spec :enqueue_in_with_queue
alias :remove_delayed_without_resque_spec :remove_delayed
alias :remove_scheduled_without_resque_spec :remove_schedule
alias :set_schedule_without_resque_spec :set_schedule
end
end

klass.extend(ResqueSpec::SchedulerExtMethods)
end
end

module SchedulerExtMethods
def set_schedule(name, config, reload = false)
return set_schedule(name, config, reload) if ResqueSpec.disable_ext && respond_to(:set_schedule)

ResqueSpec.set_schedule(name, config)
end

def remove_schedule(name)
return remove_schedule(name) if ResqueSpec.disable_ext && respond_to(:remove_schedule)

ResqueSpec.remove_schedule(name)
end

def enqueue_at(time, klass, *args)
return enqueue_at_without_resque_spec(time, klass, *args) if ResqueSpec.disable_ext && respond_to?(:enqueue_at_without_resque_spec)

Expand Down Expand Up @@ -49,6 +64,14 @@ def remove_delayed(klass, *args)
end
end

def set_schedule(name, config)
set_schedule_with_queue(name, config)
end

def remove_schedule(name)
remove_schedule_from_queue(name)
end

def enqueue_at(time, klass, *args)
enqueue_at_with_queue(schedule_queue_name(klass), time, klass, *args)
end
Expand Down
52 changes: 52 additions & 0 deletions spec/resque_spec/matchers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -528,4 +528,56 @@
end

end


describe "#have_set_schedule" do
let(:schedule_name) { "scheduler_start_method" }
let(:config) { {class: NoQueueClass, args: ["scheduler_arg"] } }

before do
Resque.set_schedule(schedule_name, config)
end

after :each do
Resque.remove_schedule(schedule_name)
end

it "raises approrpiate exception" do
lambda {
schedule_name.should have_set_schedule(schedule_name + "1", config)
}.should raise_error(RSpec::Expectations::ExpectationNotMetError)
end

it "return true if actual and schedule args are equal" do
schedule_name.should have_set_schedule("scheduler_arg")
end

it "not exist schedule_name" do
"#{schedule_name}+1".should_not have_set_schedule("scheduler_arg")
end

it "not existing args" do
schedule_name.should_not have_set_schedule("scheduler_other_arg")
end

context "#cron" do
context "with rule" do
let(:config) { super().merge(cron: "0 * * * *") }

it "fetches cron config from entry" do
schedule_name.should have_set_schedule("scheduler_arg").with_cron("0 * * * *")
end

it "finds only with correct cron" do
schedule_name.should_not have_set_schedule("scheduler_arg").with_cron("1 * * * *")
end
end

context "without rule" do
it "fetches empty cron config from entry" do
schedule_name.should have_set_schedule("scheduler_arg").with_cron(nil)
end
end
end
end
end
35 changes: 35 additions & 0 deletions spec/resque_spec/scheduler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,41 @@
end
end

describe "#set_schedule_with_queue" do
let(:name) { "scheduler_start_method" }
let(:config) { {class: "NoQueueClass", args: [1]} }

before do
Resque.set_schedule(name, config)
end

it "adds to the scheduled queue hash" do
ResqueSpec.queue_by_name(name).should_not be_empty
end

it "sets the klass on the queue" do
ResqueSpec.queue_by_name(name).first.should include(:class => "NoQueueClass", :args => [1])
end
end

describe "#remove_schedule_from_queue" do
let(:name) { "scheduler_start_method" }
let(:config) { {class: "NoQueueClass", args: [1]} }

before do
Resque.set_schedule(name, config)
Resque.remove_schedule(name)
end

it "removes the scheduled queue hash" do
ResqueSpec.queue_by_name(name).should be_empty
end

it "removes content from the queue" do
ResqueSpec.queue_by_name(name).should_not include(:class => "NoQueueClass")
end
end

describe "#enqueue_in_with_queue" do
before do
Timecop.freeze(Time.now)
Expand Down
39 changes: 39 additions & 0 deletions spec/resque_spec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,37 @@
end
end

describe '#set_schedule_with_queue' do
let(:config) { {class: NameFromClassMethod.to_s, args: [1]} }
let(:name) { "scheduler_start_method" }

it 'stores job in queue' do
ResqueSpec.set_schedule_with_queue(name, config)

ResqueSpec.queue_by_name(name).should include(config)
end
end


describe '#remove_schedule_from_queue' do
let(:config) { {class: NameFromClassMethod.to_s, args: [1]} }
let(:name) { "scheduler_start_method" }

it 'name found' do
ResqueSpec.set_schedule_with_queue(name, config)
ResqueSpec.remove_schedule_from_queue(name)

ResqueSpec.queue_by_name(name).should_not include(config)
end

it 'name not found' do
ResqueSpec.set_schedule_with_queue(name, config)
ResqueSpec.remove_schedule_from_queue(name)

ResqueSpec.queue_by_name(name).should_not include(config)
end
end

describe "#perform_all" do
before do
ResqueSpec.enqueue(:queue_name, NameFromClassMethod, 1)
Expand Down Expand Up @@ -281,4 +312,12 @@
ResqueSpec.inline.should == false
end
end

describe "#reset_by_name!" do
it "clears the queue" do
ResqueSpec.queue_by_name('scheduler_start_method') << 'queued'
ResqueSpec.reset_by_name!('scheduler_start_method')
ResqueSpec.queue_by_name('scheduler_start_method').should be_empty
end
end
end