Class: FiberAudit::Runtime::Lifecycle

Inherits:
Object
  • Object
show all
Defined in:
lib/fiber_audit/runtime/lifecycle.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings:, watchdog_policy: nil, operation_liveness_policy: nil, probes_enabled: false, clock: Clock.new, session_id_source: SecureRandom.method(:uuid), pid_source: Process.method(:pid), writer_factory: JSONL::Writer.method(:open), random: Sampler::RANDOM_SOURCE) ⇒ Lifecycle

Returns a new instance of Lifecycle.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 34

def initialize(
  settings:,
  watchdog_policy: nil,
  operation_liveness_policy: nil,
  probes_enabled: false,
  clock: Clock.new,
  session_id_source: SecureRandom.method(:uuid),
  pid_source: Process.method(:pid),
  writer_factory: JSONL::Writer.method(:open),
  random: Sampler::RANDOM_SOURCE
)
  validate_dependencies!(
    settings, watchdog_policy, operation_liveness_policy, probes_enabled, clock,
    session_id_source, pid_source, writer_factory, random
  )
  @settings = settings
  @watchdog_policy = watchdog_policy
  @operation_liveness_policy = operation_liveness_policy
  @probes_enabled = probes_enabled
  @clock = clock
  @session_id_source = session_id_source
  @pid_source = pid_source
  @writer_factory = writer_factory
  @random = random
  @owner_pid = current_pid
  @state = :starting
  reset_process_components!
  start_process_session!
rescue StandardError => e
  startup_failure!(e)
end

Instance Attribute Details

#active_operationsObject (readonly)

Returns the value of attribute active_operations.



24
25
26
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 24

def active_operations
  @active_operations
end

#execution_context_storeObject (readonly)

Returns the value of attribute execution_context_store.



24
25
26
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 24

def execution_context_store
  @execution_context_store
end

#operation_liveness_monitorObject (readonly)

Returns the value of attribute operation_liveness_monitor.



24
25
26
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 24

def operation_liveness_monitor
  @operation_liveness_monitor
end

#operation_liveness_policyObject (readonly)

Returns the value of attribute operation_liveness_policy.



24
25
26
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 24

def operation_liveness_policy
  @operation_liveness_policy
end

#output_pathObject (readonly)

Returns the value of attribute output_path.



24
25
26
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 24

def output_path
  @output_path
end

#owner_pidObject (readonly)

Returns the value of attribute owner_pid.



24
25
26
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 24

def owner_pid
  @owner_pid
end

#probe_registryObject (readonly)

Returns the value of attribute probe_registry.



24
25
26
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 24

def probe_registry
  @probe_registry
end

#rails_integrationObject (readonly)

Returns the value of attribute rails_integration.



24
25
26
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 24

def rails_integration
  @rails_integration
end

#recorderObject (readonly)

Returns the value of attribute recorder.



24
25
26
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 24

def recorder
  @recorder
end

#redactorObject (readonly)

Returns the value of attribute redactor.



24
25
26
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 24

def redactor
  @redactor
end

#settingsObject (readonly)

Returns the value of attribute settings.



24
25
26
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 24

def settings
  @settings
end

#watchdogObject (readonly)

Returns the value of attribute watchdog.



24
25
26
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 24

def watchdog
  @watchdog
end

#watchdog_policyObject (readonly)

Returns the value of attribute watchdog_policy.



24
25
26
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 24

def watchdog_policy
  @watchdog_policy
end

Class Method Details

.startObject



30
31
32
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 30

def self.start(...)
  new(...)
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 66

def active?
  @state == :active && recorder&.active?
end

#closed?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 74

def closed?
  @state == :closed
end

#disabled?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 70

def disabled?
  @state == :disabled || recorder&.disabled?
end

#ensure_current_process!Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 78

def ensure_current_process!
  pid = current_pid
  return self if pid == owner_pid

  abandon_inherited_runtime!
  @owner_pid = pid
  reset_process_components!
  @state = :starting
  # Reset fiber-local context after fork
  ExecutionContext.reset!
  start_process_session!
  self
rescue StandardError => e
  process_failure!(e)
end

#shutdown(exception: nil) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 94

def shutdown(exception: nil)
  ensure_current_process!
  return @summary if closed?

  runtime_error = stop_runtime_observers
  status = runtime_error && exception.nil? ? :degraded : shutdown_status(exception)
  @summary = recorder&.close(status: status)
  @state = :closed
  raise runtime_error if runtime_error && !settings.policy.fail_open?

  @summary
rescue StandardError => e
  @state = :closed
  raise e unless settings.policy.fail_open?

  nil
end