Phase 0: Add monthly credit replenishment Celery Beat task
- Created billing/tasks.py with replenish_monthly_credits task - Task runs on first day of each month at midnight - Adds plan.included_credits to all active accounts - Creates CreditTransaction records for audit trail - Configured in celery.py beat_schedule - Handles errors gracefully and logs all operations
This commit is contained in:
@@ -3,6 +3,7 @@ Celery configuration for IGNY8
|
|||||||
"""
|
"""
|
||||||
import os
|
import os
|
||||||
from celery import Celery
|
from celery import Celery
|
||||||
|
from celery.schedules import crontab
|
||||||
|
|
||||||
# Set the default Django settings module for the 'celery' program.
|
# Set the default Django settings module for the 'celery' program.
|
||||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'igny8_core.settings')
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'igny8_core.settings')
|
||||||
@@ -18,6 +19,13 @@ app.config_from_object('django.conf:settings', namespace='CELERY')
|
|||||||
# Load task modules from all registered Django apps.
|
# Load task modules from all registered Django apps.
|
||||||
app.autodiscover_tasks()
|
app.autodiscover_tasks()
|
||||||
|
|
||||||
|
# Celery Beat schedule for periodic tasks
|
||||||
|
app.conf.beat_schedule = {
|
||||||
|
'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
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
@app.task(bind=True, ignore_result=True)
|
@app.task(bind=True, ignore_result=True)
|
||||||
def debug_task(self):
|
def debug_task(self):
|
||||||
|
|||||||
99
backend/igny8_core/modules/billing/tasks.py
Normal file
99
backend/igny8_core/modules/billing/tasks.py
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
"""
|
||||||
|
Celery tasks for billing operations
|
||||||
|
"""
|
||||||
|
import logging
|
||||||
|
from celery import shared_task
|
||||||
|
from django.utils import timezone
|
||||||
|
from django.db import transaction
|
||||||
|
from igny8_core.auth.models import Account
|
||||||
|
from .services import CreditService
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@shared_task(name='igny8_core.modules.billing.tasks.replenish_monthly_credits')
|
||||||
|
def replenish_monthly_credits():
|
||||||
|
"""
|
||||||
|
Replenish monthly credits for all active accounts.
|
||||||
|
Runs on the first day of each month at midnight.
|
||||||
|
|
||||||
|
For each active account with a plan:
|
||||||
|
- Adds plan.included_credits to account.credits
|
||||||
|
- Creates a CreditTransaction record
|
||||||
|
- Logs the replenishment
|
||||||
|
"""
|
||||||
|
logger.info("=" * 80)
|
||||||
|
logger.info("MONTHLY CREDIT REPLENISHMENT TASK STARTED")
|
||||||
|
logger.info(f"Timestamp: {timezone.now()}")
|
||||||
|
logger.info("=" * 80)
|
||||||
|
|
||||||
|
# Get all active accounts with plans
|
||||||
|
accounts = Account.objects.filter(
|
||||||
|
status='active',
|
||||||
|
plan__isnull=False
|
||||||
|
).select_related('plan')
|
||||||
|
|
||||||
|
total_accounts = accounts.count()
|
||||||
|
logger.info(f"Found {total_accounts} active accounts with plans")
|
||||||
|
|
||||||
|
replenished = 0
|
||||||
|
skipped = 0
|
||||||
|
errors = 0
|
||||||
|
|
||||||
|
for account in accounts:
|
||||||
|
try:
|
||||||
|
plan = account.plan
|
||||||
|
|
||||||
|
# Get monthly credits from plan
|
||||||
|
monthly_credits = plan.included_credits or plan.credits_per_month or 0
|
||||||
|
|
||||||
|
if monthly_credits <= 0:
|
||||||
|
logger.info(f"Account {account.id} ({account.name}): Plan has no included credits, skipping")
|
||||||
|
skipped += 1
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Add credits using CreditService
|
||||||
|
with transaction.atomic():
|
||||||
|
new_balance = CreditService.add_credits(
|
||||||
|
account=account,
|
||||||
|
amount=monthly_credits,
|
||||||
|
transaction_type='subscription',
|
||||||
|
description=f"Monthly credit replenishment - {plan.name} plan",
|
||||||
|
metadata={
|
||||||
|
'plan_id': plan.id,
|
||||||
|
'plan_name': plan.name,
|
||||||
|
'monthly_credits': monthly_credits,
|
||||||
|
'replenishment_date': timezone.now().isoformat()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
f"Account {account.id} ({account.name}): "
|
||||||
|
f"Added {monthly_credits} credits (balance: {new_balance})"
|
||||||
|
)
|
||||||
|
replenished += 1
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(
|
||||||
|
f"Account {account.id} ({account.name}): "
|
||||||
|
f"Failed to replenish credits: {str(e)}",
|
||||||
|
exc_info=True
|
||||||
|
)
|
||||||
|
errors += 1
|
||||||
|
|
||||||
|
logger.info("=" * 80)
|
||||||
|
logger.info("MONTHLY CREDIT REPLENISHMENT TASK COMPLETED")
|
||||||
|
logger.info(f"Total accounts: {total_accounts}")
|
||||||
|
logger.info(f"Replenished: {replenished}")
|
||||||
|
logger.info(f"Skipped: {skipped}")
|
||||||
|
logger.info(f"Errors: {errors}")
|
||||||
|
logger.info("=" * 80)
|
||||||
|
|
||||||
|
return {
|
||||||
|
'success': True,
|
||||||
|
'total_accounts': total_accounts,
|
||||||
|
'replenished': replenished,
|
||||||
|
'skipped': skipped,
|
||||||
|
'errors': errors
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user