Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added remove_worker handler

  • Loading branch information...
commit 74192b48564daf1df1291e43fd92aa17a5af09ff 1 parent be8b5d0
@zsolt-erl authored
View
6 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
View
BIN  priv/logs/sasl/1
Binary file not shown
View
BIN  priv/logs/sasl/2
Binary file not shown
View
BIN  priv/logs/sasl/3
Binary file not shown
View
BIN  priv/logs/sasl/4
Binary file not shown
View
BIN  priv/logs/sasl/5
Binary file not shown
View
1  priv/logs/sasl/index
@@ -1 +0,0 @@
-
View
17 src/pool_manager.erl
@@ -8,9 +8,6 @@
%%% Created : 4 Mar 2012 by Zsolt Keszthelyi <zsolt@uniss>
%%%-------------------------------------------------------------------
-%%% TODO: remove_workers call
-
-
-module(pool_manager).
-behaviour(gen_server).
@@ -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}.
@@ -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)).
View
9 src/pool_sup.erl
@@ -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]} }.
View
9 src/worker.erl
@@ -55,6 +55,7 @@ start_link(ManagerPid) ->
%% @end
%%--------------------------------------------------------------------
init(ManagerPid) ->
+ process_flag(trap_exit, true),
ManagerPid ! {self(), worker_ready},
{ok, #state{}}.
@@ -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}.
%%--------------------------------------------------------------------
@@ -119,6 +125,7 @@ handle_info(_Info, State) ->
%% @end
%%--------------------------------------------------------------------
terminate(_Reason, _State) ->
+ ?d("~p terminating~n",[self()]),
ok.
%%--------------------------------------------------------------------
Please sign in to comment.
Something went wrong with that request. Please try again.