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.

Examples:

HEARTBEAT_INTERVAL_SECONDS # => 5
5

Class Method Summary collapse

Class Method Details

.clear_listenervoid

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_payloadHash

Retrieves the current session payload.

Returns:

  • (Hash)

    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.

Parameters:

  • argv (Array<String>)

    command line arguments

  • detachable_client_port (Integer, nil)

    port for detachable client

Returns:

  • (String)

    the resolved role



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.

Parameters:

  • argv (Array<String>)

    command line arguments

  • account_character (String, nil) (defaults to: nil)

    optional account character name

Returns:

  • (String)

    the resolved session name



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 ( = argv.index('--login')) && argv[ + 1]
    argv[ + 1].capitalize
  elsif  && !.to_s.empty?
    
  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.

Parameters:

  • session_name (String)

    the name of the session

  • role (String)

    the role of the session

  • heartbeat_interval (Integer) (defaults to: HEARTBEAT_INTERVAL_SECONDS)

    the interval for heartbeat signals (default: HEARTBEAT_INTERVAL_SECONDS)

Returns:

  • (Boolean)

    true if the session started successfully, false otherwise



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.message}") 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.message}") if Lich.respond_to?(:log)
  false
end

.stopBoolean

Stops the session lifecycle, cleaning up resources.

Returns:

  • (Boolean)

    true if the session stopped successfully, false otherwise



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.message}") 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.

Parameters:

  • host (String)

    the host of the listener

  • port (Integer)

    the port of the listener

  • connected (Boolean)

    whether the listener is connected



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