- Removed unnecessary `__init__.py` and `Dockerfile` to streamline project organization. - Updated `.dockerignore` and `.gitignore` to improve exclusion patterns for build artifacts and environment files. - Enhanced `Makefile` with new commands for managing Docker containers and added help documentation. - Introduced `pyproject.toml` for better project metadata management and dependency tracking. - Updated `requirements.txt` to reflect changes in dependencies for metrics and monitoring. - Refactored various handler files to improve code organization and maintainability.
93 lines
2.7 KiB
Python
93 lines
2.7 KiB
Python
#!/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)
|