Skip to content

Commit

Permalink
start the event manager only if enabled_callbacks is passed
Browse files Browse the repository at this point in the history
  • Loading branch information
michalwski committed Oct 22, 2018
1 parent 37c7c38 commit 0e4b456
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ To start a new worker pool, you can either use `wpool:start_pool` (if you want t
* **strategy**: Not the worker selection strategy (discussed below) but the supervisor flags to be used in the supervisor over the individual workers (`wpool_process_sup`). Defaults to `{one_for_one, 5, 60}`
* **pool_sup_intensity** and **pool_sup_period**: The intensity and period for the supervisor that manages the worker pool system (`wpool_pool`). The strategy of this supervisor must be `one_for_all` but the intensity and period may be changed from their defaults of `5` and `60`.
* **queue_type**: Order in which requests will be stored and handled by workers. This option can take values `lifo` or `fifo`. Defaults to `fifo`.
* **callbacks**: List of callback modules implementing `wpool_process_callbacks` to be called on certain worker events.
* **enable_callbacks**: A boolean value determining if `event_manager` should be started for callback modules.
If `callbacks` were specified, there is no need to set this option.
* **callbacks**: Initial list of callback modules implementing `wpool_process_callbacks` to be called on certain worker events.
This options will only work if the `enable_callbacks` is set to **true**. Callbacks can be added and removed later by `wpool_pool:add_callback_module/2` and `wpool_pool:remove_callback_module/2`.

#### Using the Workers
Since the workers are `gen_server`s, messages can be `call`ed or `cast`ed to them. To do that you can use `wpool:call` and `wpool:cast` as you would use the equivalent functions on `gen_server`.
Expand Down
14 changes: 5 additions & 9 deletions src/wpool_pool.erl
Original file line number Diff line number Diff line change
Expand Up @@ -514,14 +514,10 @@ set_random_fun() ->
end,
application:set_env(worker_pool, random_fun, RndFun).

maybe_event_manager(Options, Spec) ->
InitialCallbacks = proplists:get_value(callbacks, Options, undefined),
maybe_event_manager(Options, Item) ->
EnableEventManager = proplists:get_value(enable_callbacks, Options, false),
case {EnableEventManager, InitialCallbacks} of
{true, _} ->
[Spec];
{_, AList} when is_list(AList) ->
[Spec];
_ ->
[]
case EnableEventManager of
true ->
[Item];
_ -> []
end.
3 changes: 3 additions & 0 deletions test/wpool_process_callbacks_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ complete_callback_passed_when_starting_pool(_Config) ->
meck:expect(callbacks, handle_worker_creation, fun(_AWorkerName) -> ok end),
meck:expect(callbacks, handle_worker_death, fun(_AWorkerName, _Reason) -> ok end),
{ok, _Pid} = wpool:start_pool(Pool, [{workers, WorkersCount},
{enable_callbacks, true},
{worker, {crashy_server, []}},
{callbacks, [callbacks]}]),

Expand All @@ -64,6 +65,7 @@ partial_callback_passed_when_starting_pool(_Config) ->
meck:expect(callbacks, handle_worker_creation, fun(_AWorkerName) -> ok end),
meck:expect(callbacks, handle_worker_death, fun(_AWorkerName, _Reason) -> ok end),
{ok, _Pid} = wpool:start_pool(Pool, [{workers, WorkersCount},
{enable_callbacks, true},
{callbacks, [callbacks]}]),
WorkersCount = ktn_task:wait_for(function_calls(callbacks, handle_worker_creation,
['_']), WorkersCount),
Expand Down Expand Up @@ -120,6 +122,7 @@ crashing_callback_does_not_affect_others(_Config) ->
error(not_going_to_work) end),
{ok, _Pid} = wpool:start_pool(Pool, [{workers, WorkersCount},
{worker, {crashy_server, []}},
{enable_callbacks, true},
{callbacks, [callbacks, callbacks2]}]),

WorkersCount = ktn_task:wait_for(function_calls(callbacks, handle_worker_creation,
Expand Down

0 comments on commit 0e4b456

Please sign in to comment.