Supervisers who always restart their children

Francesco Cesarini cesarini@REDACTED
Wed Oct 27 11:52:11 CEST 1999


> #In the return value of your Module:init function you can make
> MaxR as some HUGE integer value and MaxT=1
> This will make sure your supervised process is always restarted.

That is only safeish, however. It can be argued that the supervisor
behaviour should allow the atom infinity a a valid value denoting MaxR and
MaxT to be set to infinity, as this value is allowed elsewhere along side
integers. It is just a few lines of code to be added in the supervisor
module... The easiest solution, however, is your own supervisor...

Rgds,
FC
--

-module(mysup).

-behaviour(gen_server).

-export([start_link/3]).
-export([init/1, handle_info/2, terminate/2]).

%%%----------------------------------------------------------------------
%%% API
%%%----------------------------------------------------------------------
start_link(Mod,Fun,Args) ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, {Mod,Fun,Args}, []).

%%%----------------------------------------------------------------------
%%% Callback functions from gen_server
%%%----------------------------------------------------------------------

init({Mod,Func,Args}) ->
    process_flag(trap_exit, true),
    Pid = proc_lib:spawn_link(Mod, Func, Args),
    {ok, {Pid,{Mod,Func,Args}}}.

handle_info({'EXIT', Pid, Reason}, {Pid, {Mod, Func, Args}}) ->
    NewPid = proc_lib:spawn_link(Mod, Func, Args),
    {noreply, {NewPid, {Mod, Func, Args}}}.

terminate(Reason, {Pid, {Mod, Func, Arg}}) ->
	exit(Pid, shutdown),    %%Be kind
	receive
           {'EXIT', Pid, shutdown} -> ok
         after 10000 ->		%%Timeout, use violence
           exit(Pid, kill)
        end




More information about the erlang-questions mailing list