Module: Lich::InternalAPI::ActiveSessions

Defined in:
documented/internal_api/active_sessions.rb,
documented/internal_api/active_sessions/client.rb,
documented/internal_api/active_sessions/server.rb,
documented/internal_api/active_sessions/registry.rb,
documented/internal_api/active_sessions/lifecycle.rb

Overview

Cross-process local service that tracks currently active Lich sessions.

This module owns the process-local server instance for the active Cross-process local service that tracks currently active Lich sessions.

This module owns the process-local server instance for the active sessions and provides methods to manage them.

See Also:

Defined Under Namespace

Modules: Lifecycle Classes: Client, Registry, Server

Constant Summary collapse

FEATURE_FLAG =
:active_sessions_api
DEFAULT_HOST =
'127.0.0.1'
DEFAULT_PORT =
42_857
DISCOVERY_FILENAME =
'lich-active-sessions.json'

Class Method Summary collapse

Class Method Details

.cleanup_discovery_if_last_session!void

This method returns an undefined value.

Cleans up the discovery file if this is the last active session.



274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'documented/internal_api/active_sessions.rb', line 274

def self.cleanup_discovery_if_last_session!
  discovery = load_discovery
  return unless discovery[:owner_pid].to_i == Process.pid

  current_snapshot = query_snapshot
  return if current_snapshot[:error]
  return unless current_snapshot[:source] == 'ActiveSessionsAPI'
  return unless current_snapshot[:total].to_i.zero?

  stop_service!
rescue StandardError
  nil
end

.enabled?Boolean

Checks if the active sessions feature is enabled.

Returns:

  • (Boolean)

    true if the feature is enabled, false otherwise.



43
44
45
46
47
48
49
50
# File 'documented/internal_api/active_sessions.rb', line 43

def self.enabled?
  return false unless defined?(Lich::Common::FeatureFlags)

  Lich::Common::FeatureFlags.enabled?(FEATURE_FLAG)
rescue StandardError => e
  Lich.log("warning: ActiveSessions feature flag check failed: #{e.class}: #{e.message}") if Lich.respond_to?(:log)
  false
end

.ensure_service!Boolean

Ensures that the active sessions service is running.

Returns:

  • (Boolean)

    true if the service is available, false otherwise.



54
55
56
57
58
# File 'documented/internal_api/active_sessions.rb', line 54

def self.ensure_service!
  return false unless enabled?

  ensure_service_internal!(allow_bootstrap: true)
end

.query_snapshotObject



147
148
149
150
151
152
153
154
155
# File 'documented/internal_api/active_sessions.rb', line 147

def self.query_snapshot
  response = service_client&.snapshot
  return fallback_snapshot(error: 'active sessions service unavailable') unless response
  return fallback_snapshot(error: response[:error]) unless response[:ok]

  response[:payload]
rescue StandardError => e
  fallback_snapshot(error: e.message)
end

.register_session(payload) ⇒ Boolean

Registers a new session with the given payload.

Parameters:

  • payload (Hash)

    the session data to register.

Returns:

  • (Boolean)

    true if the session was successfully registered, false otherwise.



102
103
104
105
106
107
# File 'documented/internal_api/active_sessions.rb', line 102

def self.register_session(payload)
  return false unless enabled?
  return false unless ensure_service!

  service_client&.upsert(payload)&.fetch(:ok, false) || false
end

.service_infoHash

Provides information about the active sessions service.

Returns:

  • (Hash)

    a hash containing service information such as owner PID and availability.



159
160
161
162
163
164
165
166
167
# File 'documented/internal_api/active_sessions.rb', line 159

def self.service_info
  discovery = load_discovery
  {
    source: 'ActiveSessionsAPI',
    owner_pid: discovery[:owner_pid],
    updated_at: discovery[:updated_at],
    service_available: service_available?
  }.compact
end

.snapshotHash

Retrieves a snapshot of the current active sessions.

Returns:

  • (Hash)

    a snapshot of active sessions or a fallback snapshot in case of failure.



135
136
137
138
139
140
141
142
143
144
145
# File 'documented/internal_api/active_sessions.rb', line 135

def self.snapshot
  return fallback_snapshot unless enabled?
  return fallback_snapshot unless ensure_service!

  response = service_client&.snapshot || fallback_snapshot(error: 'active sessions service unavailable')
  return fallback_snapshot(error: response[:error]) unless response[:ok]

  response[:payload]
rescue StandardError => e
  fallback_snapshot(error: e.message)
end

.stop_service!void

This method returns an undefined value.

Stops the active sessions service and cleans up resources.



171
172
173
174
175
176
177
178
179
180
181
182
# File 'documented/internal_api/active_sessions.rb', line 171

def self.stop_service!
  @mutex.synchronize do
    @server&.stop
    @server = nil
    @registry = nil
  end
  @service_client_mutex.synchronize do
    @service_client = nil
    @service_client_token = nil
  end
  delete_discovery_if_owned
end

.unregister_session(pid:) ⇒ Boolean

Unregisters a session by its process ID.

Parameters:

  • pid (Integer)

    the process ID of the session to unregister.

Returns:

  • (Boolean)

    true if the session was successfully unregistered, false otherwise.



119
120
121
122
123
124
# File 'documented/internal_api/active_sessions.rb', line 119

def self.unregister_session(pid:)
  return false unless enabled?
  return false unless ensure_service!

  service_client&.remove(pid)&.fetch(:ok, false) || false
end