#!/usr/bin/env python3 """ start.py – Gunicorn Process Manager for SexyChat. Usage: python start.py start # Starts SexyChat in the background python start.py stop # Stops the background process python start.py restart # Restarts the background process python start.py status # Shows if the server is running python start.py debug # Runs in the foreground with debug logging """ import os import sys import subprocess import signal import time import eventlet # Monkey-patch stdlib BEFORE any other import eventlet.monkey_patch() from config import get_conf # PID file to track the daemon process PID_FILE = "sexchat.pid" def get_pid(): if os.path.exists(PID_FILE): with open(PID_FILE, "r") as f: try: return int(f.read().strip()) except ValueError: return None return None def is_running(pid): if not pid: return False try: os.kill(pid, 0) except OSError: return False return True def start_daemon(): pid = get_pid() if is_running(pid): print(f"āŒ SexChat is already running (PID: {pid}).") return print("šŸš€ Starting SexChat in background...") cmd = [ "gunicorn", "--worker-class", "eventlet", "-w", "1", "--bind", f"{get_conf('HOST', '0.0.0.0')}:{get_conf('PORT', 5000)}", "--daemon", "--pid", PID_FILE, "--access-logfile", "access.log", "--error-logfile", "error.log", "start:application" ] subprocess.run(cmd) time.sleep(1) new_pid = get_pid() if is_running(new_pid): print(f"āœ… SexChat started successfully (PID: {new_pid}).") else: print("āŒ Failed to start SexChat. Check error.log for details.") def stop_daemon(): pid = get_pid() if not is_running(pid): print("ā„¹ļø SexChat is not running.") if os.path.exists(PID_FILE): os.remove(PID_FILE) return print(f"šŸ›‘ Stopping SexChat (PID: {pid})...") try: os.kill(pid, signal.SIGTERM) # Wait for it to die for _ in range(10): if not is_running(pid): break time.sleep(0.5) if is_running(pid): print("āš ļø Force killing...") os.kill(pid, signal.SIGKILL) if os.path.exists(PID_FILE): os.remove(PID_FILE) print("āœ… SexChat stopped.") except Exception as e: print(f"āŒ Error stopping: {e}") def get_status(): pid = get_pid() if is_running(pid): print(f"🟢 SexChat is RUNNING (PID: {pid}).") else: print("šŸ”“ SexChat is STOPPED.") def run_debug(): print("šŸ› ļø Starting SexChat in DEBUG mode (foreground)...") cmd = [ "gunicorn", "--worker-class", "eventlet", "-w", "1", "--bind", f"{get_conf('HOST', '0.0.0.0')}:{get_conf('PORT', 5000)}", "--log-level", "debug", "--access-logfile", "-", "--error-logfile", "-", "start:application" ] try: subprocess.run(cmd) except KeyboardInterrupt: print("\nšŸ‘‹ Debug session ended.") # This is the Flask Application instance for Gunicorn from app import create_app application = create_app() if __name__ == "__main__": if len(sys.argv) < 2: print(__doc__) sys.exit(1) command = sys.argv[1].lower() if command == "start": start_daemon() elif command == "stop": stop_daemon() elif command == "restart": stop_daemon() time.sleep(1) start_daemon() elif command == "status": get_status() elif command == "debug": run_debug() else: print(f"āŒ Unknown command: {command}") print(__doc__) sys.exit(1)