Module: Lich::Common::SessionLifecycle
- Defined in:
- documented/common/session_lifecycle.rb
Overview
Minimal lifecycle coordinator for session summary reporting. Registers a session, emits periodic heartbeats, and unregisters on clean shutdown.
Constant Summary collapse
- REGISTRATION_DELAY_SECONDS =
5
Class Method Summary collapse
-
.attempt_register(session_name:, role:, frontend:, started_epoch:, started_iso:, registration_delay:) ⇒ Boolean
Performs deferred register attempt once game context is available.
-
.game_context_ready? ⇒ Boolean
Indicates whether XML runtime context is sufficient for registration.
-
.resolve_frontend ⇒ String?
Resolves frontend identifier for session reporting.
-
.resolve_game_code ⇒ String?
Resolves game code from XML runtime state.
-
.resolve_role(argv:, detachable_client_port:) ⇒ String
Resolves the logical runtime role used for reporting.
-
.resolve_session_name(argv:, account_character: nil) ⇒ String
Resolves the reporting session name from launch/runtime context.
-
.start(session_name:, role:, heartbeat_interval: SessionsSettings::HEARTBEAT_INTERVAL_SECONDS, registration_delay: REGISTRATION_DELAY_SECONDS) ⇒ Boolean
Starts lifecycle tracking and heartbeat emission for the current process.
-
.stop ⇒ Boolean
Stops lifecycle tracking and unregisters the current process.
Class Method Details
.attempt_register(session_name:, role:, frontend:, started_epoch:, started_iso:, registration_delay:) ⇒ Boolean
Performs deferred register attempt once game context is available.
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'documented/common/session_lifecycle.rb', line 227 def self.attempt_register(session_name:, role:, frontend:, started_epoch:, started_iso:, registration_delay:) begin game_code = resolve_game_code return false if game_code.nil? Lich.log( "info: SessionLifecycle deferred register attempt " \ "pid=#{Process.pid} session=#{session_name.inspect} role=#{role.inspect} " \ "attempt_epoch=#{Time.now.to_i}" ) if Lich.respond_to?(:log) SessionsSettings.send(:register_session_admitted, pid: Process.pid, session_name: session_name, role: role, state: 'running', frontend: frontend, game_code: game_code) Lich.log( "info: SessionLifecycle deferred register success " \ "pid=#{Process.pid} session=#{session_name.inspect} role=#{role.inspect} " \ "success_epoch=#{Time.now.to_i}" ) if Lich.respond_to?(:log) true rescue StandardError => e Lich.log( "warning: SessionLifecycle deferred register failed: #{e.class}: #{e.} " \ "pid=#{Process.pid} session=#{session_name.inspect} role=#{role.inspect} " \ "started_epoch=#{started_epoch} started_iso=#{started_iso} delay=#{registration_delay}s" ) if Lich.respond_to?(:log) false end end |
.game_context_ready? ⇒ Boolean
Indicates whether XML runtime context is sufficient for registration.
204 205 206 |
# File 'documented/common/session_lifecycle.rb', line 204 def self.game_context_ready? !resolve_game_code.nil? end |
.resolve_frontend ⇒ String?
Resolves frontend identifier for session reporting.
186 187 188 189 190 |
# File 'documented/common/session_lifecycle.rb', line 186 def self.resolve_frontend return $frontend if defined?($frontend) && !$frontend.nil? && !$frontend.to_s.empty? nil end |
.resolve_game_code ⇒ String?
Resolves game code from XML runtime state.
195 196 197 198 199 |
# File 'documented/common/session_lifecycle.rb', line 195 def self.resolve_game_code return XMLData.game if defined?(XMLData) && XMLData.respond_to?(:game) && !XMLData.game.to_s.empty? nil end |
.resolve_role(argv:, detachable_client_port:) ⇒ String
Resolves the logical runtime role used for reporting.
48 49 50 51 52 53 |
# File 'documented/common/session_lifecycle.rb', line 48 def self.resolve_role(argv:, detachable_client_port:) return 'detachable' unless detachable_client_port.nil? return 'headless' if argv.include?('--without-frontend') 'session' end |
.resolve_session_name(argv:, account_character: nil) ⇒ String
Resolves the reporting session name from launch/runtime context.
Resolution order:
--login <name>CLI argument- account character provided by auth layer
XMLData.namewhen available after parser initialization- deterministic PID fallback (
pid-<pid>)
31 32 33 34 35 36 37 38 39 40 41 |
# File 'documented/common/session_lifecycle.rb', line 31 def self.resolve_session_name(argv:, account_character: nil) if (login_idx = argv.index('--login')) && argv[login_idx + 1] argv[login_idx + 1].capitalize elsif account_character && !account_character.to_s.empty? account_character elsif defined?(XMLData) && XMLData.respond_to?(:name) && !XMLData.name.to_s.empty? XMLData.name else "pid-#{Process.pid}" end end |
.start(session_name:, role:, heartbeat_interval: SessionsSettings::HEARTBEAT_INTERVAL_SECONDS, registration_delay: REGISTRATION_DELAY_SECONDS) ⇒ Boolean
Starts lifecycle tracking and heartbeat emission for the current process. Registration is intentionally deferred to allow XMLData game context to initialize.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'documented/common/session_lifecycle.rb', line 63 def self.start(session_name:, role:, heartbeat_interval: SessionsSettings::HEARTBEAT_INTERVAL_SECONDS, registration_delay: REGISTRATION_DELAY_SECONDS) feature_enabled = SessionsSettings.enabled? return false unless feature_enabled @mutex.synchronize do return false if @started @feature_enabled = feature_enabled frontend = resolve_frontend started_epoch = Time.now.to_i started_iso = Time.at(started_epoch).utc.iso8601 scheduled_register_epoch = started_epoch + registration_delay.to_i scheduled_register_iso = Time.at(scheduled_register_epoch).utc.iso8601 registration_complete = false begin @running = true @started = true Lich.log( "info: SessionLifecycle start scheduled " \ "pid=#{Process.pid} session=#{session_name.inspect} role=#{role.inspect} " \ "started_epoch=#{started_epoch} started_iso=#{started_iso} " \ "register_at_epoch=#{scheduled_register_epoch} register_at_iso=#{scheduled_register_iso} " \ "heartbeat_interval=#{heartbeat_interval}s" ) if Lich.respond_to?(:log) @heartbeat_thread = Thread.new do sleep registration_delay Thread.exit unless @running if game_context_ready? registration_complete = attempt_register( session_name: session_name, role: role, frontend: frontend, started_epoch: started_epoch, started_iso: started_iso, registration_delay: registration_delay ) else Lich.log( "info: SessionLifecycle deferred register postponed " \ "pid=#{Process.pid} session=#{session_name.inspect} role=#{role.inspect} " \ "reason=xmldata_game_unavailable attempt_epoch=#{Time.now.to_i}" ) if Lich.respond_to?(:log) end loop do sleep heartbeat_interval break unless @running game_code = resolve_game_code if !registration_complete && !game_code.nil? registration_complete = attempt_register( session_name: session_name, role: role, frontend: frontend, started_epoch: started_epoch, started_iso: started_iso, registration_delay: registration_delay ) end SessionsSettings.send(:heartbeat_admitted, pid: Process.pid, state: 'running', session_name: session_name, role: role, frontend: frontend, game_code: game_code) end rescue StandardError => e Lich.log("warning: SessionLifecycle heartbeat failed: #{e.class}: #{e.}") if Lich.respond_to?(:log) end rescue StandardError @running = false @started = false @feature_enabled = false @heartbeat_thread = nil raise end end true rescue StandardError => e Lich.log("warning: SessionLifecycle start failed: #{e.class}: #{e.}") if Lich.respond_to?(:log) false end |
.stop ⇒ Boolean
Stops lifecycle tracking and unregisters the current process.
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 |
# File 'documented/common/session_lifecycle.rb', line 154 def self.stop heartbeat_thread = nil was_enabled = false @mutex.synchronize do return false unless @started was_enabled = @feature_enabled @running = false heartbeat_thread = @heartbeat_thread @heartbeat_thread = nil end # Cooperative shutdown first: allow the heartbeat loop to observe # @running=false and exit naturally before using hard-kill fallback. heartbeat_thread&.join(0.5) heartbeat_thread&.kill if heartbeat_thread&.alive? @mutex.synchronize do @heartbeat_thread = nil SessionsSettings.send(:unregister_session_admitted, pid: Process.pid) if was_enabled @started = false @feature_enabled = false end true rescue StandardError => e Lich.log("warning: SessionLifecycle stop failed: #{e.class}: #{e.}") if Lich.respond_to?(:log) false end |