Class: Rack::Timeout::Scheduler
- Inherits:
-
Object
- Object
- Rack::Timeout::Scheduler
- Includes:
- MonotonicTime
- 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.
-
#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 MonotonicTime
Constructor Details
#initialize ⇒ Scheduler
Returns a new instance of Scheduler.
53 54 55 56 57 |
# File 'lib/rack/timeout/support/scheduler.rb', line 53 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
140 141 142 |
# File 'lib/rack/timeout/support/scheduler.rb', line 140 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. 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.
117 118 119 120 121 122 123 124 |
# File 'lib/rack/timeout/support/scheduler.rb', line 117 def delay(event, secs) @mx_events.synchronize { return unless @events.include? event event.monotime += secs runner.run return event } end |
#join ⇒ Object
waits on the runner thread to finish
103 104 105 106 |
# File 'lib/rack/timeout/support/scheduler.rb', line 103 def join @joined = true runner.join end |
#run_every(seconds, &block) ⇒ Object
schedules a block to run every x seconds; returns the created event object
132 133 134 |
# File 'lib/rack/timeout/support/scheduler.rb', line 132 def run_every(seconds, &block) schedule RepeatEvent.new(fsecs, block, seconds) end |
#run_in(secs, &block) ⇒ Object
schedules a block to run in the given number of seconds; returns the created event object
127 128 129 |
# File 'lib/rack/timeout/support/scheduler.rb', line 127 def run_in(secs, &block) schedule RunEvent.new(fsecs + secs, block) end |
#schedule(event) ⇒ Object
adds a RunEvent struct to the run schedule
109 110 111 112 113 |
# File 'lib/rack/timeout/support/scheduler.rb', line 109 def schedule(event) @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 |