#!/usr/bin/env python3 """ Quick fix script to stop all running scraper tasks and restart Celery workers. This ensures the updated code is loaded and tasks are properly terminated. """ import os import sys import signal import subprocess import time from datetime import datetime # Add project root to path sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../..'))) def kill_celery_processes(): """Kill all running Celery processes""" print("Killing Celery processes...") try: # Get all celery processes result = subprocess.run(['pgrep', '-f', 'celery'], capture_output=True, text=True) if result.returncode == 0: pids = result.stdout.strip().split('\n') for pid in pids: if pid: try: os.kill(int(pid), signal.SIGTERM) print(f" Killed process {pid}") except ProcessLookupError: pass # Process already dead # Wait a moment for graceful shutdown time.sleep(2) # Force kill any remaining processes result = subprocess.run(['pgrep', '-f', 'celery'], capture_output=True, text=True) if result.returncode == 0: pids = result.stdout.strip().split('\n') for pid in pids: if pid: try: os.kill(int(pid), signal.SIGKILL) print(f" Force killed process {pid}") except ProcessLookupError: pass print("✓ All Celery processes terminated") except Exception as e: print(f"⚠ Error killing processes: {e}") def stop_scraper_state(): """Set scraper state to inactive using Flask app context""" try: from scipaperloader import create_app from scipaperloader.models import ScraperState, PaperMetadata from scipaperloader.db import db app = create_app() with app.app_context(): # Set scraper to inactive ScraperState.set_active(False) ScraperState.set_paused(False) print("✓ Set scraper state to inactive") # Revert any pending papers to "New" status (simple approach since we don't have previous_status data yet) pending_papers = PaperMetadata.query.filter_by(status="Pending").all() reverted_count = 0 for paper in pending_papers: paper.status = "New" # Simple fallback - revert all to "New" reverted_count += 1 if reverted_count > 0: db.session.commit() print(f"✓ Reverted {reverted_count} papers from 'Pending' to 'New'") else: print("✓ No pending papers to revert") except Exception as e: print(f"⚠ Error setting scraper state: {e}") def main(): print("=== QUICK SCRAPER FIX ===") print(f"Time: {datetime.now()}") print() # Step 1: Stop scraper state stop_scraper_state() # Step 2: Kill all Celery processes kill_celery_processes() print() print("=== FIX COMPLETE ===") print("The scraper has been stopped and all tasks terminated.") print("You can now restart the Celery workers with:") print(" make celery") print("or") print(" make run") if __name__ == "__main__": main()