|
| 1 | +# Copyright 2023 Hathor Labs |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +from typing import NoReturn |
| 17 | + |
| 18 | +from structlog import get_logger |
| 19 | + |
| 20 | +from hathor.event import EventManager |
| 21 | +from hathor.transaction.storage import TransactionStorage |
| 22 | + |
| 23 | +logger = get_logger() |
| 24 | + |
| 25 | + |
| 26 | +class ExecutionManager: |
| 27 | + """Class to manage actions related to full node execution.""" |
| 28 | + __slots__ = ('_log', '_tx_storage', '_event_manager') |
| 29 | + |
| 30 | + def __init__(self, *, tx_storage: TransactionStorage, event_manager: EventManager) -> None: |
| 31 | + self._log = logger.new() |
| 32 | + self._tx_storage = tx_storage |
| 33 | + self._event_manager = event_manager |
| 34 | + |
| 35 | + def crash_and_exit(self, *, reason: str) -> NoReturn: |
| 36 | + """ |
| 37 | + Calling this function is a very extreme thing to do, so be careful. It should only be called when a |
| 38 | + critical, unrecoverable failure happens. It crashes and exits the full node, rendering the database |
| 39 | + corrupted, and requiring manual intervention. In other words, a restart with a clean database (from scratch |
| 40 | + or a snapshot) will be required. |
| 41 | + """ |
| 42 | + self._tx_storage.full_node_crashed() |
| 43 | + self._event_manager.full_node_crashed() |
| 44 | + self._log.critical( |
| 45 | + 'Critical failure occurred, causing the full node to halt execution. Manual intervention is required.', |
| 46 | + reason=reason, |
| 47 | + exc_info=True |
| 48 | + ) |
| 49 | + # We use os._exit() instead of sys.exit() or any other approaches because this is the only one Twisted |
| 50 | + # doesn't catch. |
| 51 | + os._exit(-1) |
0 commit comments