Class: Phronomy::Runtime

Inherits:
Object
  • Object
show all
Defined in:
lib/phronomy/engine/runtime.rb,
lib/phronomy/engine/runtime/timer_queue.rb,
lib/phronomy/engine/runtime/timer_service.rb,
lib/phronomy/engine/runtime/shutdown_result.rb

Overview

Owns the EventLoop, offloaded synchronous work, timers and shutdown lifecycle.

Runtime no longer schedules arbitrary Tasks. Framework control flow belongs to EventLoop/FSMSession; synchronous work that must not run on the EventLoop belongs to OffloadPool.

Defined Under Namespace

Classes: ShutdownResult, TimerQueue, TimerService

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRuntime

Returns a new instance of Runtime.



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/phronomy/engine/runtime.rb', line 67

def initialize
  @timer_service = TimerService.new
  @pool_registry = Phronomy::Concurrency::PoolRegistry.new(
    timer_queue_provider: -> { timer_queue }
  )
  @lifecycle_mutex = Mutex.new
  @shutdown_mutex = Mutex.new
  @state = :running
  @event_loop = nil
  @failure = nil
  @shutdown_result = nil
end

Class Method Details

.default_if_initialized_for_testObject



23
24
25
# File 'lib/phronomy/engine/runtime.rb', line 23

def default_if_initialized_for_test
  instance_mutex.synchronize { @instance }
end

.in_event_loop_context?Boolean

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/phronomy/engine/runtime.rb', line 55

def in_event_loop_context?
  runtime = instance_mutex.synchronize { @instance }
  runtime&.event_loop_current? || false
end

.instanceObject



17
18
19
20
21
# File 'lib/phronomy/engine/runtime.rb', line 17

def instance
  instance_mutex.synchronize do
    @instance ||= new
  end
end

.replace_default_for_test(runtime) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/phronomy/engine/runtime.rb', line 27

def replace_default_for_test(runtime)
  instance_mutex.synchronize do
    previous = @instance
    @instance = runtime
    previous
  end
end

.reset_default!(timeout: Phronomy.configuration.event_loop_stop_grace_seconds) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/phronomy/engine/runtime.rb', line 39

def reset_default!(timeout: Phronomy.configuration.event_loop_stop_grace_seconds)
  runtime = instance_mutex.synchronize { @instance }
  return ShutdownResult.not_started unless runtime

  result = runtime.shutdown(timeout: timeout)
  unless result.cleanup_complete?
    raise Phronomy::RuntimeShutdownError,
      "Runtime cleanup is incomplete; default Runtime was retained"
  end

  instance_mutex.synchronize do
    @instance = nil if @instance.equal?(runtime)
  end
  result
end

.restore_default_for_test(runtime) ⇒ Object



35
36
37
# File 'lib/phronomy/engine/runtime.rb', line 35

def restore_default_for_test(runtime)
  instance_mutex.synchronize { @instance = runtime }
end

Instance Method Details

#__event_loop_failed(error) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/phronomy/engine/runtime.rb', line 136

def __event_loop_failed(error)
  @lifecycle_mutex.synchronize do
    return if @shutdown_result || @state == :terminated

    @failure ||= error
    @state = :failed
  end
end

#__timer_queueObject

Internal EventLoop access that does not recursively initialise EventLoop.



106
107
108
# File 'lib/phronomy/engine/runtime.rb', line 106

def __timer_queue
  @timer_service.timer_queue
end

#event_loopObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/phronomy/engine/runtime.rb', line 110

def event_loop
  @lifecycle_mutex.synchronize do
    case @state
    when :running
      unless @event_loop
        @event_loop = EventLoop.new(runtime: self)
        @timer_service.wake_with { @event_loop&.wake }
      end
      @event_loop
    when :draining
      return @event_loop if @event_loop

      raise Phronomy::RuntimeShutdownError,
        "EventLoop was not initialized before Runtime shutdown began"
    else
      raise Phronomy::RuntimeShutdownError,
        "Runtime is #{@state}; EventLoop is unavailable"
    end
  end
