Created
January 23, 2010 06:44
-
-
Save jmettraux/284467 to your computer and use it in GitHub Desktop.
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
require 'rubygems' | |
require 'rufus-scheduler' | |
class CronScheduler | |
def initialize (opts={}) | |
@frequency = opts[:frequency] || 0.3 | |
@jobs = [] | |
end | |
def run | |
loop do | |
t = Time.now | |
@jobs.each do |job| | |
if t >= job[:at] | |
job[:block].call | |
job[:at] = Rufus::CronLine.new(job[:cron]).next_time(t) | |
end | |
end | |
sleep(@frequency) | |
end | |
end | |
def cron (s, &block) | |
@jobs << { | |
:cron => s, | |
:at => Rufus::CronLine.new(s).next_time(Time.now + 1), | |
:block => block | |
} | |
end | |
end | |
s = CronScheduler.new | |
s.cron '* * * * *' do | |
puts "#{Time.now} new sun on new world" | |
end | |
s.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment