Class: SemanticLogger::RactorProcessor

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

Overview

Internal, experimental: the processor of a Ractor other than the main one. Ruby 4.0 and later only.

The appenders belong to the main Ractor, which only it can use, so this sends each log entry to RactorReceiver in the main Ractor, which writes it to the appenders. It knows the name its Ractor is logged as. Adding or changing appenders, like every other setting, is only possible in the main Ractor.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ RactorProcessor

Returns a new instance of RactorProcessor.



19
20
21
22
# File 'lib/semantic_logger/ractor_processor.rb', line 19

def initialize
  ractor = ::Ractor.current
  @name  = ractor.name || "ractor-#{ractor.inspect[/\A#<Ractor:#(\d+)/, 1]}"
end

Instance Attribute Details

#name ⇒ Object (readonly)

Returns [String] the name that identifies this Ractor in the thread name of its log entries: its own name, or its number, as in "ractor-2".



17
18
19
# File 'lib/semantic_logger/ractor_processor.rb', line 17

def name
  @name
end

Class Method Details

.current ⇒ Object

Returns [RactorProcessor] the processor of the current Ractor.



11
12
13
# File 'lib/semantic_logger/ractor_processor.rb', line 11

def self.current
  ::Ractor.store_if_absent(:semantic_logger_processor) { new }
end

Instance Method Details

#flush ⇒ Object

Waits until the main Ractor has written the entries sent before this call, and has flushed the appenders. Returns the result of the main Ractor's flush, as SemanticLogger.flush there would. Returns false without waiting when the main Ractor is not receiving, for example since no appender was added.



42
43
44
45
46
47
48
49
50
# File 'lib/semantic_logger/ractor_processor.rb', line 42

def flush
  return false unless RactorReceiver.running?

  reply = ::Ractor::Port.new
  RactorReceiver.port << [:flush, reply]
  reply.receive
ensure
  reply&.close
end

#lag_check_interval=(_value) ⇒ Object

Raises:

  • (::Ractor::IsolationError)


68
69
70
# File 'lib/semantic_logger/ractor_processor.rb', line 68

def lag_check_interval=(_value)
  raise ::Ractor::IsolationError, main_only(:lag_check_interval=)
end

#log(log) ⇒ Object

Sends the log entry to the main Ractor. Its thread name is prefixed with this Ractor's name.

Sending copies the entry. When something in it cannot be copied, such as a Proc in its payload, those values are replaced (see Log#make_copyable) and it is sent again.



28
29
30
31
32
33
34
35
36
# File 'lib/semantic_logger/ractor_processor.rb', line 28

def log(log)
  log.thread_name = "#{name}/#{log.thread_name}"
  begin
    RactorReceiver.port << [:log, *RactorCopy.pack(log)]
  rescue StandardError
    RactorReceiver.port << [:log, *RactorCopy.pack(log.make_copyable)]
  end
  true
end

#logger ⇒ Object

Returns the internal logger, to report problems logging in this Ractor, such as an on_log subscriber that raised. It writes to this Ractor's $stderr.



54
55
56
# File 'lib/semantic_logger/ractor_processor.rb', line 54

def logger
  @logger ||= Appender::IO.new($stderr, level: :warn).tap { |logger| logger.name = self.class.name }
end

#non_blocking=(_value) ⇒ Object

Raises:

  • (::Ractor::IsolationError)


72
73
74
# File 'lib/semantic_logger/ractor_processor.rb', line 72

def non_blocking=(_value)
  raise ::Ractor::IsolationError, main_only(:non_blocking=)
end