class OneApm::Support::EventLoop
Public Class Methods
new()
click to toggle source
# File lib/one_apm/support/event/event_loop.rb, line 10 def initialize @self_pipe_rd, @self_pipe_wr = IO.pipe @event_queue = Queue.new @stopped = false @timers = {} @subscriptions = Hash.new { |h,k| h[k] = [] } @subscriptions[:__add_timer] << Proc.new { |t| set_timer(t) } @subscriptions[:__add_event] << Proc.new { |e, blk| @subscriptions[e] << blk } end
Public Instance Methods
dispatch_event(event, args)
click to toggle source
# File lib/one_apm/support/event/event_loop.rb, line 95 def dispatch_event(event, args) OneApm::Manager.logger.debug("EventLoop: Dispatching event '#{event}' with #{@subscriptions[event].size} callback(s).") errors = [] @subscriptions[event].each do |s| begin s.call(*args) rescue OneApm::ForceRestartException, OneApm::ForceDisconnectException raise rescue => e errors << e end end if !errors.empty? OneApm::Manager.logger.error "#{errors.size} error(s) running task for event '#{event}' in Agent Event Loop:", *errors end end
fire(event, *args)
click to toggle source
# File lib/one_apm/support/event/event_loop.rb, line 122 def fire(event, *args) @event_queue << [event, args] wakeup end
fire_after(interval, event)
click to toggle source
# File lib/one_apm/support/event/event_loop.rb, line 132 def fire_after(interval, event) OneApm::Manager.logger.debug "Firing event #{event} after #{interval} seconds." fire(:__add_timer, Timer.new(interval, event, false)) end
fire_every(interval, event)
click to toggle source
# File lib/one_apm/support/event/event_loop.rb, line 127 def fire_every(interval, event) OneApm::Manager.logger.debug "Firing event #{event} every #{interval} seconds." fire(:__add_timer, Timer.new(interval, event, true)) end
fire_timer(timer)
click to toggle source
# File lib/one_apm/support/event/event_loop.rb, line 84 def fire_timer(timer) if timer.due? @event_queue << [timer.event] timer.set_fired_time end end
fire_timers()
click to toggle source
# File lib/one_apm/support/event/event_loop.rb, line 78 def fire_timers @timers.each do |event, timer| fire_timer(timer) end end
next_timeout()
click to toggle source
# File lib/one_apm/support/event/event_loop.rb, line 34 def next_timeout return nil if @timers.empty? timeout = @timers.values.map(&:next_fire_time).min - Time.now timeout < 0 ? 0 : timeout end
on(event, &blk)
click to toggle source
# File lib/one_apm/support/event/event_loop.rb, line 118 def on(event, &blk) fire(:__add_event, event, blk) end
prune_timers()
click to toggle source
# File lib/one_apm/support/event/event_loop.rb, line 91 def prune_timers @timers.delete_if { |e, t| t.finished? } end
reschedule_timer_for_event(e)
click to toggle source
# File lib/one_apm/support/event/event_loop.rb, line 114 def reschedule_timer_for_event(e) @timers[e].reschedule if @timers[e] end
run()
click to toggle source
# File lib/one_apm/support/event/event_loop.rb, line 49 def run OneApm::Manager.logger.debug "Running event loop" while !stopped? run_once end end
run_once(nonblock=false)
click to toggle source
# File lib/one_apm/support/event/event_loop.rb, line 56 def run_once(nonblock=false) wait_to_run(nonblock) prune_timers fire_timers until @event_queue.empty? evt, args = @event_queue.pop dispatch_event(evt, args) reschedule_timer_for_event(evt) end end
set_timer(timer)
click to toggle source
# File lib/one_apm/support/event/event_loop.rb, line 21 def set_timer(timer) existing_timer = @timers[timer.event] if existing_timer elapsed_interval = Time.now - existing_timer.last_interval_start timer.advance(elapsed_interval) end @timers[timer.event] = timer fire_timer(timer) end
stop()
click to toggle source
# File lib/one_apm/support/event/event_loop.rb, line 44 def stop @stopped = true wakeup end
stopped?()
click to toggle source
# File lib/one_apm/support/event/event_loop.rb, line 40 def stopped? @stopped end
wait_to_run(nonblock)
click to toggle source
# File lib/one_apm/support/event/event_loop.rb, line 69 def wait_to_run(nonblock) timeout = nonblock ? 0 : next_timeout ready = IO.select([@self_pipe_rd], nil, nil, timeout) if ready && ready[0] && ready[0][0] && ready[0][0] == @self_pipe_rd @self_pipe_rd.read(1) end end
wakeup()
click to toggle source
# File lib/one_apm/support/event/event_loop.rb, line 137 def wakeup @self_pipe_wr.write_nonblock '.' rescue Errno::EAGAIN OneApm::Manager.logger.debug "Failed to wakeup event loop" end