Class: SemanticLogger::QueueProcessor::Reopener

Inherits:
Object
  • Object
show all
Defined in:
lib/semantic_logger/queue_processor/reopener.rb

Overview

Internal: reopens the appender of a queue processor, on its worker thread, once asked.

The worker thread asks it between writes, so the appender is never reopened while it is writing, such as a file being closed under a write, or a connection ended under a request. Nor is it reopened on the thread that asks, often a signal trap after a log rotation, where Ruby does not allow a Mutex to be locked, as connecting with a timeout does.

It knows whether a reopen has been asked for, and reports a reopen that fails to the internal logger. Asked from any thread, and from a signal trap, so it records that on a Queue.

Instance Method Summary collapse

Constructor Details

#initialize(appender, reporter) ⇒ Reopener

Returns a new instance of Reopener.



13
14
15
16
17
18
# File 'lib/semantic_logger/queue_processor/reopener.rb', line 13

def initialize(appender, reporter)
  @appender  = appender
  @reporter  = reporter
  @asked     = Queue.new
  @reopening = false
end

Instance Method Details

#ask ⇒ Object

Ask for the appender to be reopened before the worker thread writes again.



21
22
23
# File 'lib/semantic_logger/queue_processor/reopener.rb', line 21

def ask
  @asked << true
end

#pending? ⇒ Boolean

Returns [true|false] whether a reopen has been asked for and the worker thread has not yet finished it, including one that it is running. Reads only a Queue and a flag, so that it can be called from any thread, or a signal trap. Reads them in the opposite order to the worker thread, which marks the reopen as running before it takes the request, so that a reopen starting between the two reads is still seen.

Returns:

  • (Boolean)


30
# File 'lib/semantic_logger/queue_processor/reopener.rb', line 30

def pending? = !@asked.empty? || @reopening

#reopen_if_asked ⇒ Object

Reopen the appender, if asked to since it was last reopened. Called by the worker thread, between writes. A reopen that fails is reported rather than raised, whatever the exception, as Appenders#reopen reports it, so that one that is not a StandardError, such as a LoadError or NotImplementedError, does not stop the worker thread for good: the appender keeps what it had, and writing to it then fails, or not, as it would have.

Returns nil, or the SystemStackError when reopening overflowed the stack, so that the worker thread hands over to a new one, since it must not overflow its stack again: on macOS a second stack overflow on the same thread crashes Ruby.



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/semantic_logger/queue_processor/reopener.rb', line 41

def reopen_if_asked
  return if @asked.empty?

  @reopening = true
  @asked.clear
  @appender.reopen if @appender.respond_to?(:reopen)
  nil
rescue Exception => e
  @reporter.log(:error, "Async: Failed to reopen the appender", e)
  e if e.is_a?(SystemStackError)
ensure
  @reopening = false
end