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, 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.



31
32
33
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
65
66
67
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 31

def initialize(
  settings:,
  watchdog_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, probes_enabled, clock,
    session_id_source, pid_source, writer_factory, random
  )
  @settings = settings
  @watchdog_policy = watchdog_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
  @output_path = nil
  @recorder = nil
  @watchdog = nil
  @scheduler_observer = nil
  @active_operations = nil
  @redactor = nil
  @probe_registry = nil
  @execution_context_store = nil
  @rails_integration = nil
  start_process_session!
rescue StandardError => e
  startup_failure!(e)
end

Instance Attribute Details

#active_operationsObject (readonly)

Returns the value of attribute active_operations.



22
23
24
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 22

def active_operations
  @active_operations
end

#execution_context_storeObject (readonly)

Returns the value of attribute execution_context_store.



22
23
24
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 22

def execution_context_store
  @execution_context_store
end

#output_pathObject (readonly)

Returns the value of attribute output_path.



22
23
24
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 22

def output_path
  @output_path
end

#owner_pidObject (readonly)

Returns the value of attribute owner_pid.



22
23
24
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 22

def owner_pid
  @owner_pid
end

#probe_registryObject (readonly)

Returns the value of attribute probe_registry.



22
23
24
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 22

def probe_registry
  @probe_registry
end

#rails_integrationObject (readonly)

Returns the value of attribute rails_integration.



22
23
24
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 22

def rails_integration
  @rails_integration
end

#recorderObject (readonly)

Returns the value of attribute recorder.



22
23
24
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 22

def recorder
  @recorder
end

#redactorObject (readonly)

Returns the value of attribute redactor.



22
23
24
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 22

def redactor
  @redactor
end

#settingsObject (readonly)

Returns the value of attribute settings.



22
23
24
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 22

def settings
  @settings
end

#watchdogObject (readonly)

Returns the value of attribute watchdog.



22
23
24
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 22

def watchdog
  @watchdog
end

#watchdog_policyObject (readonly)

Returns the value of attribute watchdog_policy.



22
23
24
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 22

def watchdog_policy
  @watchdog_policy
end

Class Method Details

.startObject



27
28
29
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 27

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

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


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

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

#closed?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 77

def closed?
  @state == :closed
end

#disabled?Boolean

Returns:

  • (Boolean)


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

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

#ensure_current_process!Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 81

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

  abandon_inherited_runtime!
  @owner_pid = pid
  @output_path = nil
  @recorder = nil
  @watchdog = nil
  @scheduler_observer = nil
  @active_operations = nil
  @redactor = nil
  @probe_registry = nil
  @execution_context_store = nil
  @rails_integration = nil
  @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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/fiber_audit/runtime/lifecycle.rb', line 105

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