<commit_before>-module(ccc_manage).
-export([connect/1, stop/0, enter/1, leave/1]).

connect(Node) ->
  pong = net_adm:ping(Node),
  ok   = global:sync(),
  Regs = [clusterccd, nodes_pool],
  Pids = [global:whereis_name(Reg) || Reg <- Regs],
  not lists:member(undefined, Pids).

trap_exit_RAII(Function) ->
  Prev = process_flag(trap_exit, true),
  R    = Function(),
  true = process_flag(trap_exit, Prev),
  R.

stop() ->
  F = fun() ->
    common_io:prefixed("send terminate signal"),
    global:send(clusterccd, {manage, self(), terminate})
    end,
  trap_exit_RAII(F).

manage(Function, Arg) when is_atom(Function) ->
  Self = self(),
  try global:send(nodes_pool, {manage, Self, Function, Arg}) of
    _ -> true
  catch
    exit:{badarg, {nodes_pool, {manage, Self, Function, Arg}}} -> false
  end.

enter(Node) -> manage(enter, Node).
leave(Node) -> manage(leave, Node).<commit_msg>to be generic<commit_after>-module(ccc_manage).
-export([connect/1, stop/0, enter/1, leave/1]).

connect(Node) ->
  pong = net_adm:ping(Node),
  ok   = global:sync(),
  Regs = [clusterccd, nodes_pool],
  Pids = [global:whereis_name(Reg) || Reg <- Regs],
  not lists:member(undefined, Pids).

trap_exit_RAII(Function) ->
  Prev = process_flag(trap_exit, true),
  R    = Function(),
  true = process_flag(trap_exit, Prev),
  R.


manage(To, Arg = [F | A]) when is_atom(F), is_list(A) ->
  Self = self(),
  M = list_to_tuple([manage, Self | Arg]),
  try global:send(To, M) of
    _ -> true
  catch
    exit:{badarg, {To, M}} -> false
  end.

stop() ->
  F = fun() ->
    common_io:prefixed("send terminate signal"),
    manage(clusterccd, [terminate])
    end,
  trap_exit_RAII(F).

enter(Node) -> manage(nodes_pool, [enter, Node]).
leave(Node) -> manage(nodes_pool, [leave, Node]).
{'fraction_non_alphanumeric': 0.17966101694915254, 'fraction_numerical': 0.004519774011299435, 'mean_word_length': 3.9058171745152355, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4767371', 'n_tokens_mistral': 689, 'n_tokens_neox': 668, 'n_words': 144}