|
|
| import os
|
| import sys
|
| import subprocess
|
| import time
|
| from pathlib import Path
|
|
|
| def run_command(cmd):
|
| print(f"Running command: {' '.join(cmd)}")
|
| process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
|
|
|
| for line in process.stdout:
|
| sys.stdout.write(line)
|
| sys.stdout.flush()
|
|
|
| process.wait()
|
| return process.returncode
|
|
|
| def check_required_files():
|
| required_files = [
|
| "pyscout_api.py",
|
| "deepinfra_client.py",
|
| "proxy_finder.py",
|
| "db_helper.py",
|
| "hf_utils.py",
|
| ]
|
|
|
| for file in required_files:
|
| if not Path(file).exists():
|
| print(f"ERROR: Required file '{file}' not found!")
|
| return False
|
|
|
| return True
|
|
|
| def wait_for_mongodb():
|
| import time
|
| import pymongo
|
|
|
| mongo_uri = os.environ.get("MONGODB_URI")
|
| if not mongo_uri:
|
| print("MongoDB URI not found in environment variables, skipping connection check")
|
| return True
|
|
|
| max_attempts = 30
|
| for attempt in range(max_attempts):
|
| try:
|
| client = pymongo.MongoClient(mongo_uri, serverSelectionTimeoutMS=5000)
|
| client.admin.command('ping')
|
| print(f"MongoDB connection successful after {attempt+1} attempts")
|
| return True
|
| except Exception as e:
|
| print(f"Attempt {attempt+1}/{max_attempts}: MongoDB not yet available. Waiting... ({str(e)})")
|
| time.sleep(2)
|
|
|
| print("ERROR: Failed to connect to MongoDB after multiple attempts")
|
| return False
|
|
|
| def main():
|
| if not check_required_files():
|
| sys.exit(1)
|
|
|
|
|
| print("Starting PyScoutAI API server")
|
| wait_for_mongodb()
|
|
|
| host = os.environ.get("HOST", "0.0.0.0")
|
| port = int(os.environ.get("PORT", "7860"))
|
|
|
| cmd = [
|
| "uvicorn",
|
| "pyscout_api:app",
|
| "--host", host,
|
| "--port", str(port)
|
| ]
|
|
|
| return run_command(cmd)
|
|
|
| if __name__ == "__main__":
|
| sys.exit(main()) |