Class: FiberAudit::Runtime::SchedulerObserver

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

Overview

Explicit-runtime-only hooks for schedulers installed after RUBYOPT boot.

Defined Under Namespace

Modules: FiberHook, SchedulerCloseHook

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(watchdog:) ⇒ SchedulerObserver

Returns a new instance of SchedulerObserver.



81
82
83
84
85
# File 'lib/fiber_audit/runtime/scheduler_observer.rb', line 81

def initialize(watchdog:)
  @watchdog = watchdog
  @owner_pid = Process.pid
  @active = true
end

Instance Attribute Details

#owner_pidObject (readonly)

Returns the value of attribute owner_pid.



79
80
81
# File 'lib/fiber_audit/runtime/scheduler_observer.rb', line 79

def owner_pid
  @owner_pid
end

#watchdogObject (readonly)

Returns the value of attribute watchdog.



79
80
81
# File 'lib/fiber_audit/runtime/scheduler_observer.rb', line 79

def watchdog
  @watchdog
end

Class Method Details

.activate(watchdog:) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/fiber_audit/runtime/scheduler_observer.rb', line 34

def activate(watchdog:)
  raise RuntimeContractError, 'watchdog must be a FiberAudit::Runtime::Watchdog' unless watchdog.is_a?(Watchdog)

  install_fiber_hook!
  observer = new(watchdog: watchdog)
  @current = observer
  observer.attach_existing!
  observer
end

.deactivate(observer) ⇒ Object



59
60
61
62
# File 'lib/fiber_audit/runtime/scheduler_observer.rb', line 59

def deactivate(observer)
  @current = nil if @current.equal?(observer)
  observer.deactivate
end

.scheduler_closing(thread:) ⇒ Object



55
56
57
# File 'lib/fiber_audit/runtime/scheduler_observer.rb', line 55

def scheduler_closing(thread:)
  current_for_process&.scheduler_closing(thread: thread)
end

.scheduler_installed(scheduler:, thread:) ⇒ Object



48
49
50
51
52
53
# File 'lib/fiber_audit/runtime/scheduler_observer.rb', line 48

def scheduler_installed(scheduler:, thread:)
  observer = current_for_process
  return unless observer

  observer.scheduler_installed(scheduler: scheduler, thread: thread)
end

.scheduler_replacing(thread:) ⇒ Object



44
45
46
# File 'lib/fiber_audit/runtime/scheduler_observer.rb', line 44

def scheduler_replacing(thread:)
  current_for_process&.scheduler_closing(thread: thread)
end

Instance Method Details

#active_for_current_process?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/fiber_audit/runtime/scheduler_observer.rb', line 87

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

#attach_existing!Object



91
92
93
94
95
# File 'lib/fiber_audit/runtime/scheduler_observer.rb', line 91

def attach_existing!
  scheduler = Fiber.scheduler
  scheduler_installed(scheduler: scheduler, thread: Thread.current) if scheduler
  self
end

#deactivateObject



115
116
117
118
# File 'lib/fiber_audit/runtime/scheduler_observer.rb', line 115

def deactivate
  @active = false
  self
end

#scheduler_closing(thread:) ⇒ Object



110
111
112
113
# File 'lib/fiber_audit/runtime/scheduler_observer.rb', line 110

def scheduler_closing(thread:)
  watchdog.scheduler_closing(thread: thread) if active_for_current_process?
  self
end

#scheduler_installed(scheduler:, thread:) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/fiber_audit/runtime/scheduler_observer.rb', line 97

def scheduler_installed(scheduler:, thread:)
  return self unless active_for_current_process? && watchdog.enabled?

  install_close_hook!(scheduler)
  watchdog.scheduler_installed(thread: thread)
  self
rescue StandardError
  watchdog.scheduler_unsupported(thread: thread)
  raise unless watchdog.fail_open?

  self
end