129 lines
3.5 KiB
Python
129 lines
3.5 KiB
Python
"""
|
|
Celery Application - ServiceManagerWeb Workers
|
|
|
|
Configuración principal de Celery para tareas asíncronas
|
|
"""
|
|
|
|
from celery import Celery
|
|
from celery.schedules import crontab
|
|
import os
|
|
from app.core.config import get_settings
|
|
from app.core.logging import setup_logging
|
|
|
|
# Setup logging
|
|
setup_logging()
|
|
|
|
# Get settings
|
|
settings = get_settings()
|
|
|
|
# Create Celery application
|
|
celery_app = Celery(
|
|
"servicemanager-workers",
|
|
broker=settings.CELERY_BROKER_URL,
|
|
backend=settings.CELERY_RESULT_BACKEND,
|
|
include=[
|
|
"app.tasks.email_tasks",
|
|
"app.tasks.sla_tasks",
|
|
"app.tasks.maintenance_tasks",
|
|
"app.tasks.notification_tasks"
|
|
]
|
|
)
|
|
|
|
# Configure Celery
|
|
celery_app.conf.update(
|
|
# Task settings
|
|
task_serializer="json",
|
|
accept_content=["json"],
|
|
result_serializer="json",
|
|
timezone="UTC",
|
|
enable_utc=True,
|
|
|
|
# Result backend settings
|
|
result_expires=3600, # 1 hour
|
|
result_persistent=True,
|
|
|
|
# Worker settings
|
|
worker_prefetch_multiplier=1,
|
|
worker_max_tasks_per_child=1000,
|
|
worker_disable_rate_limits=False,
|
|
|
|
# Task routing
|
|
task_routes={
|
|
"app.tasks.email_tasks.*": {"queue": "email"},
|
|
"app.tasks.sla_tasks.*": {"queue": "sla"},
|
|
"app.tasks.maintenance_tasks.*": {"queue": "maintenance"},
|
|
"app.tasks.notification_tasks.*": {"queue": "notifications"},
|
|
},
|
|
|
|
# Queue configuration
|
|
task_default_queue="default",
|
|
task_default_exchange="default",
|
|
task_default_routing_key="default",
|
|
|
|
# Beat schedule for periodic tasks
|
|
beat_schedule={
|
|
# Check SLA violations every 5 minutes
|
|
"check-sla-violations": {
|
|
"task": "app.tasks.sla_tasks.check_sla_violations",
|
|
"schedule": crontab(minute="*/5"),
|
|
},
|
|
|
|
# Send daily digest at 8:00 AM
|
|
"send-daily-digest": {
|
|
"task": "app.tasks.notification_tasks.send_daily_digest",
|
|
"schedule": crontab(hour=8, minute=0),
|
|
},
|
|
|
|
# Clean old logs weekly on Sunday at 2:00 AM
|
|
"cleanup-old-logs": {
|
|
"task": "app.tasks.maintenance_tasks.cleanup_old_logs",
|
|
"schedule": crontab(hour=2, minute=0, day_of_week=0),
|
|
},
|
|
|
|
# Generate weekly reports on Monday at 9:00 AM
|
|
"generate-weekly-reports": {
|
|
"task": "app.tasks.maintenance_tasks.generate_weekly_reports",
|
|
"schedule": crontab(hour=9, minute=0, day_of_week=1),
|
|
},
|
|
|
|
# Health check every minute
|
|
"worker-health-check": {
|
|
"task": "app.tasks.maintenance_tasks.health_check",
|
|
"schedule": crontab(minute="*/1"),
|
|
},
|
|
},
|
|
|
|
# Error handling
|
|
task_reject_on_worker_lost=True,
|
|
task_acks_late=True,
|
|
|
|
# Monitoring
|
|
worker_send_task_events=True,
|
|
task_send_sent_event=True,
|
|
|
|
# Security
|
|
worker_hijack_root_logger=False,
|
|
worker_log_format="[%(asctime)s: %(levelname)s/%(processName)s] %(message)s",
|
|
worker_task_log_format="[%(asctime)s: %(levelname)s/%(processName)s][%(task_name)s(%(task_id)s)] %(message)s",
|
|
)
|
|
|
|
# Optional: Configure SSL if needed
|
|
if settings.ENVIRONMENT == "production":
|
|
# Enable SSL for production
|
|
celery_app.conf.update(
|
|
broker_use_ssl=True,
|
|
redis_backend_use_ssl=True,
|
|
)
|
|
|
|
|
|
# Import all tasks to register them
|
|
from app.tasks import (
|
|
email_tasks,
|
|
sla_tasks,
|
|
maintenance_tasks,
|
|
notification_tasks
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
celery_app.start() |