From 5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 23 Feb 2024 07:08:18 +0100 Subject: wip --- learn-you-some-erlang/pool/ppool_nagger.erl | 50 +++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 learn-you-some-erlang/pool/ppool_nagger.erl (limited to 'learn-you-some-erlang/pool/ppool_nagger.erl') diff --git a/learn-you-some-erlang/pool/ppool_nagger.erl b/learn-you-some-erlang/pool/ppool_nagger.erl new file mode 100644 index 0000000..903f821 --- /dev/null +++ b/learn-you-some-erlang/pool/ppool_nagger.erl @@ -0,0 +1,50 @@ +%% demo module, a nagger for tasks, +%% because the previous one wasn't good enough +%% +%% Can take: +%% - a time delay for which to nag, +%% - an adress to say where the messages should be sent +%% - a message to send in the mailbox telling you what to nag, +%% with an id to be able to call: -> +%% - a command to say the task is done +-module(ppool_nagger). +-behaviour(gen_server). +-export([start_link/4, stop/1]). +-export([init/1, handle_call/3, handle_cast/2, + handle_info/2, code_change/3, terminate/2]). + +start_link(Task, Delay, Max, SendTo) -> + gen_server:start_link(?MODULE, {Task, Delay, Max, SendTo} , []). + +stop(Pid) -> + gen_server:call(Pid, stop). + +init({Task, Delay, Max, SendTo}) -> + process_flag(trap_exit, true), % for tests & terminate too + {ok, {Task, Delay, Max, SendTo}, Delay}. + +%%% OTP Callbacks +handle_call(stop, _From, State) -> + {stop, normal, ok, State}; +handle_call(_Msg, _From, State) -> + {noreply, State}. + +handle_cast(_Msg, State) -> + {noreply, State}. + +handle_info(timeout, {Task, Delay, Max, SendTo}) -> + SendTo ! {self(), Task}, + if Max =:= infinity -> + {noreply, {Task, Delay, Max, SendTo}, Delay}; + Max =< 1 -> + {stop, normal, {Task, Delay, 0, SendTo}}; + Max > 1 -> + {noreply, {Task, Delay, Max-1, SendTo}, Delay} + end; +handle_info(_Msg, State) -> + {noreply, State}. + +code_change(_OldVsn, State, _Extra) -> + {ok, State}. + +terminate(_Reason, _State) -> ok. -- cgit v1.2.3