#!/usr/bin/env python3 """Scheduled runner that executes the pipeline hourly and logs results.""" import time import signal import sys from datetime import datetime from pathlib import Path from pipeline import Pipeline LOG_DIR = Path(__file__).parent / "logs" LOG_DIR.mkdir(exist_ok=True) running = True def signal_handler(sig, frame): global running print("\nShutting down scheduled runner...") running = False signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGTERM, signal_handler) def main(): pipeline = Pipeline(live=False, bankroll=1000.0) print(f"Scheduled runner started at {datetime.now():%Y-%m-%d %H:%M:%S}") print(f"Running every hour. Logs in {LOG_DIR}/") print() while running: try: log_file = LOG_DIR / f"run_{datetime.now():%Y%m%d_%H%M}.txt" with open(log_file, "w") as f: old_stdout = sys.stdout sys.stdout = f pipeline.run() sys.stdout = old_stdout print(f"[{datetime.now():%H:%M}] Run complete -> {log_file}") except Exception as e: print(f"[{datetime.now():%H:%M}] Error: {e}") # Sleep until next hour for _ in range(3600): if not running: break time.sleep(1) print("Runner stopped.") if __name__ == "__main__": main()