Class: LogBrew::Rails::Runtime

Inherits:
Object
  • Object
show all
Defined in:
lib/logbrew/rails_integration.rb

Overview

Owns one lazy automatic-delivery client per operating-system process.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration, transport_factory: nil, client_factory: nil, timestamp_provider: nil, process_id_provider: nil, on_error: nil) ⇒ Runtime

Returns a new instance of Runtime.



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/logbrew/rails_integration.rb', line 254

def initialize(
  configuration,
  transport_factory: nil,
  client_factory: nil,
  timestamp_provider: nil,
  process_id_provider: nil,
  on_error: nil
)
  unless configuration.is_a?(Configuration)
    raise SdkError.new("configuration_error", "Rails runtime requires a Rails configuration")
  end

  @configuration = configuration
  @transport_factory = transport_factory || method(:build_transport)
  @client_factory = client_factory || method(:build_client)
  @timestamp_provider = timestamp_provider || -> { Time.now.utc }
  @process_id_provider = process_id_provider || -> { Process.pid }
  @on_error = on_error
  @mutex = Mutex.new
  @state_process_id = @process_id_provider.call
  @client = nil
  @shutdown_response = nil
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



252
253
254
# File 'lib/logbrew/rails_integration.rb', line 252

def configuration
  @configuration
end

Instance Method Details

#clientObject



278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/logbrew/rails_integration.rb', line 278

def client
  return nil unless @configuration.enabled?

  prepare_process_state
  @mutex.synchronize do
    return nil unless @shutdown_response.nil?

    @client ||= create_client
  end
rescue StandardError => error
  report_error("client_initialization", error)
  nil
end

#delivery_healthObject



292
293
294
295
296
297
298
# File 'lib/logbrew/rails_integration.rb', line 292

def delivery_health
  active_client = client
  active_client&.delivery_health
rescue StandardError => error
  report_error("delivery_health", error)
  nil
end

#report_error(stage, error) ⇒ Object



315
316
317
318
319
320
321
# File 'lib/logbrew/rails_integration.rb', line 315

def report_error(stage, error)
  return unless @on_error.respond_to?(:call)

  @on_error.call(stage.to_s, error)
rescue StandardError
  nil
end

#shutdownObject



300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/logbrew/rails_integration.rb', line 300

def shutdown
  return nil unless @configuration.enabled?

  prepare_process_state
  @mutex.synchronize do
    return @shutdown_response unless @shutdown_response.nil?
    return nil if @client.nil?

    @shutdown_response = @client.shutdown
  end
rescue StandardError => error
  report_error("shutdown", error)
  nil
end