classy_lib ¶Misc. utility functions.
-type wakeup_timer() :: undefined |
{integer(), reference()}.
-type unix_time_s() :: integer().
-type multicall_result(Result) :: #{multicall_target()
=> {ok, Result} | multicall_error()}.
-type multicall_error() :: {error, site_is_down} |
{error, {erpc, _}} |
wrapped_exception().
-type wrapped_exception() :: {error, {throw, _Reason}} |
{error, {error, _Reason, _Stacktrace :: list()}} |
{error, {exit, _Reason}}.
-type multicall_args() :: #{multicall_target() =>
{module(), atom(), list()}}.
-type multicall_target() :: classy:site() |
{classy:site(), _Token}.
-type mfargs() :: {module(), atom(), list()}.
-spec map_deep_insert(list(), term(), map()) -> map().
-spec is_normal_exit(_) -> boolean().
-spec ensure_list(binary() | string()) -> string().
If input is a binary, convert it to a list. Keep input list as is.
-spec sync_stop_proc(pid() | atom(), _ExitReason,
timeout()) -> ok | {error, timeout}.
Send exit signal Reason to a process and wait for the shutdown
-spec cancel_wakeup(wakeup_timer()) -> undefined.
Cancel a timer created by classy_lib:wakeup_after/3. This function is idempotent.
-spec wakeup_after(term(), integer(),
wakeup_timer()) -> wakeup_timer().
Set up a wakeup timer that sends message Msg to the calling process.
If the timer was previously set up to fire at a later time, this function resets it to the earlier time.
-spec time_s() -> unix_time_s().
Return Unix time in seconds.
Adjust a local timestamp Val to the remote nodes’s clock,
given the remote’s “current” time RemoteTimeS at the time of the call.
Note: it only makes sense to use this function in a call with a finite timeout. Then the error will be limited by the roundtrip timeout.
Return value of n_sites environment variable (with default)
Return value rpc_timeout environment variable (with default)
-spec fold_per_cluster(Fun, Acc,
classy:cluster_info()) -> #{classy:cluster_id() =>
Acc} when Fun ::
fun((node(),
classy:info(),
Acc) -> Acc).
Perform a fold over the result of classy:info/1 with a separate accumulator per cluster.
InitialAcc parameter is used as the initial value of the accumulator for each cluster.
Sites that are not part of any cluster or don’t have a site ID are ignored.
-spec sites_to_nodes([classy:site()]) -> {[node()],
_BadSites :: [classy:site()]}.
Translate site IDs to node names of connected nodes.
Return list of unreachable nodes in the second element of the tuple.
-spec n_connected_peers(#{classy:site() =>
classy:peer_info()}) -> non_neg_integer().
-spec multicall(multicall_args(),
timeout()) -> multicall_result(term()).
Call a function on multiple sites in parallel.
Arguments:
SitesWithArgs:
a map from RPC target to {Module, Function, Args} tuple.
Multicall target could be a site ID that is automatically translated to Erlang node, or a tuple containing site ID and an arbitrary term. The latter form allows to make multiple requests towards the same site. Each multicall target can call a different function.
Timeout:
abandon waiting for replies after the specified timeout.
Return value:
A map from RPC target to {ok, Return} tuple if the call was successful
or an error tuple t:classy_lib:multicall_error/0.
Example:
classy_lib:multicall(
#{S => {erlang, node, []} || S <- [S1, S2, SBad]},
5_000)
returns
#{ S1 => {ok, 'node1@localhost'}
, S2 => {ok, 'node2@localhost'}
, SBad => {error, site_is_down}
}
-spec
multicall(multicall_args()) -> multicall_result(term()).
See classy_lib:multicall/2. Uses the default timeout.
-spec multicast(multicall_args()) -> ok.
Call functions on multiple sites similarly to classy_lib:multicall/2 without waiting for the results.
-spec safe_apply(module(), atom(), list()) -> {ok,
term()} |
wrapped_exception().
Apply a function while catching all exceptions and returning them as a term.
-spec safe_apply(mfargs()) -> {ok, term()} |
wrapped_exception().