[erlang-questions] Stopping gen_server during processing

Tony Rogvall tony@REDACTED
Fri Nov 8 11:03:36 CET 2013


Maybe you just want to kill it ?

exit(Pid, die_now). 

or if the little bugger is trapping exit you may have to be brutal.

exit(Pid, kill).

http://www.erlang.org/doc/man/erlang.html#exit-2


You can send the process normal messages:

Pid ! stop_processing_now.

Then while doing processing loop:

check_records(State) ->
	do some work...
	receive 
		stop_processing_now -> {stop, normal, State};
	after 0 ->
		check_records(State)
	end

The after 0 make sure that the process will not wait for messages, also
the selective receive approach here will guarantee that messages to
the gen_server (call and casts etc) will stay where they are.

You may have to add a clause in handle_info to handle the case
where the message is receive by the gen_server dispatcher.

handle_info(stop_processing_now, State) ->
	Stop? or something


/Tony



On 8 nov 2013, at 10:43, Janos Hary <janos.n.hary@REDACTED> wrote:

> Sorry, I cannot see how could it work in Erlang.
> 
> I have a handle_info processing a large number of records:
> handle_info(check, State) ->
> 	check_records(State).
> 
> check_records(State) ->
> 	do some work...
> 	case State#stop_flag of
> 		true ->
> 			{stop, normal, State};
> 		_ ->
> 			check_records(State)
> 	end.
> 
> The State in check_records cannot changed from another cast. I'm not even
> sure that the cast will execute before check_records finishes.
> 
> Janos
> 
> 
> -----Original Message-----
> From: erlang-questions-bounces@REDACTED
> [mailto:erlang-questions-bounces@REDACTED] On Behalf Of Bengt Kleberg
> Sent: Friday, November 08, 2013 9:28 AM
> To: undisclosed-recipients:
> Cc: erlang-questions@REDACTED
> Subject: Re: [erlang-questions] Stopping gen_server during processing
> 
> These are my opinions, not hard facts.
> 
> Add a member to the gen_server state record that says that you are going to
> stop when possible. Use a cast to set it to true when you want to stop. If
> it is true and you can stop the gen_server, stop it.
> 
> 
> bengt
> 
> On Fri, 2013-11-08 at 08:55 +0100, Janos Hary wrote:
>> All,
>> 
>> 
>> 
>> I have a gen_server process which periodically processes new records 
>> from a database. I use timer:send_after to schedule gathering and 
>> processing new records. I can gracefully stop the gen_server when it 
>> is waiting, but I need a way to stop it during processing. Of cause I 
>> only want to stop it at certain points of the processing, for example 
>> before starting to process the next record. I need to group the 
>> records, so I cannot ask them one by one from the db.
>> 
>> 
>> 
>> What is the preferred way of doing this?
>> 
>> 
>> 
>> Thanks for your answers in advance.
>> 
>> Janos
>> 
>> 
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
> 
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
> 
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions

"Installing applications can lead to corruption over time. Applications gradually write over each other's libraries, partial upgrades occur, user and system errors happen, and minute changes may be unnoticeable and difficult to fix"



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20131108/74685739/attachment.htm>


More information about the erlang-questions mailing list