Skip to content

Commit

Permalink
allow_overlap rework (code and doc)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmettraux committed Apr 26, 2011
1 parent 352156e commit d5b05bc
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

== rufus-scheduler - 2.0.7 released 2010/11/09

- :allow_overlap => false, thanks Adam Davies
- cron and timezones, thanks Tanzeeb Khalili
- Scheduler#trigger_threads, thanks Tim Uckun

Expand Down
2 changes: 1 addition & 1 deletion CREDITS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
- concept47 (https://github.com/concept47) every and :discard_past
- Chris Kampemeier (http://github.com/chrisk) rspec 2.0 refinements
- Tanzeeb Khalili (http://github.com/tanzeeb) cron and timezones
- Adam Davies (http://github.com/adz), @allow_overlap = false
- Adam Davies (http://github.com/adz), :allow_overlap => false
- Klaas Jan Wierenga, at/every/in stress tests (1.0 and 2.0)
- TobyH (http://github.com/tobyh), faster and cleaner CronLine#next_time

Expand Down
23 changes: 22 additions & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ http://rufus.rubyforge.org/rufus-scheduler/classes/Rufus/Scheduler/Job.html
p Rufus.to_time_string 7 * 24 * 3600 # => "1w"


== :blocking
== :blocking => true

Jobs will, by default, trigger in their own thread. This is usually desirable since one expects the scheduler to continue scheduling even if a job is currently running.

Expand All @@ -175,6 +175,27 @@ Jobs scheduled with the :blocking parameter will run in the thread of the schedu
Hence, our espresso will come in 22 minutes instead of 21.


== :allow_overlap => false

By default, every and cron jobs will "overlap" :

scheduler.every '3s' do
4.times do
puts "hello!"
sleep 1
end
end

This every job, will have overlaps. To prevent that :

scheduler.every '3s', :allow_overlap => false do
4.times do
puts "hello!"
sleep 1
end
end


== 'every' jobs and :first_at / :first_in

This job will execute every 3 days, but first time will be in 5 days from now :
Expand Down
19 changes: 11 additions & 8 deletions lib/rufus/sc/jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ class Job
#
attr_reader :job_id

attr_accessor :running

# Instantiating the job.
#
def initialize(scheduler, t, params, &block)
Expand All @@ -79,10 +77,6 @@ def initialize(scheduler, t, params, &block)
@block = block || params[:schedulable]

@running = false
@allow_overlapping = true
if !params[:allow_overlapping].nil?
@allow_overlapping = params[:allow_overlapping]
end

raise ArgumentError.new(
'no block or :schedulable passed, nothing to schedule'
Expand All @@ -95,6 +89,15 @@ def initialize(scheduler, t, params, &block)
determine_at
end

# Returns true if this job is currently running (in the middle of #trigger)
#
def running

@running
end

alias running? running

# Returns the list of tags attached to the job.
#
def tags
Expand Down Expand Up @@ -126,9 +129,10 @@ def trigger(t=Time.now)
job_thread = nil
to_job = nil

return if @running && !@allow_overlapping
return if @running and (params[:allow_overlapping] == false)

@running = true

@scheduler.send(:trigger_job, @params[:blocking]) do
#
# Note that #trigger_job is protected, hence the #send
Expand Down Expand Up @@ -169,7 +173,6 @@ def trigger(t=Time.now)
end
end
end

end

# Simply encapsulating the block#call/trigger operation, for easy
Expand Down
4 changes: 2 additions & 2 deletions spec/every_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@
end


it 'does not allow a job to overlap execution if set !allow_overlapping' do
it "doesn't allow overlapped execution if :allow_overlapping => false" do

stack = []

Expand All @@ -238,7 +238,7 @@
stack.size.should == 2
end

it 'allows a job to overlap execution (backward compatibility?)' do
it 'allows overlapped execution by default' do

stack = []

Expand Down

0 comments on commit d5b05bc

Please sign in to comment.