SuperWorker.Supervisor.ApiHelper (SuperWorker v0.3.6)

View Source

Utility functions for SuperWorker.Supervisor.

This module provides common utility functions used across the supervisor implementation, including:

  • API call helpers

Summary

Functions

Waits for an API response matching the given reference.

Sends an API response to the caller.

Makes a synchronous API call to a target process.

Makes an asynchronous API call (fire-and-forget).

Types

api_result()

@type api_result() :: {:ok, any()} | {:error, any()}

timeout_ms()

@type timeout_ms() :: non_neg_integer() | :infinity

Functions

api_receiver(ref, timeout)

@spec api_receiver(reference(), timeout_ms()) :: any()

Waits for an API response matching the given reference.

Blocks until a message with the matching reference is received or the timeout expires.

Examples

# ... send message with ref ...
api_receiver(ref, 5000)

api_response(message, result)

@spec api_response(SuperWorker.Supervisor.Message.t(), any()) :: any()

Sends an API response to the caller.

Examples

def handle_api({from, ref}, params) do
  result = do_work(params)
  api_response({from, ref}, result)
end

call_api(target, api, params, timeout)

@spec call_api(atom() | pid(), atom(), any(), timeout_ms()) :: any()

Makes a synchronous API call to a target process.

Sends a message in the format {api_name, {from, ref}, params} and waits for a response with the matching reference.

Parameters

  • target - The pid or registered name of the target process
  • api - The API function name (atom)
  • params - Parameters to pass to the API
  • timeout - Maximum time to wait for response in milliseconds

Examples

result = call_api(:my_supervisor, :get_worker, worker_id, 5000)

call_api_no_reply(target, api, params)

@spec call_api_no_reply(atom() | pid(), atom(), any()) :: reference()

Makes an asynchronous API call (fire-and-forget).

Sends a message but does not wait for a response.

Examples

call_api_no_reply(:my_supervisor, :notify, :data_updated)

internal_call_api_no_reply(target, api, params)

@spec internal_call_api_no_reply(atom() | pid(), atom(), any()) :: reference()

valid_type?(type)