diff --git a/src/amoc_controller.erl b/src/amoc_controller.erl index 3b300467..29b724d9 100644 --- a/src/amoc_controller.erl +++ b/src/amoc_controller.erl @@ -298,11 +298,12 @@ handle_start_user(#state{create_users = [], tref = TRef} = State) -> State#state{tref = maybe_stop_timer(TRef)}. -spec handle_start_all_users(state()) -> state(). -handle_start_all_users(#state{create_users = []} = State) -> - handle_start_user(State); -handle_start_all_users(State) -> - NewState = handle_start_user(State), - handle_start_all_users(NewState). +handle_start_all_users(#state{create_users = AllUsers, + scenario = Scenario, + scenario_state = ScenarioState, + tref = TRef} = State) -> + amoc_users_sup_sup:start_children(Scenario, AllUsers, ScenarioState), + State#state{create_users = [], tref = maybe_stop_timer(TRef)}. %% ------------------------------------------------------------------ %% helpers diff --git a/src/users/amoc_users_sup.erl b/src/users/amoc_users_sup.erl index 5adae83a..9f5d67be 100644 --- a/src/users/amoc_users_sup.erl +++ b/src/users/amoc_users_sup.erl @@ -49,10 +49,18 @@ handle_cast({start_child, Scenario, Id, ScenarioState}, #state{tid = Tid} = Stat case amoc_user:start_link(Scenario, Id, ScenarioState) of {ok, Pid} -> handle_up_user(Tid, Pid, Id), - {noreply, State#state{}}; + {noreply, State}; _ -> {noreply, State} end; +handle_cast({start_children, Scenario, Ids, ScenarioState}, #state{tid = Tid} = State) -> + [ case amoc_user:start_link(Scenario, Id, ScenarioState) of + {ok, Pid} -> + handle_up_user(Tid, Pid, Id); + _ -> + ok + end || Id <- Ids], + {noreply, State}; handle_cast({stop_child, ForceRemove}, #state{tid = Tid} = State) -> Pids = case ets:match_object(Tid, '$1', 1) of {[{Pid, _Id}], _} -> [Pid]; diff --git a/src/users/amoc_users_sup_sup.erl b/src/users/amoc_users_sup_sup.erl index 58158916..ba0808a8 100644 --- a/src/users/amoc_users_sup_sup.erl +++ b/src/users/amoc_users_sup_sup.erl @@ -9,7 +9,7 @@ %% API -export([init_storage/0, incr_no_of_users/0, decr_no_of_users/0, count_no_of_users/0, - start_child/3, stop_children/2, terminate_all_children/0]). + start_child/3, start_children/3, stop_children/2, terminate_all_children/0]). -record(storage, { user_count :: atomics:atomics_ref(), @@ -66,6 +66,17 @@ start_child(Scenario, Id, ScenarioState) -> Sup = get_sup_for_user_id(Id), gen_server:cast(Sup, {start_child, Scenario, Id, ScenarioState}). +-spec start_children(amoc:scenario(), [amoc_scenario:user_id()], any()) -> ok. +start_children(Scenario, UserIds, ScenarioState) -> + KeyFun = fun(UserId) -> + get_sup_for_user_id(UserId) + end, + Assignments = maps:groups_from_list(KeyFun, UserIds), + CastFun = fun (Sup, Users) -> + gen_server:cast(Sup, {start_children, Scenario, Users, ScenarioState}) + end, + maps:foreach(CastFun, Assignments). + -spec stop_children(non_neg_integer(), boolean()) -> non_neg_integer(). stop_children(Count, Force) -> TotalCount = count_no_of_users(),