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.19.0 lib/abstractivator/schedule/schedule_runner.rb
abstractivator-0.18.0 lib/abstractivator/schedule/schedule_runner.rb
abstractivator-0.17.0 lib/abstractivator/schedule/schedule_runner.rb
abstractivator-0.16.0 lib/abstractivator/schedule/schedule_runner.rb
abstractivator-0.15.0 lib/abstractivator/schedule/schedule_runner.rb
abstractivator-0.14.0 lib/abstractivator/schedule/schedule_runner.rb
abstractivator-0.13.0 lib/abstractivator/schedule/schedule_runner.rb
abstractivator-0.12.0 lib/abstractivator/schedule/schedule_runner.rb
abstractivator-0.11.0 lib/abstractivator/schedule/schedule_runner.rb
abstractivator-0.10.0 lib/abstractivator/schedule/schedule_runner.rb
abstractivator-0.8.0 lib/abstractivator/schedule/schedule_runner.rb
abstractivator-0.7.0 lib/abstractivator/schedule/schedule_runner.rb
abstractivator-0.6.0 lib/abstractivator/schedule/schedule_runner.rb
abstractivator-0.5.0 lib/abstractivator/schedule/schedule_runner.rb
abstractivator-0.4.1 lib/abstractivator/schedule/schedule_runner.rb
abstractivator-0.4.0 lib/abstractivator/schedule/schedule_runner.rb
abstractivator-0.3.1 lib/abstractivator/schedule/schedule_runner.rb
abstractivator-0.3.0 lib/abstractivator/schedule/schedule_runner.rb
abstractivator-0.2.0 lib/abstractivator/schedule/schedule_runner.rb
abstractivator-0.1.1 lib/abstractivator/schedule/schedule_runner.rb