13
13
# limitations under the License.
14
14
15
15
import os
16
- from typing import NoReturn
16
+ from typing import Callable , NoReturn
17
17
18
18
from structlog import get_logger
19
19
20
- from hathor .event import EventManager
21
- from hathor .transaction .storage import TransactionStorage
22
-
23
20
logger = get_logger ()
24
21
25
22
26
23
class ExecutionManager :
27
24
"""Class to manage actions related to full node execution."""
28
- __slots__ = ('_log' , '_tx_storage' , '_event_manager ' )
25
+ __slots__ = ('_log' , '_on_crash_callbacks ' )
29
26
30
- def __init__ (self , * , tx_storage : TransactionStorage , event_manager : EventManager ) -> None :
27
+ def __init__ (self ) -> None :
31
28
self ._log = logger .new ()
32
- self ._tx_storage = tx_storage
33
- self ._event_manager = event_manager
29
+ self ._on_crash_callbacks : list [tuple [int , Callable [[], None ]]] = []
30
+
31
+ def register_on_crash_callback (self , callback : Callable [[], None ], * , priority : int = 0 ) -> None :
32
+ """Register a callback to be executed before the full node exits."""
33
+ self ._on_crash_callbacks .append ((priority , callback ))
34
+
35
+ def _run_on_crash_callbacks (self ) -> None :
36
+ """Run all registered on crash callbacks."""
37
+ callbacks = sorted (self ._on_crash_callbacks , reverse = True )
38
+
39
+ for _ , callback in callbacks :
40
+ try :
41
+ callback ()
42
+ except BaseException as e :
43
+ self ._log .critical (f'Failed execution of on_crash callback "{ callback } ". Exception: { repr (e )} ' )
34
44
35
45
def crash_and_exit (self , * , reason : str ) -> NoReturn :
36
46
"""
@@ -39,8 +49,7 @@ def crash_and_exit(self, *, reason: str) -> NoReturn:
39
49
corrupted, and requiring manual intervention. In other words, a restart with a clean database (from scratch
40
50
or a snapshot) will be required.
41
51
"""
42
- self ._tx_storage .full_node_crashed ()
43
- self ._event_manager .full_node_crashed ()
52
+ self ._run_on_crash_callbacks ()
44
53
self ._log .critical (
45
54
'Critical failure occurred, causing the full node to halt execution. Manual intervention is required.' ,
46
55
reason = reason ,
0 commit comments