Managing Predefined Periodic Tasks and Their Schedules
This documentation explains how predefined periodic tasks are created and stored in the database using Django and Celery. It also includes a section for updating, viewing, enabling/disabling, and deleting these tasks via Django Admin.
Purpose of the Command
The store_periodic_tasks command stores predefined periodic tasks and their schedules in the database using Celery and django_celery_beat. This allows the tasks to run automatically based on predefined schedules.
Predefined Tasks and Their Schedules
The following table lists the predefined tasks and their schedules. These tasks are automatically created by the store_periodic_tasks management command.
| Task Name | Task Path | Schedule Type | Schedule Details |
|---|---|---|---|
activate_newsletters |
app_promotions.tasks.activate_newsletters |
Crontab | Runs every day at midnight (0 0 * * *) |
deactivate_old_newsletters |
app_promotions.tasks.deactivate_old_newsletters |
Crontab | Runs every day at midnight (0 0 * * *) |
send_newsletter_every_friday_8_45 |
app_promotions.tasks.send_newsletter |
Crontab | Runs every Friday at 8:45 PM (14 15 * * 5) |
coupon_code_scheduler |
checkout.tasks.coupon_code_scheduler |
Crontab | Runs every day at midnight (0 0 * * *) |
update_bestseller_metrics |
best_seller.tasks.update_bestseller_rankings |
Interval | Runs every 30 minutes (*/30) |
delete_expired_cart_items |
cart.tasks.delete_expired_cart_items |
Crontab | Runs every day at midnight (0 0 * * *) |
Explanation of Schedules
- Crontab Schedule:
-
This is used when a task needs to run on a specific time, day, or hour, such as every day at midnight or every Friday at a certain time. It uses a cron expression to define the schedule.
-
Interval Schedule:
- This is used when a task needs to run at fixed intervals, such as every 30 minutes.
Update Predefined Tasks and Their Schedules from Django Admin
To update the predefined tasks and their schedules, follow these steps:
-
Log in to Django Admin
- Open the Django Admin interface by navigating to `http://
/admin/ - Log in using your Django admin credentials.
- Open the Django Admin interface by navigating to `http://
-
Navigate to Periodic Tasks Section After logging in, go to the
django_celery_beatsection in theDjango Admin. This section contains three important models:- Periodic Tasks: The tasks that will run on the schedules defined.
- Crontab Schedules: The schedules that define when a task should run (e.g., every day at midnight).
- Interval Schedules: The schedules for tasks that should run at regular intervals (e.g., every 30 minutes).
-
Viewing and Editing Task Schedules
View and Edit Crontab Schedule:
- In the CrontabSchedules section, find the schedule for the task you want to modify.
- Example: You may find the schedule for
activate_newsletters(which runs at midnight every day). - Click on the schedule to view its details.
- Edit the schedule, for example, by changing the hour or minute, then save the changes.
View and Edit Interval Schedule:
- In the IntervalSchedules section, find the interval schedule for tasks like
update_bestseller_metrics(which runs every 30 minutes). - Click on the schedule to view and edit its details. For example, you can change it to run every hour instead of every 30 minutes.
- Save your changes.
Linking to Periodic Task:
- After modifying the schedule (whether it's a Crontab or Interval), go to the Periodic Tasks section.
- Find the task (e.g.,
update_bestseller_metrics). - Select the modified schedule from the dropdown (either the Crontab or Interval schedule).
- Save the task.
-
Viewing Task Execution Details (Last Run Time & Start Time)
In the Periodic Tasks section, each task will display:
- Start Time: The time when the task was scheduled to run.
- Last Run Time: The time when the task was last executed.
These fields help admins keep track of when the task last ran and when it is scheduled to run next.
-
Enabling/Disabling Tasks
Tasks can be enabled or disabled in Django Admin by toggling the Enabled checkbox in the Periodic Tasks section.
- Enabled: When this box is checked, the task will continue running according to its schedule.
- Disabled: When unchecked, the task will not run. It will be paused and will not execute until the checkbox is checked again.
Steps to Enable or Disable a Task:
- In the Periodic Tasks section, find the task you want to enable or disable.
- Click on the task to open its detail page.
- Toggle the Enabled checkbox to enable or disable the task.
- Click Save to confirm the change.