Module: Lich::Common::SessionLifecycle

Defined in:
documented/common/session_lifecycle.rb

Constant Summary collapse

REGISTRATION_DELAY_SECONDS =
5

Class Method Summary collapse

Class Method Details

.attempt_register(session_name:, role:, frontend:, started_epoch:, started_iso:, registration_delay:) ⇒ Boolean

Attempts to register the session with the given parameters.

Parameters:

  • session_name (String)

    the name of the session

  • role (String)

    the role of the session

  • frontend (String)

    the frontend identifier

  • started_epoch (Integer)

    the epoch time when the session started

  • started_iso (String)

    the ISO 8601 formatted start time

  • registration_delay (Integer)

    delay before registration in seconds

Returns:

  • (Boolean)

    true if registration was successful, false otherwise



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'documented/common/session_lifecycle.rb', line 210

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.register_session(
      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.message} " \
      "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

Checks if the game context is ready for registration.

Returns:

  • (Boolean)

    true if the game context is ready, false otherwise



197
198
199
# File 'documented/common/session_lifecycle.rb', line 197

def self.game_context_ready?
  !resolve_game_code.nil?
end

.resolve_frontendString?

Resolves the frontend context for the session.

Returns:

  • (String, nil)

    the frontend identifier or nil if not set



179
180
181
182
183
# File 'documented/common/session_lifecycle.rb', line 179

def self.resolve_frontend
  return $frontend if defined?($frontend) && !$frontend.nil? && !$frontend.to_s.empty?

  nil
end

.resolve_game_codeString?

Resolves the game code from the XML data.

Returns:

  • (String, nil)

    the game code or nil if not available



188
189
190
191
192
# File 'documented/common/session_lifecycle.rb', line 188

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

Determines the role of the session based on command line arguments.

Parameters:

  • argv (Array<String>)

    command line arguments

  • detachable_client_port (Integer, nil)

    optional port for detachable clients

Returns:

  • (String)

    the resolved role



40
41
42
43
44
45
# File 'documented/common/session_lifecycle.rb', line 40

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 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



23
24
25
26
27
28
29
30
31
32
33
# File 'documented/common/session_lifecycle.rb', line 23

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: SessionsSettings::HEARTBEAT_INTERVAL_SECONDS, registration_delay: REGISTRATION_DELAY_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: SessionsSettings::HEARTBEAT_INTERVAL_SECONDS)

    interval for heartbeat in seconds

  • registration_delay (Integer) (defaults to: REGISTRATION_DELAY_SECONDS)

    delay before registration in seconds

Returns:

  • (Boolean)

    true if the session started successfully, false otherwise



54
55
56
57
58
59
60
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
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
# File 'documented/common/session_lifecycle.rb', line 54

def self.start(session_name:, role:, heartbeat_interval: SessionsSettings::HEARTBEAT_INTERVAL_SECONDS, registration_delay: REGISTRATION_DELAY_SECONDS)
  return false unless SessionsSettings.enabled?

  @mutex.synchronize do
    return false if @started

    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

          Lich.log(
            "debug: SessionLifecycle heartbeat tick " \
            "pid=#{Process.pid} session=#{session_name.inspect} role=#{role.inspect} " \
            "tick_epoch=#{Time.now.to_i}"
          ) if Lich.respond_to?(:log)
          SessionsSettings.heartbeat(
            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.message}") if Lich.respond_to?(:log)
      end
    rescue StandardError
      @running = false
      @started = false
      @heartbeat_thread = nil
      raise
    end
  end
  true
rescue StandardError => e
  Lich.log("warning: SessionLifecycle start failed: #{e.class}: #{e.message}") if Lich.respond_to?(:log)
  false
end

.stopBoolean

Stops the session lifecycle, unregistering the session.

Returns:

  • (Boolean)

    true if the session stopped successfully, false otherwise



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
# File 'documented/common/session_lifecycle.rb', line 148

def self.stop
  return false unless SessionsSettings.enabled?

  heartbeat_thread = nil
  @mutex.synchronize do
    return false unless @started

    @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.unregister_session(pid: Process.pid)
    @started = false
  end
  true
rescue StandardError => e
  Lich.log("warning: SessionLifecycle stop failed: #{e.class}: #{e.message}") if Lich.respond_to?(:log)
  false
end