Ticket #27 (new enhancement)

Opened 1 year ago

Last modified 3 months ago

add cron style syntax on top of periodic timers in EM

Reported by: tmm1 Assigned to:
Priority: minor Milestone: EventMachine "Framework"
Keywords: Cc: raggi

Description

Here's a simple one I'm using to run something daily at a given time (i.e. EM.daily('9:00am'){ puts 'Wake up!!' })

require 'ramaze/snippets' # for 1.day
require 'time' # for Time.parse
require 'eventmachine'

module EventMachine
  def self.daily at, &blk
    time = Time.parse(at) - Time.now
    time += 1.day if time < 0

    EM.run do
      EM::add_timer(time) do
        blk.call
        EM::add_periodic_timer(1.day, &blk)
      end
    end
  end
end

Change History

07/20/08 05:15:38 changed by tmm1

02/26/09 16:45:49 changed by tmm1

02/27/09 23:41:21 changed by tmm1

This version schedules the next timer as soon as the new one fires, preventing "drift" over time:

require 'time'
module EventMachine
  def self.daily at, &blk
    time = Time.parse(at) - Time.now
    time += 1.day if time < 0

    EM.run do
      run_me = proc{
        EM.add_timer(1.day, run_me)
        blk.call
      }
      EM.add_timer(time, run_me)
    end
  end
end

02/28/09 01:12:24 changed by yakischloba

here is some helper stuff, though not using cron syntax

http://github.com/yakischloba/em-timers/tree/master