-
-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #153 from inaka/rel-3.1-worker-events-callback
Add support for callback functions around worker process events
- Loading branch information
Showing
7 changed files
with
347 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
-module(wpool_process_callbacks). | ||
|
||
-behaviour(gen_event). | ||
|
||
%% gen_event callbacks | ||
|
||
-export([ init/1 | ||
, handle_event/2 | ||
, handle_call/2 | ||
, handle_info/2 | ||
, code_change/3 | ||
, terminate/2 | ||
]). | ||
|
||
-export([ notify/3 | ||
, add_callback_module/2 | ||
, remove_callback_module/2 | ||
]). | ||
-type state() :: module(). | ||
|
||
-type event() :: handle_init_start | handle_worker_creation | handle_worker_death. | ||
|
||
-callback handle_init_start(wpool:name()) -> any(). | ||
-callback handle_worker_creation(wpool:name()) -> any(). | ||
-callback handle_worker_death(wpool:name(), term()) -> any(). | ||
|
||
-optional_callbacks([handle_init_start/1, handle_worker_creation/1, handle_worker_death/2]). | ||
|
||
-spec init(module()) -> {ok, state()}. | ||
init(Module) -> | ||
{ok, Module}. | ||
|
||
-spec handle_event({event(), [any()]}, state()) -> {ok, state()}. | ||
handle_event({Event, Args}, Module) -> | ||
call(Module, Event, Args), | ||
{ok, Module}; | ||
handle_event(_, State) -> | ||
{ok, State}. | ||
|
||
-spec handle_call(any(), state()) -> {ok, ok, state()}. | ||
handle_call(_, State) -> | ||
{ok, ok, State}. | ||
|
||
-spec handle_info(any(), state()) -> {ok, state()}. | ||
handle_info(_, State) -> | ||
{ok, State}. | ||
|
||
-spec code_change(any(), state(), any()) -> {ok, state()}. | ||
code_change(_OldVsn, State, _Extra) -> | ||
{ok, State}. | ||
|
||
-spec terminate(any(), state()) -> ok. | ||
terminate(_Reason, _State) -> | ||
ok. | ||
|
||
-spec notify(event(), [wpool:option()], [any()]) -> ok. | ||
notify(Event, Options, Args) -> | ||
case lists:keyfind(event_manager, 1, Options) of | ||
{event_manager, EventMgr} -> | ||
gen_event:notify(EventMgr, {Event, Args}); | ||
_ -> | ||
ok | ||
end. | ||
|
||
-spec add_callback_module(wpool:name(), module()) -> ok | {error, any()}. | ||
add_callback_module(EventManager, Module) -> | ||
case ensure_loaded(Module) of | ||
ok -> | ||
gen_event:add_handler(EventManager, | ||
{wpool_process_callbacks, Module}, Module); | ||
Other -> | ||
Other | ||
end. | ||
|
||
|
||
-spec remove_callback_module(wpool:name(), module()) -> ok | {error, any()}. | ||
remove_callback_module(EventManager, Module) -> | ||
gen_event:delete_handler(EventManager, {wpool_process_callbacks, Module}, Module). | ||
|
||
call(Module, Event, Args) -> | ||
try | ||
case erlang:function_exported(Module, Event, length(Args)) of | ||
true -> | ||
erlang:apply(Module, Event, Args); | ||
_ -> | ||
ok | ||
end | ||
catch | ||
E:R -> | ||
error_logger:warning_msg("Could not call callback module, error:~p, reason:~p", [E, R]) | ||
end. | ||
|
||
ensure_loaded(Module) -> | ||
case code:ensure_loaded(Module) of | ||
{module, Module} -> | ||
ok; | ||
{error, embedded} -> %% We are in embedded mode so the module was loaded if exists | ||
ok; | ||
Other -> | ||
Other | ||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.