Class: EventMachine::TickLoop
- Inherits:
-
Object
- Object
- EventMachine::TickLoop
- Defined in:
- lib/em/tick_loop.rb
Overview
A TickLoop is useful when one needs to distribute amounts of work throughout ticks in order to maintain response times. It is also useful for simple repeated checks and metrics.
Instance Method Summary collapse
-
#initialize(*a, &b) ⇒ TickLoop
constructor
Arguments: A callback (EM::Callback) to call each tick.
-
#on_stop(*a, &b) ⇒ Object
Arguments: A callback (EM::Callback) to call once on the next stop (or immediately if already stopped).
-
#start ⇒ Object
Start the tick loop, will raise argument error if the loop is already running.
-
#stop ⇒ Object
Stop the tick loop immediately, and call it’s on_stop callbacks.
-
#stopped? ⇒ Boolean
Query if the loop is stopped.
Constructor Details
#initialize(*a, &b) ⇒ TickLoop
Arguments: A callback (EM::Callback) to call each tick. If the call returns :stop
then the loop will be stopped. Any other value is ignored.
35 36 37 38 39 |
# File 'lib/em/tick_loop.rb', line 35 def initialize(*a, &b) @work = EM::Callback(*a, &b) @stops = [] @stopped = true end |
Instance Method Details
#on_stop(*a, &b) ⇒ Object
Arguments: A callback (EM::Callback) to call once on the next stop (or immediately if already stopped).
43 44 45 46 47 48 49 |
# File 'lib/em/tick_loop.rb', line 43 def on_stop(*a, &b) if @stopped EM::Callback(*a, &b).call else @stops << EM::Callback(*a, &b) end end |
#start ⇒ Object
Start the tick loop, will raise argument error if the loop is already running.
66 67 68 69 70 |
# File 'lib/em/tick_loop.rb', line 66 def start raise ArgumentError, "double start" unless @stopped @stopped = false schedule end |
#stop ⇒ Object
Stop the tick loop immediately, and call it’s on_stop callbacks.
52 53 54 55 56 57 |
# File 'lib/em/tick_loop.rb', line 52 def stop @stopped = true until @stops.empty? @stops.shift.call end end |
#stopped? ⇒ Boolean
Query if the loop is stopped.
60 61 62 |
# File 'lib/em/tick_loop.rb', line 60 def stopped? @stopped end |