Module: Fluent::PluginHelper::EventLoop

Includes:
Thread
Included in:
Timer
Defined in:
lib/fluent/plugin_helper/event_loop.rb

Defined Under Namespace

Classes: DefaultWatcher

Constant Summary collapse

EVENT_LOOP_RUN_DEFAULT_TIMEOUT =

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

0.5

Constants included from Thread

Thread::THREAD_DEFAULT_WAIT_SECONDS

Instance Attribute Summary collapse

Attributes included from Thread

#_threads

Instance Method Summary collapse

Methods included from Thread

#stop, #thread_create, #thread_current_running?, #thread_exist?, #thread_running?, #thread_started?, #thread_wait_until_start, #thread_wait_until_stop

Instance Attribute Details

#_event_loopObject (readonly)

for tests



34
35
36
# File 'lib/fluent/plugin_helper/event_loop.rb', line 34

def _event_loop
  @_event_loop
end

Instance Method Details

#closeObject



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/fluent/plugin_helper/event_loop.rb', line 87

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) ⇒ Object



36
37
38
39
40
# File 'lib/fluent/plugin_helper/event_loop.rb', line 36

def event_loop_attach(watcher)
  @_event_loop_mutex.synchronize do
    @_event_loop.attach(watcher)
  end
end

#event_loop_running?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/fluent/plugin_helper/event_loop.rb', line 50

def event_loop_running?
  @_event_loop_running
end

#event_loop_wait_until_startObject



42
43
44
# File 'lib/fluent/plugin_helper/event_loop.rb', line 42

def event_loop_wait_until_start
  sleep(0.1) until event_loop_running?
end

#event_loop_wait_until_stopObject



46
47
48
# File 'lib/fluent/plugin_helper/event_loop.rb', line 46

def event_loop_wait_until_stop
  sleep(0.1) while event_loop_running?
end

#initializeObject



54
55
56
57
58
59
60
61
# File 'lib/fluent/plugin_helper/event_loop.rb', line 54

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
end

#shutdownObject



76
77
78
79
80
81
82
83
84
85
# File 'lib/fluent/plugin_helper/event_loop.rb', line 76

def shutdown
  @_event_loop_mutex.synchronize do
    @_event_loop.watchers.each {|w| w.detach if w.attached? }
  end
  while @_event_loop_running
    sleep 0.1
  end

  super
end

#startObject



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fluent/plugin_helper/event_loop.rb', line 63

def start
  super

  # event loop does not run here, so mutex lock is not required
  thread_create :event_loop do
    default_watcher = DefaultWatcher.new
    @_event_loop.attach(default_watcher)
    @_event_loop_running = true
    @_event_loop.run(@_event_loop_run_timeout) # this method blocks
    @_event_loop_running = false
  end
end

#terminateObject



99
100
101
102
103
104
105
106
# File 'lib/fluent/plugin_helper/event_loop.rb', line 99

def terminate
  @_event_loop = nil
  @_event_loop_running = false
  @_event_loop_mutex = nil
  @_event_loop_run_timeout = nil

  super
end