HH/config/celery.py
2026-03-28 14:03:56 +03:00

168 lines
6.4 KiB
Python

"""
Celery configuration for PX360 project.
"""
import os
from celery import Celery
from celery.schedules import crontab
# Set the default Django settings module for the 'celery' program.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.dev")
# Apply zoneinfo compatibility patch for django-celery-beat with Python 3.12+
# This must be done before Celery app is created
from config.celery_scheduler import apply_tzcrontab_patch
apply_tzcrontab_patch()
app = Celery("px360")
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object("django.conf:settings", namespace="CELERY")
# Load task modules from all registered Django apps.
app.autodiscover_tasks()
# Celery Beat schedule for periodic tasks
app.conf.beat_schedule = {
# Process unprocessed integration events every 1 minute
"process-integration-events": {
"task": "apps.integrations.tasks.process_pending_events",
"schedule": crontab(minute="*/1"),
},
# Fetch patient data from HIS every 5 minutes
"fetch-his-surveys": {
"task": "apps.integrations.tasks.fetch_his_surveys",
"schedule": crontab(minute="*/5"),
"options": {
"expires": 300,
},
},
# TEST TASK - Fetch from JSON file (uncomment for testing, remove when done)
# 'test-fetch-his-surveys-from-json': {
# 'task': 'apps.integrations.tasks.test_fetch_his_surveys_from_json',
# 'schedule': crontab(minute='*/5'), # Every 5 minutes
# },
# Send pending scheduled surveys every 10 minutes
"send-pending-scheduled-surveys": {
"task": "apps.surveys.tasks.send_pending_scheduled_surveys",
"schedule": crontab(minute="*/10"), # Every 10 minutes
},
# Check for overdue complaints every 15 minutes
"check-overdue-complaints": {
"task": "apps.complaints.tasks.check_overdue_complaints",
"schedule": crontab(minute="*/15"),
},
# Check for overdue actions every 15 minutes
"check-overdue-actions": {
"task": "apps.px_action_center.tasks.check_overdue_actions",
"schedule": crontab(minute="*/15"),
},
# Send SLA reminders every hour
"send-sla-reminders": {
"task": "apps.complaints.tasks.send_sla_reminders",
"schedule": crontab(minute=0), # Every hour at minute 0
},
# Check for overdue explanation requests every 15 minutes
"check-overdue-explanation-requests": {
"task": "apps.complaints.tasks.check_overdue_explanation_requests",
"schedule": crontab(minute="*/15"),
},
# Send explanation reminders every hour
"send-explanation-reminders": {
"task": "apps.complaints.tasks.send_explanation_reminders",
"schedule": crontab(minute=0), # Every hour at minute 0
},
# Check for overdue inquiries every 15 minutes
"check-overdue-inquiries": {
"task": "apps.complaints.tasks.check_overdue_inquiries",
"schedule": crontab(minute="*/15"),
},
# Send inquiry SLA reminders every hour
"send-inquiry-sla-reminders": {
"task": "apps.complaints.tasks.send_inquiry_sla_reminders",
"schedule": crontab(minute=0), # Every hour at minute 0
},
# Check for overdue observations every 15 minutes
"check-overdue-observations": {
"task": "apps.observations.tasks.check_overdue_observations",
"schedule": crontab(minute="*/15"),
},
# Send observation SLA reminders every hour
"send-observation-sla-reminders": {
"task": "apps.observations.tasks.send_observation_sla_reminders",
"schedule": crontab(minute=0), # Every hour at minute 0
},
# Send onboarding reminders every hour
"send-onboarding-reminders": {
"task": "apps.accounts.tasks.send_onboarding_reminders",
"schedule": crontab(minute=30), # Every hour at minute 30
},
# Clean up expired invitations daily at midnight
"cleanup-expired-invitations": {
"task": "apps.accounts.tasks.cleanup_expired_invitations",
"schedule": crontab(hour=0, minute=0), # Daily at midnight
},
# Calculate daily KPIs at 1 AM
"calculate-daily-kpis": {
"task": "apps.analytics.tasks.calculate_daily_kpis",
"schedule": crontab(hour=1, minute=0),
},
# Fetch doctor ratings from HIS on the 1st of each month at 1 AM (before aggregation)
"fetch-his-doctor-ratings-monthly": {
"task": "apps.physicians.tasks.fetch_his_doctor_ratings_monthly",
"schedule": crontab(hour=1, minute=0, day_of_month=1),
},
# Calculate physician monthly ratings on the 1st of each month at 2 AM
"calculate-physician-ratings": {
"task": "apps.physicians.tasks.calculate_monthly_ratings",
"schedule": crontab(hour=2, minute=0, day_of_month=1),
},
# Scraping schedules
"scrape-youtube-hourly": {
"task": "social.tasks.scrape_youtube_comments",
"schedule": 3600.0, # Every hour (in seconds)
},
"scrape-facebook-every-6-hours": {
"task": "social.tasks.scrape_facebook_comments",
"schedule": 6 * 3600.0, # Every 6 hours
},
"scrape-instagram-daily": {
"task": "social.tasks.scrape_instagram_comments",
"schedule": crontab(hour=8, minute=0), # Daily at 8:00 AM
},
"scrape-twitter-every-2-hours": {
"task": "social.tasks.scrape_twitter_comments",
"schedule": 2 * 3600.0, # Every 2 hours
},
"scrape-linkedin-daily": {
"task": "social.tasks.scrape_linkedin_comments",
"schedule": crontab(hour=9, minute=0), # Daily at 9:00 AM
},
"scrape-google-reviews-daily": {
"task": "social.tasks.scrape_google_reviews",
"schedule": crontab(hour=10, minute=0), # Daily at 10:00 AM
},
# Commented out - individual platform tasks provide sufficient coverage
# 'scrape-all-platforms-daily': {
# 'task': 'social.tasks.scrape_all_platforms',
# 'schedule': crontab(hour=2, minute=0), # Daily at 2:00 AM
# },
# Analysis schedules
"analyze-comments-fallback": {
"task": "social.tasks.analyze_pending_comments",
"schedule": 30 * 60.0, # Every 30 minutes
"kwargs": {"limit": 100},
},
}
@app.task(bind=True, ignore_result=True)
def debug_task(self):
"""Debug task to test Celery is working."""
print(f"Request: {self.request!r}")