RedisServerWrapper.Cli (redis_server_wrapper v0.7.2)

Copy Markdown View Source

Wrapper around the redis-cli binary for running commands against Redis instances.

Summary

Functions

Runs redis-cli --cluster create to form a cluster from the given node addresses.

Runs CLUSTER INFO and returns it as a parsed map.

Creates a new Cli struct.

Sends PING, returns true if PONG received.

Runs an arbitrary redis-cli command. Returns {:ok, output} or {:error, reason}.

Runs a command, raising on failure.

Runs SENTINEL MASTER <name> and returns parsed key-value map.

Sends SHUTDOWN NOSAVE. Best-effort, ignores errors.

Polls with PING until the server responds or timeout (ms) is reached.

Types

t()

@type t() :: %RedisServerWrapper.Cli{
  bin: String.t(),
  host: String.t(),
  password: String.t() | nil,
  port: non_neg_integer(),
  tls: boolean()
}

Functions

cluster_create(cli, node_addrs, replicas_per_master \\ 0)

@spec cluster_create(t(), [String.t()], non_neg_integer()) ::
  {:ok, String.t()} | {:error, String.t()}

Runs redis-cli --cluster create to form a cluster from the given node addresses.

cluster_info(cli)

@spec cluster_info(t()) :: {:ok, map()} | {:error, String.t()}

Runs CLUSTER INFO and returns it as a parsed map.

new(opts \\ [])

@spec new(keyword()) :: t()

Creates a new Cli struct.

Cli.new(port: 6400, password: "secret")

ping(cli)

@spec ping(t()) :: boolean()

Sends PING, returns true if PONG received.

run(cli, args)

@spec run(t(), [String.t()]) :: {:ok, String.t()} | {:error, String.t()}

Runs an arbitrary redis-cli command. Returns {:ok, output} or {:error, reason}.

run!(cli, args)

@spec run!(t(), [String.t()]) :: String.t()

Runs a command, raising on failure.

sentinel_master(cli, master_name)

@spec sentinel_master(t(), String.t()) :: {:ok, map()} | {:error, String.t()}

Runs SENTINEL MASTER <name> and returns parsed key-value map.

shutdown(cli)

@spec shutdown(t()) :: :ok

Sends SHUTDOWN NOSAVE. Best-effort, ignores errors.

wait_for_ready(cli, timeout_ms \\ 10000)

@spec wait_for_ready(t(), non_neg_integer()) ::
  :ok | {:error, :timeout} | {:error, {:unexpected_reply, String.t()}}

Polls with PING until the server responds or timeout (ms) is reached.

Returns {:error, {:unexpected_reply, output}} if the peer accepts the connection but responds with something other than PONG (or a transient LOADING/BUSY reply). This catches the common case of another service holding the port (e.g. macOS AirPlay on 7000) or a different redis-server with a mismatched password already bound to the port.