问题描述
我使用OTP主管行为来监督和重新启动子进程。然而,当孩子死亡时,我想以崩溃之前的状态重新启动它。
I'm using OTP supervisor behaviour to supervise and restart child processes. However when the child dies I want to restart it with the same state it had before the crash.
如果我编写自己的自定义主管,我可以接收{EXIT,Pid,Reason}消息并对其进行操作。当使用OTP主管行为时,它全部由OTP管理,我无法控制。我实现的唯一的回调函数是init。
If I write my own custom supervisor, I can just receive {EXIT,Pid,Reason} message and act upon it. When using OTP supervisor behaviour however it is all managed by OTP and I have no control over it. The only callback function I implement is init.
在这种情况下有没有任何标准的方法?如何自定义由otp主管动态重新启动的孩子的状态?如何使用OTP获取Pid终止进程?或者也许它可能在终止之前得到孩子的状态,然后将孩子恢复到与之前相同的状态?
Is there any standard approach in case like this? How to customise the state of a child being restarted dynamically by the otp supervisor? How to get Pid of the terminating process using OTP? Or maybe its possible to get the state of the child just before termination, and then restore the child to the same state it had before it crashed?
推荐答案
可能以同样的状态重新启动不是一个好主意。可能错误的状态导致进程崩溃,如果您以同样的状态重新启动,它将再次崩溃。但是如果你想要这个,使用外部资源来保存它(如ets或mnesia)。
Possibly restart with same state is not good idea. Probably wrong state lead process to crash and if you restart with same state, it will crash again. But if you want this, use external resource to keep it (like ets or mnesia).
这篇关于如何使用Erlang OTP管理员行为重新启动具有自定义状态的子级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!