My projects

Schedule email processing and cleanup

Use the command or an existing task queue once per minute, then verify that the real scheduler works.

Lesson 8 / 22 · Commentail 1.3.2

Configuring mail does not process the queue. The worker claims database jobs and removes expired temporary records. Starting the web server does not start it. First run it manually:

sh
python manage.py process_commentail

Run from the manage.py directory with the website's environment, settings and database. One invocation processes a pass and exits; it is not a persistent server.

Without a task queue

On Linux, cron can run the command once per minute. Prepare a log directory writable by the service user and substitute real paths:

cron
* * * * * cd /srv/site && /srv/site/.venv/bin/python manage.py process_commentail >> /var/log/site/commentail.log 2>&1

/srv/site is the project root and .venv/bin/python belongs to this site. Test the command as the intended user first. crontab -e edits that user's schedule. Do not run as root merely to bypass directory permissions.

Cron does not inherit variables from your interactive terminal. Use the hosting system's existing protected environment setup or a scheduler panel that supplies Environment settings. Do not put credentials in the cron line or published logs. If the environment cannot be passed reliably, use the existing service scheduler.

For local Windows practice, run the command manually. For a persistent schedule use Task Scheduler with a one-minute repeat: the environment's full Python path as program, manage.py process_commentail as arguments, and the manage.py directory as working directory. Configure the service account and its environment separately.

With an existing Celery installation

Do not install Celery solely for Commentail. In the installed host app myapp, add tasks.py:

python
from celery import shared_task
from commentail.background import process_pending


@shared_task(name="myapp.process_commentail")
def process_commentail_task():
    return process_pending(mail_limit=100)

With the standard CELERY namespace, extend the existing schedule:

python
# Add to the existing schedule; preserve its other entries.
CELERY_BEAT_SCHEDULE["commentail-every-minute"] = {
    "task": "myapp.process_commentail",
    "schedule": 60.0,
}

Replace myapp consistently. Initialize an empty dictionary first if no schedule exists. Keep both the Celery worker and one beat instance running. After restart, test execution through the actual broker. Celery Django integration.

Choose one schedule: command/hosting panel or host queue. Another task system can call process_pending(mail_limit=100) after Django initialization, with autocommit and outside transaction.atomic(). Its sent count means backend-accepted messages; removed counts expired unconfirmed comments erased, not every cleaned record.

Detect a stopped schedule

Submit a practice guest message and wait for an automatic pass. Monitor queue age and process failures. Temporary failures retry after 1, 5 and 15 minutes; template errors need correction and manual retry. A delayed worker never extends confirmation deadlines. An uncertain send can produce duplicates; a stable Message-ID does not guarantee exactly-once delivery.

Related lessons

Related documentation

Was this article useful?

Comments 0

Name: up to 40 characters.

After submitting, we will email you a link to confirm your email address.

Maximum 5000 characters.

No published comments yet.