rss/scheduler.py
2025-11-24 23:06:26 +01:00

42 lines
1.2 KiB
Python

import time
import logging
import atexit
from datetime import datetime, timedelta
import sys
from apscheduler.schedulers.background import BackgroundScheduler
from app import app, fetch_and_store_all
logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='[%(asctime)s] %(levelname)s in %(module)s: %(message)s')
scheduler = BackgroundScheduler(daemon=True, timezone="UTC")
def shutdown_scheduler():
"""Función para detener el planificador de forma segura al salir."""
if scheduler.running:
scheduler.shutdown()
logging.info("Scheduler del worker detenido.")
atexit.register(shutdown_scheduler)
if __name__ == '__main__':
with app.app_context():
scheduler.add_job(
fetch_and_store_all,
"interval",
minutes=3,
id="rss_job",
next_run_time=datetime.utcnow() + timedelta(seconds=10)
)
scheduler.start()
logging.info("Worker del Scheduler iniciado. Tarea programada cada 3 minutos.")
logging.info("El proceso se mantendrá en ejecución para permitir que se ejecuten las tareas.")
try:
while True:
time.sleep(60)
except (KeyboardInterrupt, SystemExit):
logging.info("Apagando el worker...")