Module: Lich::InternalAPI::ActiveSessions::Lifecycle
- Defined in:
- documented/internal_api/active_sessions/lifecycle.rb
Constant Summary collapse
- HEARTBEAT_INTERVAL_SECONDS =
The interval in seconds for the heartbeat signal.
5
Class Method Summary collapse
-
.clear_listener ⇒ void
Clears the listener's connection details.
-
.current_payload ⇒ Hash
Retrieves the current session payload.
-
.resolve_role(argv:, detachable_client_port:) ⇒ String
Determines the role of the session based on command line arguments.
-
.resolve_session_name(argv:, account_character: nil) ⇒ String
Resolves the session name based on command line arguments or defaults.
-
.start(session_name:, role:, heartbeat_interval: HEARTBEAT_INTERVAL_SECONDS) ⇒ Boolean
Starts the session lifecycle with the given parameters.
-
.stop ⇒ Boolean
Stops the session lifecycle, cleaning up resources.
-
.update_listener(host:, port:, connected:) ⇒ void
Updates the listener's connection details.
Class Method Details
.clear_listener ⇒ void
This method returns an undefined value.
Clears the listener's connection details.
176 177 178 179 180 181 182 183 184 185 |
# File 'documented/internal_api/active_sessions/lifecycle.rb', line 176 def self.clear_listener return unless started? @mutex.synchronize do @listener_host = nil @listener_port = nil @listener_connected = false end upsert_current_session end |
.current_payload ⇒ Hash
Retrieves the current session payload.
190 191 192 |
# File 'documented/internal_api/active_sessions/lifecycle.rb', line 190 def self.current_payload @mutex.synchronize { build_current_payload } end |
.resolve_role(argv:, detachable_client_port:) ⇒ String
Determines the role of the session based on command line arguments.
48 49 50 51 52 53 |
# File 'documented/internal_api/active_sessions/lifecycle.rb', line 48 def self.resolve_role(argv:, detachable_client_port:) return 'headless' if argv.include?('--without-frontend') return 'detachable' unless detachable_client_port.nil? 'session' end |
.resolve_session_name(argv:, account_character: nil) ⇒ String
Resolves the session name based on command line arguments or defaults.
31 32 33 34 35 36 37 38 39 40 41 |
# File 'documented/internal_api/active_sessions/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: HEARTBEAT_INTERVAL_SECONDS) ⇒ Boolean
Starts the session lifecycle with the given parameters.
61 62 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 |
# File 'documented/internal_api/active_sessions/lifecycle.rb', line 61 def self.start(session_name:, role:, heartbeat_interval: HEARTBEAT_INTERVAL_SECONDS) feature_enabled = ActiveSessions.enabled? return false unless feature_enabled # Bootstrap once during lifecycle startup so the admitted-only # heartbeat/update path has a running service to talk to. ActiveSessions.ensure_service! thread = nil @mutex.synchronize do return false if @started @session_name = session_name @role = role @started_at = Time.now.to_i @feature_enabled = feature_enabled @running = true @started = true @lifecycle_generation += 1 end thread = Thread.new do loop do sleep heartbeat_interval break unless running? upsert_current_session end rescue StandardError => e Lich.log("warning: ActiveSessions heartbeat failed: #{e.class}: #{e.}") if Lich.respond_to?(:log) end @mutex.synchronize { @heartbeat_thread = thread if @started } upsert_current_session true rescue StandardError => e @mutex.synchronize do @running = false @started = false @heartbeat_thread = nil @session_name = nil @role = nil @listener_host = nil @listener_port = nil @listener_connected = false @started_at = nil @feature_enabled = false end thread.kill if thread.respond_to?(:alive?) && thread.alive? Lich.log("warning: ActiveSessions lifecycle start failed: #{e.class}: #{e.}") if Lich.respond_to?(:log) false end |
.stop ⇒ Boolean
Stops the session lifecycle, cleaning up resources.
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 150 151 152 153 154 |
# File 'documented/internal_api/active_sessions/lifecycle.rb', line 118 def self.stop thread = nil lifecycle_active = false @mutex.synchronize do lifecycle_active = @started || !@heartbeat_thread.nil? || @running return false unless lifecycle_active @running = false @started = false @lifecycle_generation += 1 thread = @heartbeat_thread @heartbeat_thread = nil end thread&.join(0.5) thread&.kill if thread&.alive? if feature_enabled? @registration_mutex.synchronize do ActiveSessions.send(:unregister_session_admitted, pid: Process.pid) ActiveSessions.cleanup_discovery_if_last_session! end end @mutex.synchronize do @session_name = nil @role = nil @listener_host = nil @listener_port = nil @listener_connected = false @started_at = nil @feature_enabled = false end true rescue StandardError => e Lich.log("warning: ActiveSessions lifecycle stop failed: #{e.class}: #{e.}") if Lich.respond_to?(:log) false end |
.update_listener(host:, port:, connected:) ⇒ void
This method returns an undefined value.
Updates the listener's connection details.
162 163 164 165 166 167 168 169 170 171 |
# File 'documented/internal_api/active_sessions/lifecycle.rb', line 162 def self.update_listener(host:, port:, connected:) return unless started? @mutex.synchronize do @listener_host = host @listener_port = port @listener_connected = connected end upsert_current_session end |