Class: Rack::Timeout::Scheduler
- Inherits:
-
Object
- Object
- Rack::Timeout::Scheduler
- Includes:
- AssertTypes
- Defined in:
- lib/rack/timeout/support/scheduler.rb
Overview
Runs code at a later time
Basic usage:
Scheduler.run_in(5) { do_stuff } # <- calls do_stuff 5 seconds from now
Scheduled events run in sequence in a separate thread, the main thread continues on. That means you may need to #join the scheduler if the main thread is only waiting on scheduled events to run.
Scheduler.join
Basic usage is through a singleton instance, its methods are available as class methods, as shown above. One could also instantiate separate instances which would get you separate run threads, but generally there’s no point in it.
Defined Under Namespace
Classes: RepeatEvent, RunEvent, Timeout
Constant Summary collapse
- MAX_IDLE_SECS =
how long the runner thread is allowed to live doing nothing
30
Class Method Summary collapse
-
.singleton ⇒ Object
accessor to the singleton instance.
Instance Method Summary collapse
-
#delay(event, secs) ⇒ Object
reschedules an event by the given number of seconds.
-
#initialize ⇒ Scheduler
constructor
A new instance of Scheduler.
-
#join ⇒ Object
waits on the runner thread to finish.
-
#reschedule(event, time) ⇒ Object
reschedules an event to run at a different time.
-
#run_at(time, &block) ⇒ Object
schedules a block to run at a given time; returns the created event object.
-
#run_every(seconds, &block) ⇒ Object
schedules a block to run every x seconds; returns the created event object.
-
#run_in(secs, &block) ⇒ Object
schedules a block to run in the given number of seconds; returns the created event object.
-
#schedule(event) ⇒ Object
adds a RunEvent struct to the run schedule.
Methods included from AssertTypes
Constructor Details
#initialize ⇒ Scheduler
Returns a new instance of Scheduler.
58 59 60 61 62 |
# File 'lib/rack/timeout/support/scheduler.rb', line 58 def initialize @events = [] # array of `RunEvent`s @mx_events = Mutex.new # mutex to change said array @mx_runner = Mutex.new # mutex for creating a runner thread end |
Class Method Details
.singleton ⇒ Object
accessor to the singleton instance
156 157 158 |
# File 'lib/rack/timeout/support/scheduler.rb', line 156 def self.singleton @singleton ||= new end |
Instance Method Details
#delay(event, secs) ⇒ Object
reschedules an event by the given number of seconds. can be negative to run sooner.
133 134 135 |
# File 'lib/rack/timeout/support/scheduler.rb', line 133 def delay(event, secs) reschedule(event, event.time + secs) end |
#join ⇒ Object
waits on the runner thread to finish
108 109 110 111 |
# File 'lib/rack/timeout/support/scheduler.rb', line 108 def join @joined = true runner.join end |
#reschedule(event, time) ⇒ Object
reschedules an event to run at a different time. returns nil and does nothing if the event is not already in the queue (might’ve run already), otherwise updates the event time in-place; returns the updated event
122 123 124 125 126 127 128 129 130 |
# File 'lib/rack/timeout/support/scheduler.rb', line 122 def reschedule(event, time) assert_types! event => RunEvent, time => Time @mx_events.synchronize { return unless @events.include? event event.time = time runner.run return event } end |
#run_at(time, &block) ⇒ Object
schedules a block to run at a given time; returns the created event object
138 139 140 |
# File 'lib/rack/timeout/support/scheduler.rb', line 138 def run_at(time, &block) schedule RunEvent.new(time, block) end |
#run_every(seconds, &block) ⇒ Object
schedules a block to run every x seconds; returns the created event object
148 149 150 |
# File 'lib/rack/timeout/support/scheduler.rb', line 148 def run_every(seconds, &block) schedule RepeatEvent.new(Time.now, block, seconds) end |
#run_in(secs, &block) ⇒ Object
schedules a block to run in the given number of seconds; returns the created event object
143 144 145 |
# File 'lib/rack/timeout/support/scheduler.rb', line 143 def run_in(secs, &block) run_at(Time.now + secs, &block) end |
#schedule(event) ⇒ Object
adds a RunEvent struct to the run schedule
114 115 116 117 118 119 |
# File 'lib/rack/timeout/support/scheduler.rb', line 114 def schedule(event) assert_types! event => RunEvent @mx_events.synchronize { @events << event } runner.run # wakes up the runner thread so it can recalculate sleep length taking this new event into consideration return event end |