""" Celery configuration for AgdarCentre project. This module initializes the Celery application and configures it to work with Django. It automatically discovers tasks from all installed apps. """ 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', 'AgdarCentre.settings') app = Celery('AgdarCentre') # 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 - Periodic Tasks app.conf.beat_schedule = { # Appointment reminders - Daily at 8:00 AM 'send-daily-appointment-reminders': { 'task': 'appointments.tasks.send_daily_appointment_reminders', 'schedule': crontab(hour=8, minute=0), }, # Check for no-shows - Every 2 hours 'check-appointment-no-shows': { 'task': 'appointments.tasks.check_no_shows', 'schedule': crontab(minute=0, hour='*/2'), }, # Generate next day schedule - Daily at 6:00 PM 'generate-next-day-schedule': { 'task': 'appointments.tasks.generate_daily_schedule', 'schedule': crontab(hour=18, minute=0), }, # Check overdue invoices - Daily at 9:00 AM 'check-overdue-invoices': { 'task': 'finance.tasks.check_overdue_invoices', 'schedule': crontab(hour=9, minute=0), }, # Generate weekly financial report - Monday at 9:00 AM 'generate-weekly-financial-report': { 'task': 'finance.tasks.generate_financial_report', 'schedule': crontab(hour=9, minute=0, day_of_week=1), 'kwargs': {'period': 'weekly'}, }, # Generate monthly financial report - 1st of month at 9:00 AM 'generate-monthly-financial-report': { 'task': 'finance.tasks.generate_financial_report', 'schedule': crontab(hour=9, minute=0, day_of_month=1), 'kwargs': {'period': 'monthly'}, }, # Check pending referrals - Daily at 10:00 AM 'check-pending-referrals': { 'task': 'referrals.tasks.check_pending_referrals', 'schedule': crontab(hour=10, minute=0), }, # Send referral statistics - Friday at 5:00 PM 'send-referral-statistics': { 'task': 'referrals.tasks.send_referral_statistics', 'schedule': crontab(hour=17, minute=0, day_of_week=5), }, # Sync lab results - Every 30 minutes 'sync-lab-results': { 'task': 'integrations.tasks.sync_lab_results', 'schedule': crontab(minute='*/30'), }, # Sync radiology results - Every 30 minutes 'sync-radiology-results': { 'task': 'integrations.tasks.sync_radiology_results', 'schedule': crontab(minute='*/30'), }, # Submit pending ZATCA invoices - Daily at 11:00 PM 'submit-pending-zatca-invoices': { 'task': 'integrations.tasks.submit_pending_zatca_invoices', 'schedule': crontab(hour=23, minute=0), }, } # Celery configuration app.conf.update( task_track_started=True, task_time_limit=30 * 60, # 30 minutes task_soft_time_limit=25 * 60, # 25 minutes worker_prefetch_multiplier=1, worker_max_tasks_per_child=1000, ) @app.task(bind=True, ignore_result=True) def debug_task(self): """Debug task to test Celery configuration.""" print(f'Request: {self.request!r}')