[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M70FT8W3XQ31JEYDF868J184":3,"skill-same:01M70FT8W3XQ31JEYDF868J184":81},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":16,"categories":17,"keywords":31,"publishedAt":52,"linkOnly":53,"markdownContent":54,"markdownBody":55,"frontMatter":56,"refName":59,"commitSha":60,"skillDirectory":61,"forksCount":62,"repositoryHtmlUrl":63,"repositoryHomepageUrl":6,"ownerHtmlUrl":64,"ownerBlogUrl":6,"ownerDescription":65,"updatedAt":66,"install":67,"resources":80},"01M70FT8W3XQ31JEYDF868J184","django-celery",null,"Django + Celery async task patterns — configuration, task design, beat scheduling, retries, canvas workflows, monitoring, and testing. Use when adding background jobs, scheduled tasks, or async processing to a Django app.","mdnaimul22\u002Fhuman-skills","mdnaimul22","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F64520358?v=4","01M70FH95MH2H796Q01TYSFZ03","Naimul","01M70FH96798CQR4YHA13F17GA",3,"MIT","https:\u002F\u002Fgithub.com\u002Fmdnaimul22\u002Fhuman-skills\u002Fblob\u002Fmain\u002Fskills\u002Fstorage\u002Faffaan-m_ECC\u002Fdjango-celery\u002FSKILL.md",[18,23,26],{"categoryNo":19,"categoryName":20,"taxonomyType":21,"taxonomyTypeDesc":22},"01JBSEEDCAT000000000000008","测试与质量","SCENARIO","使用场景",{"categoryNo":24,"categoryName":25,"taxonomyType":21,"taxonomyTypeDesc":22},"01JBSEEDCAT000000000000015","视觉设计",{"categoryNo":27,"categoryName":28,"taxonomyType":29,"taxonomyTypeDesc":30},"01JBSEEDCAT000000000000032","设计师","PROFESSION","适用职业",[32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51],"django","celery","django celery","scheduling","scheduled tasks","use","async","patterns","background jobs","monitoring","tasks","when","workflows","scheduled","retries","design","testing","canvas","processing","app","2026-11-10 10:12:30",false,"---\nname: django-celery\ndescription: Django + Celery async task patterns — configuration, task design, beat scheduling, retries, canvas workflows, monitoring, and testing. Use when adding background jobs, scheduled tasks, or async processing to a Django app.\nmetadata:\n  origin: ECC\n---\n\n# Django + Celery Async Task Patterns\n\nProduction-grade patterns for background task processing in Django using Celery with Redis or RabbitMQ.\n\n## When to Activate\n\n- Adding background jobs or async processing to a Django app\n- Implementing periodic\u002Fscheduled tasks\n- Offloading slow operations (email, PDF generation, API calls) from request cycle\n- Setting up Celery Beat for cron-like scheduling\n- Debugging task failures, retries, or queue backlogs\n- Writing tests for Celery tasks\n\n## Project Setup\n\n### Installation\n\n```bash\npip install 'celery[redis]' django-celery-results django-celery-beat\n```\n\n### `celery.py` — App Entrypoint\n\n```python\n# config\u002Fcelery.py\nimport os\nfrom celery import Celery\n\nos.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.development')\n\napp = Celery('myproject')\napp.config_from_object('django.conf:settings', namespace='CELERY')\napp.autodiscover_tasks()  # Discovers tasks.py in each INSTALLED_APP\n\n@app.task(bind=True, ignore_result=True)\ndef debug_task(self):\n    print(f'Request: {self.request!r}')\n```\n\n```python\n# config\u002F__init__.py\nfrom .celery import app as celery_app\n\n__all__ = ('celery_app',)\n```\n\n### Django Settings\n\n```python\n# config\u002Fsettings\u002Fbase.py\n\n# Broker (Redis recommended for production)\nCELERY_BROKER_URL = env('CELERY_BROKER_URL', default='redis:\u002F\u002Flocalhost:6379\u002F0')\nCELERY_RESULT_BACKEND = env('CELERY_RESULT_BACKEND', default='django-db')\n\n# Serialization\nCELERY_ACCEPT_CONTENT = ['json']\nCELERY_TASK_SERIALIZER = 'json'\nCELERY_RESULT_SERIALIZER = 'json'\n\n# Task behavior\nCELERY_TASK_TRACK_STARTED = True\nCELERY_TASK_TIME_LIMIT = 30 * 60        # Hard limit: 30 min\nCELERY_TASK_SOFT_TIME_LIMIT = 25 * 60   # Soft limit: sends SoftTimeLimitExceeded\nCELERY_WORKER_PREFETCH_MULTIPLIER = 1   # Prevent worker hoarding long tasks\nCELERY_TASK_ACKS_LATE = True            # Re-queue on worker crash\n\n# Result persistence\nCELERY_RESULT_EXPIRES = 60 * 60 * 24   # Keep results 24 hours\n\n# Beat scheduler (for periodic tasks)\nCELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'\n\n# Installed apps\nINSTALLED_APPS += [\n    'django_celery_results',\n    'django_celery_beat',\n]\n```\n\n### Running Workers\n\n```bash\n# Start worker (development)\ncelery -A config worker --loglevel=info\n\n# Start beat scheduler (periodic tasks)\ncelery -A config beat --loglevel=info --scheduler django_celery_beat.schedulers:DatabaseScheduler\n\n# Combined worker + beat (dev only, never production)\ncelery -A config worker --beat --loglevel=info\n\n# Production: multiple workers with concurrency\ncelery -A config worker --loglevel=warning --concurrency=4 -Q default,high_priority\n```\n\n## Task Design Patterns\n\n### Basic Task\n\n```python\n# apps\u002Fnotifications\u002Ftasks.py\nfrom celery import shared_task\nimport logging\n\nlogger = logging.getLogger(__name__)\n\n@shared_task(name='notifications.send_welcome_email')\ndef send_welcome_email(user_id: int) -> None:\n    \"\"\"Send welcome email to newly registered user.\"\"\"\n    from apps.users.models import User\n    from apps.notifications.services import EmailService\n\n    try:\n        user = User.objects.get(pk=user_id)\n    except User.DoesNotExist:\n        logger.warning('send_welcome_email: user %s not found', user_id)\n        return  # Idempotent — do not raise, task already impossible to complete\n\n    EmailService.send_welcome(user)\n    logger.info('Welcome email sent to user %s', user_id)\n```\n\n### Retryable Task\n\n```python\n@shared_task(\n    bind=True,\n    name='integrations.sync_to_crm',\n    max_retries=5,\n    default_retry_delay=60,       # seconds before first retry\n    autoretry_for=(ConnectionError, TimeoutError),\n    retry_backoff=True,           # exponential backoff\n    retry_backoff_max=600,        # cap at 10 minutes\n    retry_jitter=True,            # randomise to avoid thundering herd\n)\ndef sync_contact_to_crm(self, contact_id: int) -> dict:\n    \"\"\"Sync contact to external CRM with retry on transient failures.\"\"\"\n    from apps.crm.services import CRMClient\n\n    try:\n        result = CRMClient().sync(contact_id)\n        return result\n    except CRMClient.RateLimitError as exc:\n        # Specific retry delay from response header\n        raise self.retry(exc=exc, countdown=int(exc.retry_after))\n```\n\n### Idempotent Task Pattern\n\nDesign tasks so they can safely run multiple times with the same inputs:\n\n```python\n@shared_task(name='orders.mark_shipped')\ndef mark_order_shipped(order_id: int, tracking_number: str) -> None:\n    \"\"\"Mark order as shipped — safe to run multiple times.\"\"\"\n    from apps.orders.models import Order\n\n    updated = Order.objects.filter(\n        pk=order_id,\n        status=Order.Status.PROCESSING,    # Guard: only update if not already shipped\n    ).update(\n        status=Order.Status.SHIPPED,\n        tracking_number=tracking_number,\n    )\n\n    if not updated:\n        logger.info('mark_order_shipped: order %s already shipped or not found', order_id)\n```\n\n### Task with Soft Time Limit\n\n```python\nfrom celery.exceptions import SoftTimeLimitExceeded\n\n@shared_task(\n    bind=True,\n    name='reports.generate_pdf',\n    soft_time_limit=120,\n    time_limit=150,\n)\ndef generate_pdf_report(self, report_id: int) -> str:\n    \"\"\"Generate PDF report with graceful timeout handling.\"\"\"\n    from apps.reports.services import PDFGenerator\n\n    try:\n        path = PDFGenerator.build(report_id)\n        return path\n    except SoftTimeLimitExceeded:\n        # Clean up partial files before hard kill\n        PDFGenerator.cleanup(report_id)\n        raise\n```\n\n## Calling Tasks\n\n```python\nfrom datetime import timedelta\nfrom django.utils import timezone\n\n# Fire and forget (async)\nsend_welcome_email.delay(user.pk)\n\n# Schedule in the future\nsend_reminder.apply_async(args=[user.pk], countdown=3600)  # 1 hour from now\nsend_reminder.apply_async(args=[user.pk], eta=timezone.now() + timedelta(days=1))\n\n# Apply with queue routing\nsync_contact_to_crm.apply_async(args=[contact.pk], queue='high_priority')\n\n# Run synchronously (tests \u002F debugging only)\nresult = generate_pdf_report.apply(args=[report.pk])\n```\n\n## Beat Scheduling (Periodic Tasks)\n\n### Code-Defined Schedule\n\n```python\n# config\u002Fsettings\u002Fbase.py\nfrom celery.schedules import crontab\n\nCELERY_BEAT_SCHEDULE = {\n    'cleanup-expired-sessions': {\n        'task': 'users.cleanup_expired_sessions',\n        'schedule': crontab(hour=2, minute=0),   # 2am daily\n    },\n    'sync-inventory': {\n        'task': 'products.sync_inventory',\n        'schedule': 60.0,                         # every 60 seconds\n    },\n    'weekly-digest': {\n        'task': 'notifications.send_weekly_digest',\n        'schedule': crontab(day_of_week='monday', hour=8, minute=0),\n    },\n}\n```\n\n### Database-Defined Schedule (via django-celery-beat)\n\n```python\n# Manage periodic tasks from Django admin or code\nfrom django_celery_beat.models import PeriodicTask, CrontabSchedule\nimport json\n\nschedule, _ = CrontabSchedule.objects.get_or_create(\n    hour='*\u002F6', minute='0',\n    timezone='UTC',\n)\n\nPeriodicTask.objects.update_or_create(\n    name='Sync inventory every 6 hours',\n    defaults={\n        'crontab': schedule,\n        'task': 'products.sync_inventory',\n        'args': json.dumps([]),\n        'enabled': True,\n    }\n)\n```\n\n## Canvas: Chaining and Grouping Tasks\n\n```python\nfrom celery import chain, group, chord\n\n# Chain: run tasks sequentially, passing results\npipeline = chain(\n    fetch_data.s(source_id),\n    transform_data.s(),          # receives fetch_data result as first arg\n    load_to_warehouse.s(),\n)\npipeline.delay()\n\n# Group: run tasks in parallel\nparallel = group(\n    send_welcome_email.s(user_id)\n    for user_id in new_user_ids\n)\nparallel.delay()\n\n# Chord: parallel tasks + callback when all complete\nresult = chord(\n    group(process_chunk.s(chunk) for chunk in data_chunks),\n    aggregate_results.s(),       # called with list of chunk results\n)\nresult.delay()\n```\n\n## Error Handling and Dead Letter Queue\n\n```python\n# apps\u002Fcore\u002Ftasks.py\nfrom celery.signals import task_failure\n\n@task_failure.connect\ndef on_task_failure(sender, task_id, exception, args, kwargs, traceback, einfo, **kw):\n    \"\"\"Log all task failures to Sentry \u002F alerting.\"\"\"\n    import sentry_sdk\n    with sentry_sdk.new_scope() as scope:\n        scope.set_context('celery', {\n            'task': sender.name,\n            'task_id': task_id,\n            'args': args,\n            'kwargs': kwargs,\n        })\n        sentry_sdk.capture_exception(exception)\n```\n\n```python\n# Route failed tasks to dead-letter queue after max retries\n@shared_task(\n    bind=True,\n    max_retries=3,\n    name='payments.charge_card',\n)\ndef charge_card(self, order_id: int) -> None:\n    from apps.payments.models import Order, FailedCharge\n\n    try:\n        _do_charge(order_id)\n    except Exception as exc:\n        if self.request.retries >= self.max_retries:\n            # Persist to dead-letter table for manual review\n            FailedCharge.objects.create(\n                order_id=order_id,\n                error=str(exc),\n                task_id=self.request.id,\n            )\n            return  # Don't raise — task is permanently failed\n        raise self.retry(exc=exc)\n```\n\n## Testing Celery Tasks\n\n### Unit Testing (No Broker)\n\n```python\n# tests\u002Ftest_tasks.py\nimport pytest\nfrom unittest.mock import patch, MagicMock\nfrom apps.notifications.tasks import send_welcome_email\n\nclass TestSendWelcomeEmail:\n\n    @pytest.mark.django_db\n    def test_sends_email_to_existing_user(self, user):\n        with patch('apps.notifications.services.EmailService') as mock_email:\n            send_welcome_email(user.pk)\n            mock_email.send_welcome.assert_called_once_with(user)\n\n    @pytest.mark.django_db\n    def test_skips_missing_user_gracefully(self):\n        \"\"\"Should not raise when user is deleted between enqueue and execute.\"\"\"\n        send_welcome_email(99999)  # Non-existent user — must not raise\n```\n\n### Integration Testing with CELERY_TASK_ALWAYS_EAGER\n\n```python\n# config\u002Fsettings\u002Ftest.py\nCELERY_TASK_ALWAYS_EAGER = True      # Run tasks synchronously in tests\nCELERY_TASK_EAGER_PROPAGATES = True  # Re-raise exceptions from tasks\n\n# tests\u002Ftest_integration.py\n@pytest.mark.django_db\ndef test_registration_triggers_welcome_email(client):\n    with patch('apps.notifications.services.EmailService') as mock_email:\n        response = client.post('\u002Fapi\u002Fusers\u002F', {\n            'email': 'new@example.com',\n            'password': 'strongpass123',\n        })\n\n    assert response.status_code == 201\n    mock_email.send_welcome.assert_called_once()\n```\n\n### Testing Retries\n\n```python\n@pytest.mark.django_db\ndef test_task_retries_on_connection_error():\n    with patch('apps.crm.services.CRMClient.sync') as mock_sync:\n        mock_sync.side_effect = ConnectionError('timeout')\n\n        with pytest.raises(ConnectionError):\n            sync_contact_to_crm.apply(args=[1], throw=True)\n\n        assert mock_sync.call_count == 1  # First attempt only when eager\n```\n\n## Monitoring\n\n```bash\n# Inspect active workers and queues\ncelery -A config inspect active\ncelery -A config inspect stats\ncelery -A config inspect reserved\n\n# Check queue lengths (Redis)\nredis-cli llen celery\n\n# Flower: web-based real-time monitor\npip install flower\ncelery -A config flower --port=5555\n```\n\n## Anti-Patterns\n\n```python\n# BAD: Passing model instances — they may be stale by execution time\nsend_welcome_email.delay(user)        # Never pass ORM objects\nsend_welcome_email.delay(user.pk)     # Always pass PKs\n\n# BAD: Calling tasks synchronously in production views\nresult = generate_report.apply()      # Blocks the request thread\n\n# BAD: Non-idempotent task without guards\n@shared_task\ndef charge_and_fulfill(order_id):\n    order.charge()     # May charge twice if task retries!\n    order.fulfill()\n\n# GOOD: Idempotent with status guard\n@shared_task\ndef charge_and_fulfill(order_id):\n    order = Order.objects.select_for_update().get(pk=order_id)\n    if order.status != Order.Status.PENDING:\n        return  # Already processed\n    order.charge()\n    order.fulfill()\n```\n\n## Production Checklist\n\n| Check | Setting |\n|-------|---------|\n| Worker restarts on crash | `supervisord` or `systemd` unit |\n| `CELERY_TASK_ACKS_LATE = True` | Re-queue tasks on worker crash |\n| `CELERY_WORKER_PREFETCH_MULTIPLIER = 1` | Fair distribution of long tasks |\n| Separate queues per priority | `-Q default,high_priority,low_priority` |\n| `CELERY_TASK_SOFT_TIME_LIMIT` set | Graceful timeout before hard kill |\n| Sentry integration | Capture all `task_failure` signals |\n| Flower or other monitor | Visibility into queue depths |\n| Beat runs on single node only | Prevents duplicate scheduled task execution |\n\n## Related Skills\n\n- `django-patterns` — ORM, service layer, and project structure\n- `django-tdd` — Testing Django models, views, and services\n- `python-testing` — pytest configuration and fixtures\n","# Django + Celery Async Task Patterns\n\nProduction-grade patterns for background task processing in Django using Celery with Redis or RabbitMQ.\n\n## When to Activate\n\n- Adding background jobs or async processing to a Django app\n- Implementing periodic\u002Fscheduled tasks\n- Offloading slow operations (email, PDF generation, API calls) from request cycle\n- Setting up Celery Beat for cron-like scheduling\n- Debugging task failures, retries, or queue backlogs\n- Writing tests for Celery tasks\n\n## Project Setup\n\n### Installation\n\n```bash\npip install 'celery[redis]' django-celery-results django-celery-beat\n```\n\n### `celery.py` — App Entrypoint\n\n```python\n# config\u002Fcelery.py\nimport os\nfrom celery import Celery\n\nos.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.development')\n\napp = Celery('myproject')\napp.config_from_object('django.conf:settings', namespace='CELERY')\napp.autodiscover_tasks()  # Discovers tasks.py in each INSTALLED_APP\n\n@app.task(bind=True, ignore_result=True)\ndef debug_task(self):\n    print(f'Request: {self.request!r}')\n```\n\n```python\n# config\u002F__init__.py\nfrom .celery import app as celery_app\n\n__all__ = ('celery_app',)\n```\n\n### Django Settings\n\n```python\n# config\u002Fsettings\u002Fbase.py\n\n# Broker (Redis recommended for production)\nCELERY_BROKER_URL = env('CELERY_BROKER_URL', default='redis:\u002F\u002Flocalhost:6379\u002F0')\nCELERY_RESULT_BACKEND = env('CELERY_RESULT_BACKEND', default='django-db')\n\n# Serialization\nCELERY_ACCEPT_CONTENT = ['json']\nCELERY_TASK_SERIALIZER = 'json'\nCELERY_RESULT_SERIALIZER = 'json'\n\n# Task behavior\nCELERY_TASK_TRACK_STARTED = True\nCELERY_TASK_TIME_LIMIT = 30 * 60        # Hard limit: 30 min\nCELERY_TASK_SOFT_TIME_LIMIT = 25 * 60   # Soft limit: sends SoftTimeLimitExceeded\nCELERY_WORKER_PREFETCH_MULTIPLIER = 1   # Prevent worker hoarding long tasks\nCELERY_TASK_ACKS_LATE = True            # Re-queue on worker crash\n\n# Result persistence\nCELERY_RESULT_EXPIRES = 60 * 60 * 24   # Keep results 24 hours\n\n# Beat scheduler (for periodic tasks)\nCELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'\n\n# Installed apps\nINSTALLED_APPS += [\n    'django_celery_results',\n    'django_celery_beat',\n]\n```\n\n### Running Workers\n\n```bash\n# Start worker (development)\ncelery -A config worker --loglevel=info\n\n# Start beat scheduler (periodic tasks)\ncelery -A config beat --loglevel=info --scheduler django_celery_beat.schedulers:DatabaseScheduler\n\n# Combined worker + beat (dev only, never production)\ncelery -A config worker --beat --loglevel=info\n\n# Production: multiple workers with concurrency\ncelery -A config worker --loglevel=warning --concurrency=4 -Q default,high_priority\n```\n\n## Task Design Patterns\n\n### Basic Task\n\n```python\n# apps\u002Fnotifications\u002Ftasks.py\nfrom celery import shared_task\nimport logging\n\nlogger = logging.getLogger(__name__)\n\n@shared_task(name='notifications.send_welcome_email')\ndef send_welcome_email(user_id: int) -> None:\n    \"\"\"Send welcome email to newly registered user.\"\"\"\n    from apps.users.models import User\n    from apps.notifications.services import EmailService\n\n    try:\n        user = User.objects.get(pk=user_id)\n    except User.DoesNotExist:\n        logger.warning('send_welcome_email: user %s not found', user_id)\n        return  # Idempotent — do not raise, task already impossible to complete\n\n    EmailService.send_welcome(user)\n    logger.info('Welcome email sent to user %s', user_id)\n```\n\n### Retryable Task\n\n```python\n@shared_task(\n    bind=True,\n    name='integrations.sync_to_crm',\n    max_retries=5,\n    default_retry_delay=60,       # seconds before first retry\n    autoretry_for=(ConnectionError, TimeoutError),\n    retry_backoff=True,           # exponential backoff\n    retry_backoff_max=600,        # cap at 10 minutes\n    retry_jitter=True,            # randomise to avoid thundering herd\n)\ndef sync_contact_to_crm(self, contact_id: int) -> dict:\n    \"\"\"Sync contact to external CRM with retry on transient failures.\"\"\"\n    from apps.crm.services import CRMClient\n\n    try:\n        result = CRMClient().sync(contact_id)\n        return result\n    except CRMClient.RateLimitError as exc:\n        # Specific retry delay from response header\n        raise self.retry(exc=exc, countdown=int(exc.retry_after))\n```\n\n### Idempotent Task Pattern\n\nDesign tasks so they can safely run multiple times with the same inputs:\n\n```python\n@shared_task(name='orders.mark_shipped')\ndef mark_order_shipped(order_id: int, tracking_number: str) -> None:\n    \"\"\"Mark order as shipped — safe to run multiple times.\"\"\"\n    from apps.orders.models import Order\n\n    updated = Order.objects.filter(\n        pk=order_id,\n        status=Order.Status.PROCESSING,    # Guard: only update if not already shipped\n    ).update(\n        status=Order.Status.SHIPPED,\n        tracking_number=tracking_number,\n    )\n\n    if not updated:\n        logger.info('mark_order_shipped: order %s already shipped or not found', order_id)\n```\n\n### Task with Soft Time Limit\n\n```python\nfrom celery.exceptions import SoftTimeLimitExceeded\n\n@shared_task(\n    bind=True,\n    name='reports.generate_pdf',\n    soft_time_limit=120,\n    time_limit=150,\n)\ndef generate_pdf_report(self, report_id: int) -> str:\n    \"\"\"Generate PDF report with graceful timeout handling.\"\"\"\n    from apps.reports.services import PDFGenerator\n\n    try:\n        path = PDFGenerator.build(report_id)\n        return path\n    except SoftTimeLimitExceeded:\n        # Clean up partial files before hard kill\n        PDFGenerator.cleanup(report_id)\n        raise\n```\n\n## Calling Tasks\n\n```python\nfrom datetime import timedelta\nfrom django.utils import timezone\n\n# Fire and forget (async)\nsend_welcome_email.delay(user.pk)\n\n# Schedule in the future\nsend_reminder.apply_async(args=[user.pk], countdown=3600)  # 1 hour from now\nsend_reminder.apply_async(args=[user.pk], eta=timezone.now() + timedelta(days=1))\n\n# Apply with queue routing\nsync_contact_to_crm.apply_async(args=[contact.pk], queue='high_priority')\n\n# Run synchronously (tests \u002F debugging only)\nresult = generate_pdf_report.apply(args=[report.pk])\n```\n\n## Beat Scheduling (Periodic Tasks)\n\n### Code-Defined Schedule\n\n```python\n# config\u002Fsettings\u002Fbase.py\nfrom celery.schedules import crontab\n\nCELERY_BEAT_SCHEDULE = {\n    'cleanup-expired-sessions': {\n        'task': 'users.cleanup_expired_sessions',\n        'schedule': crontab(hour=2, minute=0),   # 2am daily\n    },\n    'sync-inventory': {\n        'task': 'products.sync_inventory',\n        'schedule': 60.0,                         # every 60 seconds\n    },\n    'weekly-digest': {\n        'task': 'notifications.send_weekly_digest',\n        'schedule': crontab(day_of_week='monday', hour=8, minute=0),\n    },\n}\n```\n\n### Database-Defined Schedule (via django-celery-beat)\n\n```python\n# Manage periodic tasks from Django admin or code\nfrom django_celery_beat.models import PeriodicTask, CrontabSchedule\nimport json\n\nschedule, _ = CrontabSchedule.objects.get_or_create(\n    hour='*\u002F6', minute='0',\n    timezone='UTC',\n)\n\nPeriodicTask.objects.update_or_create(\n    name='Sync inventory every 6 hours',\n    defaults={\n        'crontab': schedule,\n        'task': 'products.sync_inventory',\n        'args': json.dumps([]),\n        'enabled': True,\n    }\n)\n```\n\n## Canvas: Chaining and Grouping Tasks\n\n```python\nfrom celery import chain, group, chord\n\n# Chain: run tasks sequentially, passing results\npipeline = chain(\n    fetch_data.s(source_id),\n    transform_data.s(),          # receives fetch_data result as first arg\n    load_to_warehouse.s(),\n)\npipeline.delay()\n\n# Group: run tasks in parallel\nparallel = group(\n    send_welcome_email.s(user_id)\n    for user_id in new_user_ids\n)\nparallel.delay()\n\n# Chord: parallel tasks + callback when all complete\nresult = chord(\n    group(process_chunk.s(chunk) for chunk in data_chunks),\n    aggregate_results.s(),       # called with list of chunk results\n)\nresult.delay()\n```\n\n## Error Handling and Dead Letter Queue\n\n```python\n# apps\u002Fcore\u002Ftasks.py\nfrom celery.signals import task_failure\n\n@task_failure.connect\ndef on_task_failure(sender, task_id, exception, args, kwargs, traceback, einfo, **kw):\n    \"\"\"Log all task failures to Sentry \u002F alerting.\"\"\"\n    import sentry_sdk\n    with sentry_sdk.new_scope() as scope:\n        scope.set_context('celery', {\n            'task': sender.name,\n            'task_id': task_id,\n            'args': args,\n            'kwargs': kwargs,\n        })\n        sentry_sdk.capture_exception(exception)\n```\n\n```python\n# Route failed tasks to dead-letter queue after max retries\n@shared_task(\n    bind=True,\n    max_retries=3,\n    name='payments.charge_card',\n)\ndef charge_card(self, order_id: int) -> None:\n    from apps.payments.models import Order, FailedCharge\n\n    try:\n        _do_charge(order_id)\n    except Exception as exc:\n        if self.request.retries >= self.max_retries:\n            # Persist to dead-letter table for manual review\n            FailedCharge.objects.create(\n                order_id=order_id,\n                error=str(exc),\n                task_id=self.request.id,\n            )\n            return  # Don't raise — task is permanently failed\n        raise self.retry(exc=exc)\n```\n\n## Testing Celery Tasks\n\n### Unit Testing (No Broker)\n\n```python\n# tests\u002Ftest_tasks.py\nimport pytest\nfrom unittest.mock import patch, MagicMock\nfrom apps.notifications.tasks import send_welcome_email\n\nclass TestSendWelcomeEmail:\n\n    @pytest.mark.django_db\n    def test_sends_email_to_existing_user(self, user):\n        with patch('apps.notifications.services.EmailService') as mock_email:\n            send_welcome_email(user.pk)\n            mock_email.send_welcome.assert_called_once_with(user)\n\n    @pytest.mark.django_db\n    def test_skips_missing_user_gracefully(self):\n        \"\"\"Should not raise when user is deleted between enqueue and execute.\"\"\"\n        send_welcome_email(99999)  # Non-existent user — must not raise\n```\n\n### Integration Testing with CELERY_TASK_ALWAYS_EAGER\n\n```python\n# config\u002Fsettings\u002Ftest.py\nCELERY_TASK_ALWAYS_EAGER = True      # Run tasks synchronously in tests\nCELERY_TASK_EAGER_PROPAGATES = True  # Re-raise exceptions from tasks\n\n# tests\u002Ftest_integration.py\n@pytest.mark.django_db\ndef test_registration_triggers_welcome_email(client):\n    with patch('apps.notifications.services.EmailService') as mock_email:\n        response = client.post('\u002Fapi\u002Fusers\u002F', {\n            'email': 'new@example.com',\n            'password': 'strongpass123',\n        })\n\n    assert response.status_code == 201\n    mock_email.send_welcome.assert_called_once()\n```\n\n### Testing Retries\n\n```python\n@pytest.mark.django_db\ndef test_task_retries_on_connection_error():\n    with patch('apps.crm.services.CRMClient.sync') as mock_sync:\n        mock_sync.side_effect = ConnectionError('timeout')\n\n        with pytest.raises(ConnectionError):\n            sync_contact_to_crm.apply(args=[1], throw=True)\n\n        assert mock_sync.call_count == 1  # First attempt only when eager\n```\n\n## Monitoring\n\n```bash\n# Inspect active workers and queues\ncelery -A config inspect active\ncelery -A config inspect stats\ncelery -A config inspect reserved\n\n# Check queue lengths (Redis)\nredis-cli llen celery\n\n# Flower: web-based real-time monitor\npip install flower\ncelery -A config flower --port=5555\n```\n\n## Anti-Patterns\n\n```python\n# BAD: Passing model instances — they may be stale by execution time\nsend_welcome_email.delay(user)        # Never pass ORM objects\nsend_welcome_email.delay(user.pk)     # Always pass PKs\n\n# BAD: Calling tasks synchronously in production views\nresult = generate_report.apply()      # Blocks the request thread\n\n# BAD: Non-idempotent task without guards\n@shared_task\ndef charge_and_fulfill(order_id):\n    order.charge()     # May charge twice if task retries!\n    order.fulfill()\n\n# GOOD: Idempotent with status guard\n@shared_task\ndef charge_and_fulfill(order_id):\n    order = Order.objects.select_for_update().get(pk=order_id)\n    if order.status != Order.Status.PENDING:\n        return  # Already processed\n    order.charge()\n    order.fulfill()\n```\n\n## Production Checklist\n\n| Check | Setting |\n|-------|---------|\n| Worker restarts on crash | `supervisord` or `systemd` unit |\n| `CELERY_TASK_ACKS_LATE = True` | Re-queue tasks on worker crash |\n| `CELERY_WORKER_PREFETCH_MULTIPLIER = 1` | Fair distribution of long tasks |\n| Separate queues per priority | `-Q default,high_priority,low_priority` |\n| `CELERY_TASK_SOFT_TIME_LIMIT` set | Graceful timeout before hard kill |\n| Sentry integration | Capture all `task_failure` signals |\n| Flower or other monitor | Visibility into queue depths |\n| Beat runs on single node only | Prevents duplicate scheduled task execution |\n\n## Related Skills\n\n- `django-patterns` — ORM, service layer, and project structure\n- `django-tdd` — Testing Django models, views, and services\n- `python-testing` — pytest configuration and fixtures\n",{"name":5,"metadata":57,"description":7},{"origin":58},"ECC","main","74715deacdc69119d739f38916f8ee4f0f979ebf","skills\u002Fstorage\u002Faffaan-m_ECC\u002Fdjango-celery",0,"https:\u002F\u002Fgithub.com\u002Fmdnaimul22\u002Fhuman-skills","https:\u002F\u002Fgithub.com\u002Fmdnaimul22","AI Agents Workflows Automation | Specializing in Python, FastAPI, MCP Agents & Custom ERP.","2026-11-11 21:16:57",{"source":8,"skillName":5,"commands":68,"prompt":78,"zipAvailable":79},[69,72,75],{"tool":70,"command":71},"npx","npx skills add mdnaimul22\u002Fhuman-skills --skill django-celery",{"tool":73,"command":74},"bunx","bunx skills add mdnaimul22\u002Fhuman-skills --skill django-celery",{"tool":76,"command":77},"pnpm","pnpm dlx skills add mdnaimul22\u002Fhuman-skills --skill django-celery","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002Fmdnaimul22\u002Fhuman-skills\u002Fblob\u002Fmain\u002Fskills\u002Fstorage\u002Faffaan-m_ECC\u002Fdjango-celery\u002FSKILL.md",true,[],[82,90,98,105,112,119],{"skillNo":83,"name":84,"nameZh":6,"description":85,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":86,"categories":87,"keywords":88,"publishedAt":89},"01M767791C1KRP2ZRW2Z73VZ5Q","system-lifecycle-designer","Universal architectural methodology for designing granular, atomic, and sub-atomic lifecycles and end-to-end workflows for single functionality integrations across any domain (daemons, batch jobs, request transactions, resource pools). Provides patterns for deterministic identity, pre-commit validation, differential reconciliation, sticky leasing, state machines, transactional persistence, and closed-loop feedback.","https:\u002F\u002Fgithub.com\u002Fmdnaimul22\u002Fhuman-skills\u002Fblob\u002Fmain\u002Fskills\u002Fstorage\u002Fmy_skills\u002Fsystem-lifecycle-designer\u002FSKILL.md",[],[],"2026-11-12 13:55:43",{"skillNo":91,"name":92,"nameZh":6,"description":93,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":94,"categories":95,"keywords":96,"publishedAt":97},"01M748ABPN5F1FANHH6HKZQMXT","style-sampling","Make a new episode in a proven video style - Collage Explainer, Finance Explainer, Animated Science Explainer, 3D Brand Explainer, 3D True Crime - on the person's topic. Use for \"an episode of\", \"in the style of\", \"sample this style\", 「用这个风格做一集」. One video to copy is reverse-any-video.","https:\u002F\u002Fgithub.com\u002Fmdnaimul22\u002Fhuman-skills\u002Fblob\u002Fmain\u002Fskills\u002Fstorage\u002Fheygen-com_hyperframes\u002Fstyle-sampling\u002FSKILL.md",[],[],"2026-11-11 15:11:14",{"skillNo":99,"name":100,"nameZh":6,"description":101,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":102,"categories":103,"keywords":104,"publishedAt":97},"01M748A59V04PKW4HA0XQ81FGP","reverse-any-video","Rebuild any video file as an editable HyperFrames project - motion graphics, talking heads, live action or AI-generated footage - checked against the original frame by frame. Use to reverse, recreate, remake, replicate or copy a video, or 「复刻这个视频」「照着这个视频做一个」.","https:\u002F\u002Fgithub.com\u002Fmdnaimul22\u002Fhuman-skills\u002Fblob\u002Fmain\u002Fskills\u002Fstorage\u002Fheygen-com_hyperframes\u002Freverse-any-video\u002FSKILL.md",[],[],{"skillNo":106,"name":107,"nameZh":6,"description":108,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":109,"categories":110,"keywords":111,"publishedAt":97},"01M7487T50P55CSXMWXZ0G9KRG","typescript-patterns","TypeScript patterns for strict, type-safe, maintainable code in modern TypeScript and JavaScript applications. Covers strict compiler configuration, type design, narrowing, discriminated unions, generics, async and error handling, module boundaries, dependency hygiene, and type-level testing. Use when writing or reviewing TypeScript or JavaScript code.","https:\u002F\u002Fgithub.com\u002Fmdnaimul22\u002Fhuman-skills\u002Fblob\u002Fmain\u002Fskills\u002Fstorage\u002Faffaan-m_ECC\u002Ftypescript-patterns\u002FSKILL.md",[],[],{"skillNo":113,"name":114,"nameZh":6,"description":115,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":116,"categories":117,"keywords":118,"publishedAt":97},"01M7487ERZRGPC5NJ75077Y238","test-collaboration","Inventory and govern a project's test assets. Connect requirements, business rules, risks, bugs, and cross-boundary contracts to stable TEST-IDs and executable evidence in the repository's existing test registry or TESTS.md. Use for test inventory, test gap analysis, unit\u002Fintegration\u002Fcontract\u002FE2E\u002Fsmoke classification, bug-to-test regression protection, consumer\u002Fprovider contract testing, test-necessity review, and pre-delivery evidence review.","https:\u002F\u002Fgithub.com\u002Fmdnaimul22\u002Fhuman-skills\u002Fblob\u002Fmain\u002Fskills\u002Fstorage\u002Faffaan-m_ECC\u002Ftest-collaboration\u002FSKILL.md",[],[],{"skillNo":120,"name":121,"nameZh":6,"description":122,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":123,"categories":124,"keywords":125,"publishedAt":97},"01M748721J3GKR2WS6K0QWFC35","story-lifecycle","Manage a file-based epic → story → sprint development loop without requiring an external project management tool. Creates and tracks epics, breaks them into user stories, assigns them to sprints, and drives implementation story by story. Use when starting a new feature initiative or when the team has no Jira\u002FLinear\u002FTaiga but needs structured delivery tracking.","https:\u002F\u002Fgithub.com\u002Fmdnaimul22\u002Fhuman-skills\u002Fblob\u002Fmain\u002Fskills\u002Fstorage\u002Faffaan-m_ECC\u002Fstory-lifecycle\u002FSKILL.md",[],[]]