Skip to content

Commit

Permalink
Make optional callbacks optional
Browse files Browse the repository at this point in the history
  • Loading branch information
NelsonVides committed Aug 3, 2024
1 parent 0ec5ee5 commit 33248a6
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions src/wpool_process.erl
Original file line number Diff line number Diff line change
Expand Up @@ -137,30 +137,47 @@ terminate(Reason, State) ->
State,
ok = notify_queue_manager(worker_dead, Name, Options),
wpool_process_callbacks:notify(handle_worker_death, Options, [Name, Reason]),
Mod:terminate(Reason, ModState).
case erlang:function_exported(Mod, terminate, 2) of
true ->
Mod:terminate(Reason, ModState);
_ ->
ok
end.

%% @private
-spec code_change(string(), state(), any()) -> {ok, state()} | {error, term()}.
code_change(OldVsn, State, Extra) ->
case (State#state.mod):code_change(OldVsn, State#state.state, Extra) of
{ok, NewState} ->
{ok, State#state{state = NewState}};
Error ->
{error, Error}
code_change(OldVsn, #state{mod = Mod} = State, Extra) ->
case erlang:function_exported(Mod, code_change, 3) of
true ->
case Mod:code_change(OldVsn, State#state.state, Extra) of
{ok, NewState} ->
{ok, State#state{state = NewState}};
Error ->
{error, Error}
end;
_ ->
{ok, State}
end.

%% @private
-spec handle_info(any(), state()) ->
{noreply, state()} | {noreply, state(), next_step()} | {stop, term(), state()}.
handle_info(Info, State) ->
try (State#state.mod):handle_info(Info, State#state.state) of
handle_info(Info, #state{mod = Mod} = State) ->
try Mod:handle_info(Info, State#state.state) of
{noreply, NewState} ->
{noreply, State#state{state = NewState}};
{noreply, NewState, NextStep} ->
{noreply, State#state{state = NewState}, NextStep};
{stop, Reason, NewState} ->
{stop, Reason, State#state{state = NewState}}
catch
error:undef:Stacktrace ->
case erlang:function_exported(Mod, handle_info, 2) of
false ->
{noreply, State};
true ->
erlang:raise(error, undef, Stacktrace)
end;
_:{noreply, NewState} ->
{noreply, State#state{state = NewState}};
_:{noreply, NewState, NextStep} ->
Expand Down

0 comments on commit 33248a6

Please sign in to comment.