How to do something when the server stop?

51 views
Skip to first unread message

Caiyun Deng

unread,
Aug 4, 2014, 5:16:29 AM8/4/14
to chica...@googlegroups.com
Hi!
When i want to do something at the server start, I can do something in priv/init.
But when i want to do something at the server stop, How can i do?

Jesse Gumm

unread,
Aug 5, 2014, 3:02:11 PM8/5/14
to chica...@googlegroups.com
There's not currently (As far as I know) a standard way to do that,
but you could use a monitor to achieve it.

Ref = monitor(process, boss_sup),
receive
{'DOWN', _, process, {boss_sup, _}, _} -> run_my_other_shutdown_scripts()
end.

-Jesse
> --
> You received this message because you are subscribed to the Google Groups
> "ChicagoBoss" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to chicagoboss...@googlegroups.com.
> Visit this group at http://groups.google.com/group/chicagoboss.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/chicagoboss/b1c56929-e5ac-4990-ba10-6a3188e49ed2%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.



--
Jesse Gumm
Owner, Sigma Star Systems
414.940.4866 || sigma-star.com || @jessegumm

Caiyun Deng

unread,
Aug 6, 2014, 12:51:10 AM8/6/14
to chica...@googlegroups.com
 Hi! I tried it,  I typed "q()" in ./init-dev.sh and "./init.sh stop" in the production mode, but it does not work.
 spawn(fun() -> monitor_stop() end),
monitor_stop() ->
    ?TRACE_VAR(monitor_stop),
    Ref = erlang:monitor(process, boss_sup),
    receive
        Msg ->
            ?TRACE_VAR(Msg),
            monitor_stop()
    end.



在 2014年8月6日星期三UTC+8上午3时02分11秒,Jesse Gumm写道:

chan sisowath

unread,
Aug 6, 2014, 1:38:06 AM8/6/14
to chica...@googlegroups.com
hi,

same as init

just implement stop/1 in you module piv/init
if it doesn't work just make an issue.

chan












Caiyun Deng

unread,
Aug 6, 2014, 2:23:07 AM8/6/14
to chica...@googlegroups.com
I also tried stop/1 in priv/init, it does not work.
But it didn't say the stop/1 will execute when the server stop. It just say that "stop is executed if the script is ever reloaded".

在 2014年8月6日星期三UTC+8下午1时38分06秒,mihawk写道:

chan sisowath

unread,
Aug 6, 2014, 2:40:52 AM8/6/14
to chica...@googlegroups.com

Caiyun Deng

unread,
Aug 6, 2014, 3:19:33 AM8/6/14
to chica...@googlegroups.com
Does your stop/1 in the priv/init will be called when the server stop?

在 2014年8月6日星期三UTC+8下午2时40分52秒,mihawk写道:

chan sisowath

unread,
Aug 6, 2014, 6:36:11 AM8/6/14
to chica...@googlegroups.com
this code should work :).

as jesse suggested


-module(test_script).

-export([init/0, stop/1]).
-export([watch_boss/0]).


init() ->
    Pid = spawn(?MODULE, watch_boss, []),
    io:format("~n~n<<<< -------- init -------- script --------->> Pid ~p ~n~n",[Pid]),
    {ok, []}.

stop(_) ->
 [].

watch_boss() ->
    Ref = monitor(process, boss_web),
    receive
        {Reason, _, process, {boss_web, _}, _} ->
            %% call my awesome script
            io:format("~n~n<<<< -------- stop -------- script --------->> ~p ~p<<---~n~n",[Ref, Reason])
    end.


Jesse Gumm

unread,
Aug 6, 2014, 9:05:44 AM8/6/14
to chica...@googlegroups.com

I'm honestly not sure how gracefully q() kills the VM, but both Chan and I are giving suggestions for when you run boss:stop().

If you are going want to shut down gracefully, then I'd suggest boss:stop(), followed by q().

Then you can be sure your shutdown scripts have been run. That said, there are obviously numerous reasons a VM could be shut down instantly and without a proper shutdown (kill -9, power failure, kernel panic, etc).

If you can, I recommend building around that kind of restriction, rather than relying on a "clean shutdown" every time.

--
Jesse Gumm
Owner, Sigma Star Systems
414.940.4866 || sigma-star.com || @jessegumm

Caiyun Deng

unread,
Aug 6, 2014, 10:35:09 PM8/6/14
to chica...@googlegroups.com
monitor(process, boss_web)
It works.
Thanks for your help! 
Reply all
Reply to author
Forward
0 new messages