end

#event_loop_current?Boolean

Returns:

  • (Boolean)


131
132
133
134
# File 'lib/phronomy/engine/runtime.rb', line 131

def event_loop_current?
  loop_instance = @lifecycle_mutex.synchronize { @event_loop }
  loop_instance&.current? || false
end

#offload(pool_size: Phronomy.configuration.offload_pool_size, queue_size: Phronomy.configuration.offload_queue_size) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/phronomy/engine/runtime.rb', line 84

def offload(
  pool_size: Phronomy.configuration.offload_pool_size,
  queue_size: Phronomy.configuration.offload_queue_size
)
  ensure_accepting_work!
  @pool_registry.default_pool(pool_size: pool_size, queue_size: queue_size)
end

#pool(name, size: 10, queue_size: 100) ⇒ Object



92
93
94
95
# File 'lib/phronomy/engine/runtime.rb', line 92

def pool(name, size: 10, queue_size: 100)
  ensure_accepting_work!
  @pool_registry.named_pool(name, size: size, queue_size: queue_size)
end

#shutdown(timeout: Phronomy.configuration.event_loop_stop_grace_seconds, cancel_grace: timeout) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/phronomy/engine/runtime.rb', line 145

def shutdown(
  timeout: Phronomy.configuration.event_loop_stop_grace_seconds,
  cancel_grace: timeout
)
  if event_loop_current?
    raise Phronomy::RuntimeShutdownReentrancyError,
      "Runtime#shutdown must be called from an external management thread"
  end
  validate_timeout!(timeout, :timeout)
  validate_timeout!(cancel_grace, :cancel_grace)

  @shutdown_mutex.synchronize do
    return @shutdown_result if @shutdown_result

    # Phase 1 — drain sessions with the full configured grace.
    drain_deadline = monotonic_now + timeout
    loop_instance = @lifecycle_mutex.synchronize do
      @state = :draining unless @state == :failed
      @event_loop
    end
    loop_instance&.begin_draining

    loop_idle = !loop_instance || loop_instance.wait_until_idle(drain_deadline)

    @lifecycle_mutex.synchronize do
      @state = :stopping unless @state == :failed
    end

    # Phase 2 — stop the EventLoop thread with a short independent budget.
    # An idle EventLoop processes STOP and exits in < 1ms normally; the 0.2s
    # budget here is only a safety net for OS scheduling jitter.
    stop_deadline = monotonic_now + [cancel_grace.to_f, 0.2].max
    event_loop_status = if loop_instance
      loop_instance.stop_and_join(deadline: stop_deadline)
    else
      :not_started
    end

    subsystem_error = shutdown_pools_and_timer
    cleanup_complete = loop_idle &&
      (!loop_instance || !loop_instance.thread_alive?) &&
      event_loop_status != :cancel_timeout &&
      subsystem_error.nil?

    failure = @lifecycle_mutex.synchronize { @failure } || subsystem_error
    runtime_outcome = if failure || event_loop_status == :failed
      :failed
    else
      :terminated
    end

    result = ShutdownResult.new(
      runtime_outcome: runtime_outcome,
      cleanup_status: cleanup_complete ? :complete : :incomplete,
      event_loop_status: event_loop_status,
      task_registry_status: :empty,
      error: failure
    )

    @lifecycle_mutex.synchronize do
      @state = cleanup_complete ? runtime_outcome : :failed
      @shutdown_result = result
    end
    result
  end
end

#stateObject



80
81
82
# File 'lib/phronomy/engine/runtime.rb', line 80

def state
  @lifecycle_mutex.synchronize { @state }
end

#timer_queueObject

Public timer access also ensures that the EventLoop that drives timers is alive.



98
99
100
101
102
103
# File 'lib/phronomy/engine/runtime.rb', line 98

def timer_queue
  ensure_accepting_work!
  timer = @timer_service.timer_queue
  event_loop
  timer
end