Skip to content

Celery Tasks and Configuration Documentation

This document provides an explanation of the Celery tasks and configurations used in the system, outlining their functionality and scheduling details.


Celery Setup Overview

Celery is configured to manage asynchronous tasks within the project. The Celery app is initialized in celery.py, which:

  • Sets the default Django settings module.
  • Configures Celery to use Django settings under the CELERY namespace.
  • Auto-discovers tasks from installed Django apps.
  • Defines periodic task schedules using beat_schedule.

Tasks Description

1. Activate Newsletters

This task ensures that newsletters are activated based on their scheduled start and end dates: - Activates newsletters with a start date earlier than or equal to today and an end date in the future. - Handles newsletters without defined start and end dates by activating those updated within a defined activation period (default: 7 days). - Updates the is_active field to True for eligible newsletters.

Key Points: - The activation period is configurable via the CustomConfig table. - Helps manage dynamic newsletter activation.

2. Deactivate Old Newsletters

This task deactivates newsletters that are no longer valid: - Deactivates newsletters whose end date has passed. - Deactivates newsletters without start or end dates if they exceed the deactivation period (default: 7 days).

Key Points: - Ensures that outdated newsletters are marked inactive. - Avoids clutter in active newsletters.

3. Send Newsletters

This task handles the dispatch of newsletters to subscribers: - Verifies active newsletters and eligible subscribers. - Sends newsletters to subscribers via email. - Records each sent newsletter in the PublishNewsLetter model to prevent duplicate dispatch.

Key Points: - Integrates seamlessly with email-sending functionality. - Ensures subscribers only receive newsletters for which they are subscribed. - Handles both scheduled and dynamic newsletter dispatch.


Helper Function: Retrieve Configuration Values

A helper function is used to fetch custom configuration values from the CustomConfig table. If a value is not found, a default is returned. This is critical for dynamic control of activation and deactivation periods.


Task Scheduling

Celery Beat is used to schedule tasks at predefined intervals. The schedules are defined in beat_schedule and include:

Scheduled Tasks:

  • Activate Newsletters:
  • Runs daily at midnight.
  • Activates newsletters based on their defined criteria.

  • Deactivate Old Newsletters:

  • Runs daily at midnight.
  • Deactivates newsletters that no longer meet the criteria for being active.

  • Send Newsletters:

  • Runs every Friday at a specific time (configurable).
  • Dispatches eligible newsletters to subscribers.

  • Coupon Code Scheduler:

  • A placeholder for managing coupon code-related tasks, also runs daily at midnight.

Error Handling and Logging

Each task includes mechanisms to: - Log task execution results (e.g., count of activated or deactivated newsletters). - Handle unexpected errors gracefully to ensure system stability. - Prevent redundant processing by filtering data carefully.


Summary

The Celery tasks and configurations ensure efficient management of newsletters, including activation, deactivation, and delivery. The system is designed to be flexible, with configurable parameters stored in the database, enabling dynamic updates without requiring code changes.

For more details, refer to the specific task implementations in the tasks.py file.