Class: Lich::InternalAPI::ActiveSessions::Client
- Inherits:
-
Object
- Object
- Lich::InternalAPI::ActiveSessions::Client
- Defined in:
- documented/internal_api/active_sessions/client.rb
Overview
Client for managing active sessions with the Lich internal API.
This class handles communication with the server, including sending requests and receiving responses.
Constant Summary collapse
- READ_TIMEOUT =
1
Instance Method Summary collapse
-
#initialize(host:, port:, auth_token:, socket_factory: nil) ⇒ void
constructor
Initializes a new Client instance.
-
#ping ⇒ Boolean
Sends a ping request to the server to check connectivity.
-
#remove(pid) ⇒ Hash
Sends a remove request to the server for the specified process ID.
-
#request(command, payload = {}) ⇒ Hash
Sends a request to the server with the specified command and payload.
-
#snapshot ⇒ Hash
Sends a snapshot request to the server.
-
#upsert(payload) ⇒ Hash
Sends an upsert request to the server with the provided payload.
Constructor Details
#initialize(host:, port:, auth_token:, socket_factory: nil) ⇒ void
Initializes a new Client instance.
23 24 25 26 27 28 |
# File 'documented/internal_api/active_sessions/client.rb', line 23 def initialize(host:, port:, auth_token:, socket_factory: nil) @host = host @port = port @auth_token = auth_token @socket_factory = socket_factory || ->(connect_host, connect_port) { TCPSocket.new(connect_host, connect_port) } end |
Instance Method Details
#ping ⇒ Boolean
Sends a ping request to the server to check connectivity.
56 57 58 |
# File 'documented/internal_api/active_sessions/client.rb', line 56 def ping request('ping').fetch(:ok, false) end |
#remove(pid) ⇒ Hash
Sends a remove request to the server for the specified process ID.
70 71 72 |
# File 'documented/internal_api/active_sessions/client.rb', line 70 def remove(pid) request('remove', pid: pid) end |
#request(command, payload = {}) ⇒ Hash
Sends a request to the server with the specified command and payload.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'documented/internal_api/active_sessions/client.rb', line 35 def request(command, payload = {}) socket = @socket_factory.call(@host, @port) socket.write(JSON.dump(command: command, auth: @auth_token, payload: payload) + "\n") raw = read_response(socket) return { ok: false, error: 'read timeout' } unless raw response = JSON.parse(raw.to_s, symbolize_names: true) return { ok: false, error: 'invalid response type' } unless response.is_a?(Hash) response rescue StandardError => e { ok: false, error: e. } ensure socket&.close rescue nil end |
#snapshot ⇒ Hash
Sends a snapshot request to the server.
76 77 78 |
# File 'documented/internal_api/active_sessions/client.rb', line 76 def snapshot request('snapshot') end |
#upsert(payload) ⇒ Hash
Sends an upsert request to the server with the provided payload.
63 64 65 |
# File 'documented/internal_api/active_sessions/client.rb', line 63 def upsert(payload) request('upsert', payload) end |