|
| 1 | +from argparse import ArgumentParser, Namespace |
| 2 | +import asyncio |
| 3 | +import logging |
| 4 | +import signal |
| 5 | +from typing import ( |
| 6 | + Any, |
| 7 | + Dict, |
| 8 | +) |
| 9 | + |
| 10 | +from lahja import ( |
| 11 | + EventBus, |
| 12 | + Endpoint, |
| 13 | +) |
| 14 | + |
| 15 | +from eth.db.backends.level import LevelDB |
| 16 | + |
| 17 | +from trinity.bootstrap import ( |
| 18 | + kill_trinity_gracefully, |
| 19 | + main_entry, |
| 20 | + run_database_process, |
| 21 | +) |
| 22 | +from trinity.config import ( |
| 23 | + TrinityConfig, |
| 24 | +) |
| 25 | +from trinity.events import ( |
| 26 | + ShutdownRequest |
| 27 | +) |
| 28 | +from trinity.extensibility import ( |
| 29 | + PluginManager, |
| 30 | +) |
| 31 | +from trinity.utils.ipc import ( |
| 32 | + wait_for_ipc, |
| 33 | + kill_process_gracefully, |
| 34 | +) |
| 35 | +from trinity.utils.mp import ( |
| 36 | + ctx, |
| 37 | +) |
| 38 | + |
| 39 | + |
| 40 | +def main_beacon() -> None: |
| 41 | + main_entry(trinity_boot) |
| 42 | + |
| 43 | + |
| 44 | +def trinity_boot(args: Namespace, |
| 45 | + trinity_config: TrinityConfig, |
| 46 | + extra_kwargs: Dict[str, Any], |
| 47 | + plugin_manager: PluginManager, |
| 48 | + listener: logging.handlers.QueueListener, |
| 49 | + event_bus: EventBus, |
| 50 | + main_endpoint: Endpoint, |
| 51 | + logger: logging.Logger) -> None: |
| 52 | + # start the listener thread to handle logs produced by other processes in |
| 53 | + # the local logger. |
| 54 | + listener.start() |
| 55 | + |
| 56 | + event_bus.start() |
| 57 | + |
| 58 | + # First initialize the database process. |
| 59 | + database_server_process = ctx.Process( |
| 60 | + name="DB", |
| 61 | + target=run_database_process, |
| 62 | + args=( |
| 63 | + trinity_config, |
| 64 | + LevelDB, |
| 65 | + ), |
| 66 | + kwargs=extra_kwargs, |
| 67 | + ) |
| 68 | + |
| 69 | + # start the processes |
| 70 | + database_server_process.start() |
| 71 | + logger.info("Started DB server process (pid=%d)", database_server_process.pid) |
| 72 | + |
| 73 | + # networking process needs the IPC socket file provided by the database process |
| 74 | + try: |
| 75 | + wait_for_ipc(trinity_config.database_ipc_path) |
| 76 | + except TimeoutError as e: |
| 77 | + logger.error("Timeout waiting for database to start. Exiting...") |
| 78 | + kill_process_gracefully(database_server_process, logger) |
| 79 | + ArgumentParser().error(message="Timed out waiting for database start") |
| 80 | + |
| 81 | + def kill_trinity_with_reason(reason: str) -> None: |
| 82 | + kill_trinity_gracefully( |
| 83 | + logger, |
| 84 | + (database_server_process,), |
| 85 | + plugin_manager, |
| 86 | + main_endpoint, |
| 87 | + event_bus, |
| 88 | + reason=reason |
| 89 | + ) |
| 90 | + |
| 91 | + main_endpoint.subscribe( |
| 92 | + ShutdownRequest, |
| 93 | + lambda ev: kill_trinity_with_reason(ev.reason) |
| 94 | + ) |
| 95 | + |
| 96 | + plugin_manager.prepare(args, trinity_config, extra_kwargs) |
| 97 | + |
| 98 | + kill_trinity_with_reason("No beacon support yet. SOON!") |
| 99 | + |
| 100 | + try: |
| 101 | + loop = asyncio.get_event_loop() |
| 102 | + loop.add_signal_handler(signal.SIGTERM, lambda: kill_trinity_with_reason("SIGTERM")) |
| 103 | + loop.run_forever() |
| 104 | + loop.close() |
| 105 | + except KeyboardInterrupt: |
| 106 | + kill_trinity_with_reason("CTRL+C / Keyboard Interrupt") |
0 commit comments