#!/usr/bin/env python3 """ Standalone metrics server for testing. Run this to start just the metrics system without the bot. """ import asyncio import signal import sys from helper_bot.utils.metrics_exporter import MetricsManager class MetricsServer: """Standalone metrics server.""" def __init__(self, host: str = "0.0.0.0", port: int = 8000): self.host = host self.port = port self.metrics_manager = MetricsManager(host, port) self.running = False async def start(self): """Start the metrics server.""" try: await self.metrics_manager.start() self.running = True print(f"šŸš€ Metrics server started on {self.host}:{self.port}") print(f"šŸ“Š Metrics endpoint: http://{self.host}:{self.port}/metrics") print(f"šŸ„ Health check: http://{self.host}:{self.port}/health") print(f"ā„¹ļø Info: http://{self.host}:{self.port}/") print("\nPress Ctrl+C to stop the server") # Keep the server running while self.running: await asyncio.sleep(1) except Exception as e: print(f"āŒ Error starting metrics server: {e}") raise async def stop(self): """Stop the metrics server.""" if self.running: self.running = False await self.metrics_manager.stop() print("\nšŸ›‘ Metrics server stopped") def signal_handler(self, signum, frame): """Handle shutdown signals.""" print(f"\nšŸ“” Received signal {signum}, shutting down...") asyncio.create_task(self.stop()) async def main(): """Main function.""" # Parse command line arguments host = "0.0.0.0" port = 8000 if len(sys.argv) > 1: host = sys.argv[1] if len(sys.argv) > 2: port = int(sys.argv[2]) # Create and start server server = MetricsServer(host, port) # Setup signal handlers signal.signal(signal.SIGINT, server.signal_handler) signal.signal(signal.SIGTERM, server.signal_handler) try: await server.start() except KeyboardInterrupt: print("\nšŸ“” Keyboard interrupt received") finally: await server.stop() if __name__ == "__main__": print("šŸ”§ Starting standalone metrics server...") print("Usage: python run_metrics_only.py [host] [port]") print("Default: host=0.0.0.0, port=8000") print() try: asyncio.run(main()) except KeyboardInterrupt: print("\nšŸ›‘ Server stopped by user") except Exception as e: print(f"āŒ Server error: {e}") sys.exit(1)