class EM::Cron

Constants

VERSION

Public Class Methods

schedule(cron_string) { |next_time| ... } click to toggle source

@param cron_string [String] @yield [Time] the time the block was scheduled to be called by cron. If

yielding this block returns `:stop` then the schedule stops.

@example

EM.run do
  EM::Cron.schedule("* * * * *") do |time|
    puts "hello world at time: #{time}"
  end
end
# File lib/em/cron.rb, line 16
def self.schedule(cron_string, &blk)
  cron_parser = CronParser.new(cron_string)
  next_time = cron_parser.next(Time.now)
  EM.add_timer(next_time - Time.now) do
    result = yield(next_time)
    schedule(cron_string, &blk) unless result == :stop
  end
end