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! ๐Ÿš€

Django Celery-Beat PeriodicTask illustration

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(),

  1. If the task already exists, it updates it
  2. 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:

  1. First, create an IntervalSchedule instance
  2. Then link the created IntervalSchedule to PeriodicTask

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! ๐Ÿ˜Š