How To: Prevent a job from overlapping with itself

pushmatrix edited this page Nov 6, 2012 · 6 revisions

By default, a job will overlap with itself if its execution time takes longer than the scheduled interval it runs at. For example:

SCHEDULER.every '2s' do
  puts "Going to bed...zzz...."
  sleep(6)
  puts "Ok I'm done"
end

will print (not necessarily in the same order):

Going to bed...zzz....
Going to bed...zzz....
Going to bed...zzz....
Going to bed...zzz....
Ok I'm done
Going to bed...zzz....
Ok I'm done
......

You can avoid this behaviour by passing the allow_overlapping: false argument to the SCHEDULER.

SCHEDULER.every '2s', allow_overlapping: false do
  puts "Going to bed...zzz...."
  sleep(6)
  puts "Ok I'm done"
end

Problem solved!