<commit_before>%% @author Couchbase <<EMAIL>>
%% @copyright 2013 Couchbase, Inc
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%      http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
-module(ns_child_ports_sup).

-behavior(supervisor).

-export([start_link/0, set_dynamic_children/1,
         send_command/2,
         create_ns_server_supervisor_spec/0]).

-export([init/1, launch_port/1, terminate_port/1,
         restart_port/1, restart_port_by_name/1,
         current_ports/0, find_port/1]).

-include("ns_common.hrl").

start_link() ->
    supervisor:start_link({local, ?MODULE}, ?MODULE, []).

init([]) ->
    {ok, {{one_for_one, 100, 10}, []}}.

send_command(PortName, Command) ->
    try
        do_send_command(PortName, Command)
    catch T:E ->
            ?log_error("Failed to send command ~p to port ~p due to ~p:~p. Ignoring...~n~p",
                       [Command, PortName, T, E, erlang:get_stacktrace()]),
            {T, E}
    end.

find_port(PortName) ->
    Childs = supervisor:which_children(?MODULE),
    [Pid] = [Pid || {Id, Pid, _, _} <- Childs,
                    Pid =/= undefined,
                    element(1, Id) =:= PortName],
    Pid.

do_send_command(PortName, Command) ->
    Pid = find_port(PortName),
    Pid ! {send_to_port, Command}.

-spec set_dynamic_children([any()]) -> pid().
set_dynamic_children(NCAOs) ->
    PlainCurrPortParams = current_ports(),
    CurrPortParams = PlainCurrPortParams,
    OldPortParams = CurrPortParams -- NCAOs,
    NewPortParams = NCAOs -- CurrPortParams,

    PidBefore = erlang:whereis(?MODULE),

    lists:foreach(fun(NCAO) ->
                          terminate_port(NCAO)
                  end,
                  OldPortParams),
    lists:foreach(fun(NCAO) ->
                          launch_port(NCAO)
                  end,
                  NewPortParams),<commit_msg>MB-11286: don't exclude dead childs in set_dynamic_children<commit_after>%% @author Couchbase <<EMAIL>>
%% @copyright 2013 Couchbase, Inc
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%      http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
-module(ns_child_ports_sup).

-behavior(supervisor).

-export([start_link/0, set_dynamic_children/1,
         send_command/2,
         create_ns_server_supervisor_spec/0]).

-export([init/1, launch_port/1, terminate_port/1,
         restart_port/1, restart_port_by_name/1,
         current_ports/0, find_port/1]).

-include("ns_common.hrl").

start_link() ->
    supervisor:start_link({local, ?MODULE}, ?MODULE, []).

init([]) ->
    {ok, {{one_for_one, 100, 10}, []}}.

send_command(PortName, Command) ->
    try
        do_send_command(PortName, Command)
    catch T:E ->
            ?log_error("Failed to send command ~p to port ~p due to ~p:~p. Ignoring...~n~p",
                       [Command, PortName, T, E, erlang:get_stacktrace()]),
            {T, E}
    end.

find_port(PortName) ->
    Childs = supervisor:which_children(?MODULE),
    [Pid] = [Pid || {Id, Pid, _, _} <- Childs,
                    Pid =/= undefined,
                    element(1, Id) =:= PortName],
    Pid.

do_send_command(PortName, Command) ->
    Pid = find_port(PortName),
    Pid ! {send_to_port, Command}.

-spec set_dynamic_children([any()]) -> pid().
set_dynamic_children(NCAOs) ->
    CurrPortParams = [erlang:element(1, C) || C <- supervisor:which_children(?MODULE)],
    OldPortParams = CurrPortParams -- NCAOs,
    NewPortParams = NCAOs -- CurrPortParams,

    PidBefore = erlang:whereis(?MODULE),

    lists:foreach(fun(NCAO) ->
                          terminate_port(NCAO)
                  end,
                  OldPortParams),
    lists:foreach(fun(NCAO) ->
                          launch_port(NCAO)
                  end,
                  NewPortParams),
{'fraction_non_alphanumeric': 0.13043478260869565, 'fraction_numerical': 0.01199400299850075, 'mean_word_length': 2.7211155378486054, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3511431', 'n_tokens_mistral': 1520, 'n_tokens_neox': 1458, 'n_words': 368}