Skip to content

Commit

Permalink
added remove_worker handler
Browse files Browse the repository at this point in the history
  • Loading branch information
zsolt-erl committed Mar 12, 2012
1 parent be8b5d0 commit 74192b4
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 6 deletions.
6 changes: 6 additions & 0 deletions README
@@ -1,3 +1,9 @@
Beginnings of a worker pool manager app.
This is not a general worker pool. It's made to fulfill specific requirements.

TODO:
- worker needs to finish all jobs before shutting down (empty out message queue),
rewrite worker as a gen_fsm?
- worker behavior
- pass pool state to worker at init
- integrate with gen_bunny
Binary file removed priv/logs/sasl/1
Binary file not shown.
Binary file removed priv/logs/sasl/2
Binary file not shown.
Binary file removed priv/logs/sasl/3
Binary file not shown.
Binary file removed priv/logs/sasl/4
Binary file not shown.
Binary file removed priv/logs/sasl/5
Binary file not shown.
1 change: 0 additions & 1 deletion priv/logs/sasl/index

This file was deleted.

17 changes: 14 additions & 3 deletions src/pool_manager.erl
Expand Up @@ -8,9 +8,6 @@
%%% Created : 4 Mar 2012 by Zsolt Keszthelyi <zsolt@uniss>
%%%-------------------------------------------------------------------

%%% TODO: remove_workers call


-module(pool_manager).

-behaviour(gen_server).
Expand Down Expand Up @@ -136,6 +133,12 @@ handle_call( {add_workers, Num}, _From, #state{poolspec = PoolSpec} = State) ->
Reply = ok,
{reply, Reply, State};

handle_call( {remove_workers, Num}, _From, #state{poolspec = PoolSpec} = State) ->
remove_workers_internal(PoolSpec, State#state.workers, Num),
Reply = ok,
{reply, Reply, State};


handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
Expand Down Expand Up @@ -222,3 +225,11 @@ add_workers_internal(PoolSpec, Num) ->
{Pid, 0}
end,
lists:seq(1, Num)).

remove_workers_internal(PoolSpec, ExistingWorkers, Num) ->
lists:foldl(fun
(_, []) -> [];
(_, [{Pid, _JobNum}|Rest]) -> Pid ! shutdown, Rest
end,
ExistingWorkers,
lists:seq(1, Num)).
9 changes: 8 additions & 1 deletion src/pool_sup.erl
Expand Up @@ -28,10 +28,17 @@ start_link(PoolSpec) ->
%% Supervisor callbacks
%% ===================================================================

%% supervisor2 doesn't make sense, if I use 'transient' the child with normal exit will not be respawned (which is the expected behavior), however with {transient, 30} it gets respawned when it shouldn't

init(Args = [PoolSpec]) ->
register(list_to_atom(PoolSpec#poolspec.poolname++"_sup"),self()),
?dv(Args),
Callback = PoolSpec#poolspec.worker_callback,
WorkerSpec = {Callback, {Callback, start_link, []}, {permanent, 30}, 5000, worker, [Callback]},

WorkerSpec = {Callback, {Callback, start_link, []}, transient, 5000, worker, [Callback]},
{ok, { {simple_one_for_one_terminate, 5, 10}, [WorkerSpec]} }.



%% WorkerSpec = {Callback, {Callback, start_link, []}, {transient, 30}, 5000, worker, [Callback]},
%% {ok, { {simple_one_for_one_terminate, 5, 10}, [WorkerSpec]} }.
9 changes: 8 additions & 1 deletion src/worker.erl
Expand Up @@ -55,6 +55,7 @@ start_link(ManagerPid) ->
%% @end
%%--------------------------------------------------------------------
init(ManagerPid) ->
process_flag(trap_exit, true),
ManagerPid ! {self(), worker_ready},
{ok, #state{}}.

Expand Down Expand Up @@ -104,7 +105,12 @@ handle_cast(_Msg, State) ->
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_info(_Info, State) ->
handle_info(shutdown, State)->
?d("~p is shutting down~n", [self()]),
{stop, normal, State};

handle_info(Info, State) ->
?d("~p got info:~p~n", [self(), Info]),
{noreply, State}.

%%--------------------------------------------------------------------
Expand All @@ -119,6 +125,7 @@ handle_info(_Info, State) ->
%% @end
%%--------------------------------------------------------------------
terminate(_Reason, _State) ->
?d("~p terminating~n",[self()]),
ok.

%%--------------------------------------------------------------------
Expand Down

0 comments on commit 74192b4

Please sign in to comment.