Sha256: a4d446b50d56a2cb02f2b946a57f5858f1ff1cd217fec7ec01ae73c9c88b3dc1

Contents?: true

Size: 805 Bytes

Versions: 21

Compression:

Stored size: 805 Bytes

Contents

begin
  require 'eventmachine'
rescue
  raise "ScheduleRunner requires EventMachine but `require 'eventmachine'` failed."
end

# Performs an action periodically, but only at times allowed by a Schedule.
class ScheduleRunner
  def self.run_on_schedule(task, &action)
    ScheduleRunner.new(task, action).start
  end

  def initialize(task, action)
    @task = task
    @action = action
  end

  def start
    run_after(@task.interval_seconds)
  end

  def run_after(seconds_to_wait)
    @task.before_waiting(seconds_to_wait)
    @task.pump.add_timer(seconds_to_wait) do
      schedule = @task.schedule
      if schedule.permits?(Time.now)
        @action.call
        run_after(@task.interval_seconds)
      else
        run_after(schedule.downtime_minutes_from(Time.now) * 60)
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
abstractivator-0.1.0 lib/abstractivator/schedule/schedule_runner.rb