Skip to content

Instantly share code, notes, and snippets.

@jmettraux
Created January 23, 2010 06:44
Show Gist options
  • Save jmettraux/284467 to your computer and use it in GitHub Desktop.
Save jmettraux/284467 to your computer and use it in GitHub Desktop.
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