Class: FiberAudit::Runtime::RailsIntegration

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

Overview

Process-local, idempotent Rails integration. Installs prepend hooks for Rails boundaries when detected. Becomes inert after deactivation or fork. Supports late-load rescanning for Rails components loaded after boot.

Defined Under Namespace

Modules: CableHook, ControllerHook, JobHook, RequireHook Classes: Middleware

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context_store:, recorder: nil) ⇒ RailsIntegration

Returns a new instance of RailsIntegration.



14
15
16
17
18
19
20
21
22
# File 'lib/fiber_audit/runtime/rails_integration.rb', line 14

def initialize(context_store:, recorder: nil)
  @context_store = context_store
  @recorder = recorder
  @owner_pid = Process.pid
  @active = true
  @middleware_stack = nil
  @require_hook_installed = false
  @mutex = Mutex.new
end

Class Attribute Details

.currentObject (readonly)

Returns the value of attribute current.



75
76
77
# File 'lib/fiber_audit/runtime/rails_integration.rb', line 75

def current
  @current
end

Instance Attribute Details

#context_storeObject (readonly)

Returns the value of attribute context_store.



12
13
14
# File 'lib/fiber_audit/runtime/rails_integration.rb', line 12

def context_store
  @context_store
end

#owner_pidObject (readonly)

Returns the value of attribute owner_pid.



12
13
14
# File 'lib/fiber_audit/runtime/rails_integration.rb', line 12

def owner_pid
  @owner_pid
end

#recorderObject (readonly)

Returns the value of attribute recorder.



12
13
14
# File 'lib/fiber_audit/runtime/rails_integration.rb', line 12

def recorder
  @recorder
end

Class Method Details

.activate(context_store: ExecutionContext, recorder: nil) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/fiber_audit/runtime/rails_integration.rb', line 65

def activate(context_store: ExecutionContext, recorder: nil)
  integration = new(context_store: context_store, recorder: recorder)
  integration.install!
  @current = integration
  @current
rescue StandardError
  @current = nil
  raise
end

.deactivate(integration = nil) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/fiber_audit/runtime/rails_integration.rb', line 77

def deactivate(integration = nil)
  target = integration || @current
  return unless target

  target.deactivate!
  @current = nil if @current.equal?(target)
end

.rescan!Object



85
86
87
# File 'lib/fiber_audit/runtime/rails_integration.rb', line 85

def rescan!
  @current&.rescan! if @current&.active_for_current_process?
end

.rescan_after_require!Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/fiber_audit/runtime/rails_integration.rb', line 89

def rescan_after_require!
  key = :fiber_audit_rails_require_rescan
  thread = Thread.current
  acquired = false
  return if thread.thread_variable_get(key)

  thread.thread_variable_set(key, true)
  acquired = true
  rescan!
ensure
  thread&.thread_variable_set(key, false) if acquired
end

Instance Method Details

#active_for_current_process?Boolean

Returns:

  • (Boolean)


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

def active_for_current_process?
  @active && @owner_pid == Process.pid
end

#deactivate!Object



28
29
30
31
# File 'lib/fiber_audit/runtime/rails_integration.rb', line 28

def deactivate!
  @active = false
  self
end

#install!Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fiber_audit/runtime/rails_integration.rb', line 33

def install!
  return unless active_for_current_process?

  @mutex.synchronize do
    install_require_hook
    install_controller_hook
    install_job_hook
    install_cable_hook
    try_install_middleware_hook
  end
  self
rescue StandardError => e
  handle_installation_failure(e)
  self
end

#rescan!Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fiber_audit/runtime/rails_integration.rb', line 49

def rescan!
  return unless active_for_current_process?

  @mutex.synchronize do
    install_controller_hook
    install_job_hook
    install_cable_hook
    try_install_middleware_hook
  end
  self
rescue StandardError => e
  handle_installation_failure(e)
  self
end