Files
igny8/backend/igny8_core/celery.py
2026-01-20 07:39:51 +00:00

125 lines
5.0 KiB
Python

"""
Celery configuration for IGNY8
"""
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', 'igny8_core.settings')
app = Celery('igny8_core')
# 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()
# Explicitly import tasks from igny8_core/tasks directory
app.autodiscover_tasks(['igny8_core.tasks'])
# Register billing tasks after Django is ready
@app.on_after_finalize.connect
def setup_billing_tasks(sender, **kwargs):
"""Import billing tasks after Django apps are loaded."""
try:
import igny8_core.business.billing.tasks.invoice_lifecycle
import igny8_core.business.billing.tasks.subscription_renewal
except ImportError as e:
import logging
logging.getLogger(__name__).warning(f"Failed to import billing tasks: {e}")
# Celery Beat schedule for periodic tasks
# NOTE: Do NOT set static task_id in options - it causes results to overwrite instead of creating history
app.conf.beat_schedule = {
# Monthly Credits
'replenish-monthly-credits': {
'task': 'igny8_core.modules.billing.tasks.replenish_monthly_credits',
'schedule': crontab(hour=0, minute=0, day_of_month=1), # First day of month at midnight
},
# Plan Limits Tasks
'reset-monthly-plan-limits': {
'task': 'reset_monthly_plan_limits',
'schedule': crontab(hour=0, minute=30), # Daily at 00:30 to check for period end
},
'check-approaching-limits': {
'task': 'check_approaching_limits',
'schedule': crontab(hour=9, minute=0), # Daily at 09:00 to warn users
},
# Subscription Renewal Tasks
# Stripe/PayPal: No advance notice (industry standard) - auto-pay happens on renewal day
# Bank Transfer: Invoice created 3 days before, reminders on Day 0 and Day +1
'create-bank-transfer-invoices': {
'task': 'billing.create_bank_transfer_invoices',
'schedule': crontab(hour=9, minute=0), # Daily at 09:00 - creates invoices 3 days before renewal
},
'process-subscription-renewals': {
'task': 'billing.process_subscription_renewals',
'schedule': crontab(hour=0, minute=5), # Daily at 00:05 - auto-pay for Stripe/PayPal
},
'send-renewal-day-reminders': {
'task': 'billing.send_renewal_day_reminders',
'schedule': crontab(hour=10, minute=0), # Daily at 10:00 - Day 0 reminder for bank transfer
},
'check-expired-renewals': {
'task': 'billing.check_expired_renewals',
'schedule': crontab(hour=0, minute=15), # Daily at 00:15 - expire after 7 days
},
# Send day-after reminders + reset credits for unpaid bank transfer renewals
'send-day-after-reminders': {
'task': 'billing.send_day_after_reminders',
'schedule': crontab(hour=9, minute=15), # Daily at 09:15 - Day +1 urgent + credit reset
},
# Credit Invoice Lifecycle Tasks
'send-credit-invoice-expiry-reminders': {
'task': 'billing.send_credit_invoice_expiry_reminders',
'schedule': crontab(hour=9, minute=30), # Daily at 09:30
},
'void-expired-credit-invoices': {
'task': 'billing.void_expired_credit_invoices',
'schedule': crontab(hour=0, minute=45), # Daily at 00:45
},
# Automation Tasks
'check-scheduled-automations': {
'task': 'automation.check_scheduled_automations',
'schedule': crontab(minute=5), # Every hour at :05
},
'check-test-triggers': {
'task': 'automation.check_test_triggers',
'schedule': crontab(minute='*/5'), # Every 5 minutes (task self-checks if any test mode enabled)
},
# Publishing Scheduler Tasks
'schedule-approved-content': {
'task': 'publishing.schedule_approved_content',
'schedule': crontab(minute=0), # Every hour at :00
},
'process-scheduled-publications': {
'task': 'publishing.process_scheduled_publications',
'schedule': crontab(minute='*/5'), # Every 5 minutes
},
# Maintenance: purge expired soft-deleted records daily at 3:15 AM
'purge-soft-deleted-records': {
'task': 'igny8_core.purge_soft_deleted',
'schedule': crontab(hour=3, minute=15),
},
# Daily application package backup at midnight
'daily-app-package-backup': {
'task': 'backup.create_daily_app_package',
'schedule': crontab(hour=0, minute=0), # Daily at 00:00
},
# Weekly cleanup of old packages (every Monday at 1 AM)
'weekly-package-cleanup': {
'task': 'backup.cleanup_old_packages',
'schedule': crontab(hour=1, minute=0, day_of_week=1), # Monday at 01:00
},
}
@app.task(bind=True, ignore_result=True)
def debug_task(self):
print(f'Request: {self.request!r}')