Files
ORCID2SWORD/backend/app/scheduler/sync_scheduler.py
T
Mireya Cueto Garrido af1b8e9956 feat: enhance backend security and configuration
- Updated Dockerfile to improve security with a non-root user and added health checks.
- Modified docker-compose.yml to set containers as read-only, restrict ports to localhost, and implement health checks.
- Enhanced .env.example with additional environment variables for security and configuration.
- Improved FastAPI application with middleware for security headers, CORS, and body size limits.
- Refactored authentication flow in auth.py to include state validation and improved error handling.
- Added rate limiting to various endpoints to prevent abuse.
- Updated researcher and publication handling to ensure better validation and error management.
2026-05-08 11:19:52 +02:00

54 lines
1.6 KiB
Python

import requests
from apscheduler.schedulers.background import BackgroundScheduler
from app.db.session import SessionLocal
from app.db.repositories.researcher_repository import ResearcherRepository
from dotenv import load_dotenv
import os
# Cargar variables del .env
load_dotenv()
# ---------------------------------------------------------
# Variables de entorno
# ---------------------------------------------------------
API_KEY = os.getenv("API_KEY_VALUE")
BASE_URL = os.getenv("BASE_URL")
# ---------------------------------------------------------
# Función auxiliar: ejecutar sincronización mensual
# ---------------------------------------------------------
def run_monthly_sync():
db = SessionLocal()
researchers = ResearcherRepository.get_all(db)
for r in researchers:
try:
url = f"{BASE_URL}/researchers/{r.orcid_id}/sync"
response = requests.post(
url,
headers={"X-API-Key": API_KEY}
)
if response.status_code != 200:
print(f"[ERROR] Sync failed for {r.orcid_id}: {response.text}")
else:
print(f"[OK] Synced {r.orcid_id}")
except Exception as e:
print(f"[EXCEPTION] Error syncing {r.orcid_id}: {e}")
db.close()
# ---------------------------------------------------------
# Función auxiliar: iniciar el scheduler
# ---------------------------------------------------------
def start_scheduler():
scheduler = BackgroundScheduler()
scheduler.add_job(run_monthly_sync, "cron", day=1, hour=3) # día 1 a las 03:00
scheduler.start()