Skip to content

Commit

Permalink
fix graceful shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
november-pain committed Nov 19, 2024
1 parent 8e21aa8 commit 0dd33d8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
30 changes: 24 additions & 6 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
import asyncio
import logging
import signal
from aiohttp import web
from .config import Config
from .monitor import TokenMonitor
Expand All @@ -18,7 +19,25 @@ async def main_async():
config = None
monitor = None
runner = None


async def shutdown():
logger.info("Received shutdown signal")
if monitor:
try:
await monitor.shutdown()
except Exception as e:
logger.error(f"Error during monitor shutdown: {e}")
if runner:
try:
logger.info("Cleaning up runner...")
await runner.cleanup()
except Exception as e:
logger.error(f"Error during runner cleanup: {e}")
logger.info("Finishing event loop...")

loop = asyncio.get_running_loop()
loop.add_signal_handler(signal.SIGTERM, lambda: asyncio.create_task(shutdown()))

try:
# Load configuration
config_path = os.getenv('CONFIG_PATH', 'config.yaml')
Expand All @@ -42,10 +61,7 @@ async def main_async():

except Exception as e:
logger.error(f"Fatal error: {e}")
if monitor:
await monitor.shutdown()
if runner:
await runner.cleanup()
await shutdown()
sys.exit(1)

def main():
Expand All @@ -56,6 +72,8 @@ def main():
except Exception as e:
logger.error(f"Fatal error in main: {e}")
sys.exit(1)
finally:
logger.info("Program terminated")

if __name__ == "__main__":
main()
main()
10 changes: 8 additions & 2 deletions src/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ async def collect_metrics(self):
logger.error(f"Error in collection loop: {e}")

await asyncio.sleep(self.config.scrape_interval)

logger.info("Metrics collection stopped")

async def init_web3_connections(self):
"""Initialize Web3 connections and verify them"""
Expand All @@ -216,5 +218,9 @@ async def shutdown(self):
self.running = False
# Add a small delay to allow pending operations to complete
await asyncio.sleep(1)
await self.app.shutdown()
await self.app.cleanup()
if self.app:
self.app.freeze()
await self.app.shutdown()
await self.app.cleanup()
else:
logger.warning("App is not initialized, skipping shutdown steps.")

0 comments on commit 0dd33d8

Please sign in to comment.