Web Commerce - Celery Tasks
Location: api/nextango/apps/web_commerce/tasks.py
Last Updated: 2026-07-08
Overview
Background tasks for lifecycle management of web commerce entities. These periodic tasks maintain system hygiene by cleaning up expired sessions, abandoned carts, and old audit records.
Task Summary
| Task | Schedule | Purpose | Age Threshold |
|---|---|---|---|
cleanup_expired_checkout_sessions | Every 5 min | Release reservations from expired checkouts | Session expiry |
cleanup_abandoned_carts | Daily | Mark inactive carts as abandoned | 24 hours |
cleanup_stale_guest_sessions | Weekly | Delete old guest sessions | 30 days |
cleanup_old_reservation_contexts | Monthly | Archive old audit records | 90 days |
Task Reference
cleanup_expired_checkout_sessions
Source: tasks.py:13-33
Schedule: Every 5 minutes
Purpose: Release inventory reservations from checkout sessions that have expired.
Signature:
@shared_task(bind=True, max_retries=3)
def cleanup_expired_checkout_sessions(self):
result = LifecycleService.cleanup_expired_checkout_sessions()
return result
Returns:
{
'sessions_cleaned': 5,
'reservations_released': 12
}
Retry Policy: 3 retries with 60-second countdown
Why Every 5 Minutes:
- Checkout sessions typically expire in 15-30 minutes
- Frequent cleanup ensures inventory is released promptly
- Prevents inventory lockout from abandoned checkouts
cleanup_abandoned_carts
Source: tasks.py:36-53
Schedule: Daily
Purpose: Mark carts as abandoned after 24 hours of inactivity.
Signature:
@shared_task(bind=True, max_retries=3)
def cleanup_abandoned_carts(self):
result = LifecycleService.cleanup_abandoned_carts(age_hours=24)
return result
Returns:
{
'carts_abandoned': 15
}
Retry Policy: 3 retries with 5-minute countdown
What Gets Marked:
- Carts with
status=ACTIVE updated_atolder than 24 hours- No associated completed checkout
cleanup_stale_guest_sessions
Source: tasks.py:56-73
Schedule: Weekly
Purpose: Delete guest sessions that have been inactive for 30+ days.
Signature:
@shared_task(bind=True, max_retries=3)
def cleanup_stale_guest_sessions(self):
result = LifecycleService.cleanup_stale_guest_sessions(age_days=30)
return result
Returns:
{
'sessions_deleted': 150
}
Retry Policy: 3 retries with 5-minute countdown
Why 30 Days:
- Guest sessions don't have associated accounts
- After 30 days, cart recovery is unlikely
- Reduces database storage overhead
cleanup_old_reservation_contexts
Source: tasks.py:76-93
Schedule: Monthly
Purpose: Archive/delete old reservation audit records (CheckoutReservationContext).
Signature:
@shared_task(bind=True, max_retries=3)
def cleanup_old_reservation_contexts(self):
result = LifecycleService.cleanup_old_reservation_contexts(age_days=90)
return result
Returns:
{
'contexts_deleted': 5000
}
Retry Policy: 3 retries with 5-minute countdown
What Gets Deleted:
CheckoutReservationContextrecords older than 90 days- Only
releasedorcommittedstatus (not active) - Audit trail preserved for 3 months
Celery Beat Configuration
Add to settings/celery.py:
from celery.schedules import crontab
CELERY_BEAT_SCHEDULE = {
'cleanup-expired-checkouts': {
'task': 'nextango.apps.web_commerce.tasks.cleanup_expired_checkout_sessions',
'schedule': 300.0, # Every 5 minutes
},
'cleanup-abandoned-carts': {
'task': 'nextango.apps.web_commerce.tasks.cleanup_abandoned_carts',
'schedule': crontab(hour=3, minute=0), # Daily at 3 AM
},
'cleanup-stale-guest-sessions': {
'task': 'nextango.apps.web_commerce.tasks.cleanup_stale_guest_sessions',
'schedule': crontab(hour=4, minute=0, day_of_week=0), # Weekly, Sunday 4 AM
},
'cleanup-old-reservation-contexts': {
'task': 'nextango.apps.web_commerce.tasks.cleanup_old_reservation_contexts',
'schedule': crontab(hour=5, minute=0, day_of_month=1), # Monthly, 1st at 5 AM
},
}
Manual Execution
Run tasks manually via Django shell:
from nextango.apps.web_commerce.tasks import (
cleanup_expired_checkout_sessions,
cleanup_abandoned_carts,
cleanup_stale_guest_sessions,
cleanup_old_reservation_contexts
)
# Execute synchronously
result = cleanup_expired_checkout_sessions()
print(f"Cleaned {result['sessions_cleaned']} sessions")
# Execute asynchronously
cleanup_abandoned_carts.delay()
Monitoring
Check Task Status
from celery.result import AsyncResult
result = AsyncResult('task-id-here')
print(result.status) # PENDING, STARTED, SUCCESS, FAILURE
print(result.result) # Return value if SUCCESS
View Recent Executions
celery -A nextango inspect active
celery -A nextango inspect reserved