complete a long-term task in a separate process. Let this process inform gen_server about its progress with the task (that is, if the progress of the task can be tracked) OR let the process complete the task or fail, but at least inform gen_server about the results of the task.
Let gen_server be associated with the process performing this long-term task, and let gen_server know the PID or registered name so that in case of output signals it can isolate the death of this important process from Rest.
handle_info (trigger, State) ->
Pid = spawn_link (? MODULE, some_long_running_task, [State]),
NewState = save_pid (Pid, State),
{noreply, NewState};
handle_info ({'EXIT', SomePid, _}, State) ->
case lookup_pid (State) == SomePid of
false -> %% some other process
{noreply, State};
true ->
%% our process has died
%% what do we do now?
%% spawn another one?
%% thats your decision to take
....
....
{noreply, State}
end;
handle_info ({finished, TaskResult}, State) ->
..... %% update state etc
erlang: send_after (? LOOP_TIME, self (), trigger),
{noreply, NewState}.
some_long_running_task (ServerState) ->
.... do work
.... return results
Muzaaya Joshua
source share