Class: Beachcomber::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/beachcomber/client.rb

Overview

Client sends individual requests, opening a fresh socket connection for each call. For workloads that issue many queries per invocation, use #session to reuse a persistent connection.

Examples:

client = Beachcomber::Client.new
result = client.get('git.branch', path: '/repo')
puts result.data if result.hit?

Instance Method Summary collapse

Constructor Details

#initialize(socket_path: nil, timeout: DEFAULT_TIMEOUT) ⇒ Client

Returns a new instance of Client.

Parameters:

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

    explicit socket path; auto-discovered when nil

  • timeout (Numeric) (defaults to: DEFAULT_TIMEOUT)

    connect/read timeout in seconds (default 0.1)



125
126
127
128
# File 'lib/beachcomber/client.rb', line 125

def initialize(socket_path: nil, timeout: DEFAULT_TIMEOUT)
  @socket_path = socket_path || Discovery.socket_path
  @timeout     = timeout
end

Instance Method Details

#get(key, path: nil) ⇒ Result

Reads a cached value.

Parameters:

  • key (String)

    e.g. “git.branch” or “git”

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

    optional working-directory context

Returns:

Raises:



137
138
139
140
141
# File 'lib/beachcomber/client.rb', line 137

def get(key, path: nil)
  req = { op: 'get', key: key }
  req[:path] = path if path
  roundtrip(req)
end

#listResult

Lists available providers registered with the daemon.

Returns:



159
160
161
# File 'lib/beachcomber/client.rb', line 159

def list
  roundtrip({ op: 'list' })
end

#poke(key, path: nil) ⇒ Object

Forces the daemon to recompute a provider/key.

Parameters:

  • key (String)
  • path (String, nil) (defaults to: nil)

Raises:



149
150
151
152
153
154
# File 'lib/beachcomber/client.rb', line 149

def poke(key, path: nil)
  req = { op: 'poke', key: key }
  req[:path] = path if path
  roundtrip(req)
  nil
end

#session {|Session| ... } ⇒ Object

Opens a persistent session and yields it to the block. The connection is closed automatically when the block returns (even on exception).

Yields:

Returns:

  • the block’s return value



175
176
177
178
179
180
181
# File 'lib/beachcomber/client.rb', line 175

def session
  sock    = dial
  session = Session.new(sock, @timeout)
  yield session
ensure
  session&.close
end

#statusResult

Returns scheduler and cache status from the daemon.

Returns:



166
167
168
# File 'lib/beachcomber/client.rb', line 166

def status
  roundtrip({ op: 'status' })
end