Class: Logging::Appenders::Buffering::PeriodicFlusher

Inherits:
Object
  • Object
show all
Defined in:
lib/logging/appenders/buffering.rb

Overview

The PeriodicFlusher contains an internal run loop that will periodically wake up and flush any log events contained in the message buffer of the owning appender instance. The PeriodicFlusher relies on a signal from the appender in order to wakeup and perform the flush on the appender.

Instance Method Summary collapse

Constructor Details

#initialize(appender, period) ⇒ PeriodicFlusher

Create a new PeriodicFlusher instance that will call the flush method on the given appender. The flush method will be called every period seconds, but only when the message buffer is non-empty.



309
310
311
312
313
314
315
316
317
318
# File 'lib/logging/appenders/buffering.rb', line 309

def initialize( appender, period )
  @appender = appender
  @period = period

  @mutex = Mutex.new
  @cv = ConditionVariable.new
  @thread = nil
  @waiting = nil
  @signaled = false
end

Instance Method Details

#signalObject

Signal the periodic flusher. This will wake up the run loop if it is currently waiting for something to do. If the signal method is never called, the periodic flusher will never perform the flush action on the appender.



354
355
356
357
358
359
360
361
362
363
# File 'lib/logging/appenders/buffering.rb', line 354

def signal
  return if Thread.current == @thread   # don't signal ourselves
  return if @signaled                   # don't need to signal again

  @mutex.synchronize {
    @signaled = true
    @cv.signal
  }
  self
end

#startObject

Start the periodic flusher’s internal run loop.



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/logging/appenders/buffering.rb', line 322

def start
  return if @thread

  @thread = Thread.new { loop {
    begin
      break if Thread.current[:stop]
      _wait_for_signal
      sleep @period unless Thread.current[:stop]
      @appender.flush
    rescue => err
      ::Logging.log_internal {"PeriodicFlusher for appender #{@appender.inspect} encountered an error"}
      ::Logging.log_internal(-2) {err}
    end
  }; @thread = nil }

  self
end

#stopObject

Stop the periodic flusher’s internal run loop.



342
343
344
345
346
347
# File 'lib/logging/appenders/buffering.rb', line 342

def stop
  return if @thread.nil?
  @thread[:stop] = true
  signal if waiting?
  self
end

#waiting?Boolean

Returns true if the flusher is waiting for a signal. Returns false if the flusher is somewhere in the processing loop.

Returns:

  • (Boolean)


368
369
370
# File 'lib/logging/appenders/buffering.rb', line 368

def waiting?
  @waiting
end