Jido.Signal.Dispatch.PidAdapter (Jido v1.1.0-rc.2)

View Source

An adapter for dispatching signals directly to Erlang processes using PIDs.

This adapter implements the Jido.Signal.Dispatch.Adapter behaviour and provides functionality to send signals to specific processes either synchronously or asynchronously.

Configuration Options

  • :target - (required) The PID of the destination process
  • :delivery_mode - (optional) Either :sync or :async, defaults to :async
  • :timeout - (optional) Timeout for synchronous delivery in milliseconds, defaults to 5000
  • :message_format - (optional) Function to format the signal before sending, defaults to wrapping in {:signal, signal}

Delivery Modes

  • :async - Uses send/2 to deliver the signal without waiting for a response
  • :sync - Uses GenServer.call/3 to deliver the signal and wait for a response

Examples

# Asynchronous delivery
config = {:pid, [
  target: destination_pid,
  delivery_mode: :async
]}

# Synchronous delivery with custom timeout
config = {:pid, [
  target: destination_pid,
  delivery_mode: :sync,
  timeout: 10_000
]}

# Custom message format
config = {:pid, [
  target: destination_pid,
  message_format: fn signal -> {:custom_signal, signal} end
]}

Error Handling

The adapter handles various error conditions:

  • :process_not_alive - The target process is not alive
  • :timeout - Synchronous delivery timed out
  • Other errors from the target process

Summary

Functions

Delivers a signal to the target process.

Validates the PID adapter configuration options.

Types

delivery_error()

@type delivery_error() :: :process_not_alive | :timeout | term()

delivery_mode()

@type delivery_mode() :: :sync | :async

delivery_opts()

@type delivery_opts() :: [
  target: delivery_target(),
  delivery_mode: delivery_mode(),
  timeout: timeout(),
  message_format: message_format()
]

delivery_target()

@type delivery_target() :: pid()

message_format()

@type message_format() :: (Jido.Signal.t() -> term())

Functions

deliver(signal, opts)

@spec deliver(Jido.Signal.t(), delivery_opts()) ::
  :ok | {:ok, term()} | {:error, delivery_error()}

Delivers a signal to the target process.

Parameters

  • signal - The signal to deliver
  • opts - Validated options from validate_opts/1

Options

  • :target - (required) Target PID to deliver to
  • :delivery_mode - (required) Either :sync or :async
  • :timeout - (optional) Timeout for sync delivery, defaults to 5000ms
  • :message_format - (optional) Function to format the signal

Returns

  • :ok - Signal delivered successfully (async mode)
  • {:ok, term()} - Signal delivered and response received (sync mode)
  • {:error, reason} - Delivery failed

validate_opts(opts)

@spec validate_opts(Keyword.t()) :: {:ok, Keyword.t()} | {:error, term()}

Validates the PID adapter configuration options.

Parameters

  • opts - Keyword list of options to validate

Options

  • :target - Must be a valid PID
  • :delivery_mode - Must be either :sync or :async

Returns

  • {:ok, validated_opts} - Options are valid
  • {:error, :invalid_target} - Target is not a valid PID
  • {:error, :invalid_delivery_mode} - Delivery mode is invalid