gpt4 book ai didi

elixir - 如何从任何链接进程获得任何崩溃通知?

转载 作者:行者123 更新时间:2023-12-04 08:42:34 26 4
gpt4 key购买 nike

我通过 Supervisor、Dynamic supervisor 在我的应用程序中启动多个 (genserver) 进程,有时甚至只是一个直接的 start_link。当我使用 iex -S mix 启动应用程序时,其中一些可能会崩溃而不会通知我。如果我自己没有发现,如何确保我收到来自任何进程的所有崩溃通知?在跟踪所有事件的同时围绕“让进程崩溃”有哪些好的做法?

XXX.start_link(ws_uri, __MODULE__, state, [{:name, MyModule}])

最佳答案

来自GenServer docs :

terminate/2 is called if a callback (except init/1) does one of the following:

-returns a :stop tuple
-raises
-calls Kernel.exit/1
-returns an invalid value
-the GenServer traps exits (using Process.flag/2) and the parent process sends an exit signal

If part of a supervision tree, a GenServer will receive an exit signalwhen the tree is shutting down. The exit signal is based on theshutdown strategy in the child's specification,
...
...
If the GenServer receives an exit signal (that is not :normal)from any process when it is not trapping exits it will exit abruptlywith the same reason and so not call terminate/2. Note that a processdoes NOT trap exits by default and an exit signal is sent when alinked process exits or its node is disconnected.

Therefore it is not guaranteed that terminate/2 is called when aGenServer exits. For such reasons, we usually recommend importantclean-up rules to happen in separated processes either by use ofmonitoring or by links themselves. There is no cleanup needed when theGenServer controls a port (for example, :gen_tcp.socket) orFile.io_device/0, because these will be closed on receiving aGenServer's exit signal and do not need to be closed manually interminate/2.

关于elixir - 如何从任何链接进程获得任何崩溃通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64485373/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com