In the previous part, we explored the basic method to create scheduled tasks in Django-Celery-Beat through Django-Celery-Beat: How to Create Scheduled Tasks.
๐ Click the title to check out the previous post!
However, in real projects, it is often not enough to simply run tasks at the scheduled times.
- You may need to limit the execution to specific times,
- change the execution cycle dynamically,
- deactivate tasks under certain conditions, or
- set it to be automatically unscheduled after a certain date.
To handle such complex logic, this part will dive deeper into the PeriodicTask model of Django-Celery-Beat, examining various fields and settings! ๐
1๏ธโฃ Setting Up PeriodicTask Using update_or_create
In actual services, it's more common to modify a scheduled task based on user requests and changes in the environment rather than keeping it as is after creation.
Therefore, using the update_or_create()
method is usually more beneficial than PeriodicTask.objects.create()
.
๐ When using update_or_create()
,
- If the task already exists, it updates it
- If it does not exist, it creates a new one
This means that if a scheduled task with the same name exists, it can be overwritten, preventing unnecessary duplicate instances.
2๏ธโฃ The PeriodicTask.interval Field and IntervalSchedule Model
The PeriodicTask
of Django-Celery-Beat can set the interval of repeated executions through the interval
field.
This interval
field is linked to the IntervalSchedule
model, so you need to first create an IntervalSchedule
instance and then reference it in PeriodicTask
.
So, to register a scheduled task:
- First, create an
IntervalSchedule
instance - Then link the created
IntervalSchedule
toPeriodicTask
3๏ธโฃ IntervalSchedule Field Options
Field Name | Description | Example Values |
---|---|---|
every |
Numeric value of the repetition cycle | 10 , 30 , 2 |
period |
Unit of the repetition cycle | IntervalSchedule.SECONDS , IntervalSchedule.MINUTES , IntervalSchedule.HOURS , IntervalSchedule.DAYS |
4๏ธโฃ Example of Setting Up PeriodicTask Using update_or_create
from django_celery_beat.models import PeriodicTask, IntervalSchedule
import json
# Create IntervalSchedule (runs every 10 minutes)
schedule, created = IntervalSchedule.objects.get_or_create(
every=10,
period=IntervalSchedule.MINUTES
)
# Create or update PeriodicTask
task, created = PeriodicTask.objects.update_or_create(
name="send_reminder_email",
defaults={
"interval": schedule,
"task": "myapp.tasks.send_reminder_email",
"args": json.dumps([1]),
"enabled": True,
}
)
5๏ธโฃ Other Field Descriptions for PeriodicTask
Field Name | Description | Example |
---|---|---|
name |
Unique name of the task | "send_report_task" |
interval |
Execution interval (refers to IntervalSchedule ) |
schedule |
crontab |
To run at specific times (refers to CrontabSchedule ) |
None |
clocked |
Run it once at a specific time (refers to ClockedSchedule ) |
None |
task |
Celery task to be executed | "myapp.tasks.send_report" |
args |
Arguments to be passed to the function (JSON format) | json.dumps([123, "email@example.com"]) |
kwargs |
Keyword arguments to be passed to the function (JSON format) | json.dumps({"priority": "high"}) |
enabled |
Execution status | True or False |
one_off |
Runs only once and stops | True or False |
๐ If you set one_off=True
, the task will be disabled after it runs once but will not be deleted.
๐ Using the clocked
field allows you to schedule a task to run exactly once at a specific time.
๐ฏ Conclusion: Use update_or_create
to Manage Scheduled Tasks Flexibly!
๐ Using update_or_create()
when registering scheduled tasks in Django-Celery-Beat makes it easier to modify existing tasks
๐ The PeriodicTask.interval
field refers to IntervalSchedule
, so you need to first create an IntervalSchedule
instance and then connect it.
๐ When managing complex scheduled tasks, using fields like args
, kwargs
, enabled
, one_off
, clocked
allows for more flexible settings.
๐ฅ Coming Up Next
In this part, we learned about how to create and modify PeriodicTasks more flexibly.
In the next part, we plan to cover setting up scheduled tasks to run at specific times using Crontab and how to schedule tasks to run only once at a specific time using Clocked.
If you want to learn more about advanced Celery usage, stay tuned for the next part! ๐
Also, if you want to see more articles related to Celery on the blog,
please check the 'Related Posts' list below or search for "Celery" in the search box in the upper right corner! ๐
Add a New Comment