module Fluent::PluginHelper::EventLoop

Constants

EVENT_LOOP_RUN_DEFAULT_TIMEOUT

stop : [-] shutdown : detach all event watchers on event loop close : stop event loop terminate: initialize internal state

EVENT_LOOP_SHUTDOWN_TIMEOUT

Attributes

_event_loop[R]

Public Class Methods

new() click to toggle source
Calls superclass method Fluent::PluginHelper::Thread.new
# File lib/fluent/plugin_helper/event_loop.rb, line 74
def initialize
  super
  @_event_loop = Coolio::Loop.new
  @_event_loop_running = false
  @_event_loop_mutex = Mutex.new
  # plugin MAY configure loop run timeout in #configure
  @_event_loop_run_timeout = EVENT_LOOP_RUN_DEFAULT_TIMEOUT
  @_event_loop_attached_watchers = []
end

Public Instance Methods

after_shutdown() click to toggle source
# File lib/fluent/plugin_helper/event_loop.rb, line 116
def after_shutdown
  timeout_at = Fluent::Clock.now + EVENT_LOOP_SHUTDOWN_TIMEOUT
  @_event_loop_mutex.synchronize do
    @_event_loop.watchers.reverse.each do |w|
      begin
        w.detach
      rescue => e
        log.warn "unexpected error while detaching event loop watcher", error: e
      end
    end
  end
  while @_event_loop_running
    if Fluent::Clock.now >= timeout_at
      log.warn "event loop does NOT exit until hard timeout."
      raise "event loop does NOT exit until hard timeout." if @under_plugin_development
      break
    end
    sleep 0.1
  end

  super
end
close() click to toggle source
Calls superclass method Fluent::PluginHelper::Thread#close
# File lib/fluent/plugin_helper/event_loop.rb, line 139
def close
  if @_event_loop_running
    begin
      @_event_loop.stop # we cannot check loop is running or not
    rescue RuntimeError => e
      raise unless e.message == 'loop not running'
    end
  end

  super
end
event_loop_attach(watcher) click to toggle source
# File lib/fluent/plugin_helper/event_loop.rb, line 38
def event_loop_attach(watcher)
  @_event_loop_mutex.synchronize do
    @_event_loop.attach(watcher)
    @_event_loop_attached_watchers << watcher
    watcher
  end
end
event_loop_detach(watcher) click to toggle source
# File lib/fluent/plugin_helper/event_loop.rb, line 46
def event_loop_detach(watcher)
  if watcher.attached?
    watcher.detach
  end
  @_event_loop_mutex.synchronize do
    @_event_loop_attached_watchers.delete(watcher)
  end
end
event_loop_running?() click to toggle source
# File lib/fluent/plugin_helper/event_loop.rb, line 70
def event_loop_running?
  @_event_loop_running
end
event_loop_wait_until_start() click to toggle source
# File lib/fluent/plugin_helper/event_loop.rb, line 55
def event_loop_wait_until_start
  sleep(0.1) until event_loop_running?
end
event_loop_wait_until_stop() click to toggle source
# File lib/fluent/plugin_helper/event_loop.rb, line 59
def event_loop_wait_until_stop
  timeout_at = Fluent::Clock.now + EVENT_LOOP_SHUTDOWN_TIMEOUT
  sleep(0.1) while event_loop_running? && Fluent::Clock.now < timeout_at
  if @_event_loop_running
    puts "terminating event_loop forcedly"
    caller.each{|bt| puts "\t#{bt}" }
    @_event_loop.stop rescue nil
    @_event_loop_running = true
  end
end
shutdown() click to toggle source
Calls superclass method
# File lib/fluent/plugin_helper/event_loop.rb, line 100
def shutdown
  @_event_loop_mutex.synchronize do
    @_event_loop_attached_watchers.reverse.each do |w|
      if w.attached?
        begin
          w.detach
        rescue => e
          log.warn "unexpected error while detaching event loop watcher", error: e
        end
      end
    end
  end

  super
end
start() click to toggle source
Calls superclass method
# File lib/fluent/plugin_helper/event_loop.rb, line 84
def start
  super

  # event loop does not run here, so mutex lock is not required
  thread_create :event_loop do
    begin
      default_watcher = DefaultWatcher.new
      event_loop_attach(default_watcher)
      @_event_loop_running = true
      @_event_loop.run(@_event_loop_run_timeout) # this method blocks
    ensure
      @_event_loop_running = false
    end
  end
end
terminate() click to toggle source
Calls superclass method Fluent::PluginHelper::Thread#terminate
# File lib/fluent/plugin_helper/event_loop.rb, line 151
def terminate
  @_event_loop = nil
  @_event_loop_running = false
  @_event_loop_mutex = nil
  @_event_loop_run_timeout = nil

  super
end