Check Celery tasks for proper async event loop handling
Scans Celery task files for async pattern violations and offers to fix them.
/plugin marketplace add adelabdelgawad/full-stack/plugin install celery-async-pattern@full-stackIMPORTANT: When this command is invoked, you MUST actually SCAN the codebase and REPORT findings. Do NOT just describe what to check.
Use Glob to find task files:
{backend}/tasks/*.py
{backend}/celery_tasks/*.py
Look for @celery_app.task or @app.task decorators
@celery_app.task
def my_task():
return asyncio.run(async_function()) # CRASHES in production!
@celery_app.task
async def my_task(): # DON'T use async def for Celery tasks
return await async_function()
@celery_app.task
def my_task():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
return loop.run_until_complete(async_function()) # Missing try/finally!
@celery_app.task
def my_task():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
return loop.run_until_complete(async_function())
finally:
loop.close()
FORMAT YOUR OUTPUT EXACTLY LIKE THIS:
╔══════════════════════════════════════════════════════════════╗
║ CELERY ASYNC PATTERN COMPLIANCE REPORT ║
╚══════════════════════════════════════════════════════════════╝
📋 TASK FILES CHECKED
────────────────────────
✅ email_tasks.py - All tasks use correct pattern
❌ sync_tasks.py - Has dangerous patterns
❌ scheduler_tasks.py - Has dangerous patterns
📊 VIOLATIONS FOUND
────────────────────────
sync_tasks.py:
Line 15: @celery_app.task
Line 16: def sync_users_task():
Line 17: return asyncio.run(...) ❌ DANGEROUS: asyncio.run()
scheduler_tasks.py:
Line 28: @celery_app.task
Line 29: async def run_scheduled_job(): ❌ DANGEROUS: async def task
📊 SUMMARY
────────────────────────
Tasks checked: 8
Tasks with correct pattern: 5
Tasks with violations: 3
🔧 REQUIRED FIXES
────────────────────────
1. sync_tasks.py:sync_users_task (line 15)
- Replace asyncio.run() with event loop pattern
2. scheduler_tasks.py:run_scheduled_job (line 28)
- Convert from async def to sync def with event loop pattern
If violations are found, ask: "Would you like me to fix the Celery async pattern violations?"
If user agrees, apply this pattern:
import asyncio
from celery_app import celery_app
@celery_app.task(bind=True, max_retries=3)
def my_task(self, *args, **kwargs):
"""Task description."""
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
return loop.run_until_complete(_async_my_task(*args, **kwargs))
finally:
loop.close()
async def _async_my_task(*args, **kwargs):
"""Actual async implementation."""
# Your async code here
pass
When Celery runs with gevent (for concurrency), the event loop gets closed after the first async call. Subsequent calls fail with "Event loop is closed" error.
The correct pattern:
This command MUST:
asyncio.run, async def tasksDO NOT just tell the user to run a script. PERFORM THE CHECK.