0

Hi I am working on a project where i need celery beat to run long term periodic tasks. But the problem is that after starting the celery beat it is taking the specified time to run for the first time.

I want to fire the task on load for the first time and then run periodically.

I have seen this question on stackoverflow and this issue on GitHub, but didn't found a reliable solution.

Any suggestions on this one?

Daljit Singh
  • 113
  • 1
  • 9

2 Answers2

1

Since this does not seem possible I suggest a different approach. Call the task explicitly when you need and let the scheduler continue scheduling the tasks as usual. You can call the task on startup by using one the following methods (you probably need to take care of the multiple calls of the ready method if the task is not idempotent). Alternatively call the task from the command line by using celery call after your django server startup command.

zxzak
  • 8,985
  • 4
  • 27
  • 25
1

The best place to call it will most of the times be in the ready() function of the current apps AppConfig class:

from django.apps import AppConfig
from myapp.tasks import my_task

class RockNRollConfig(AppConfig):
    # ...

    def ready(self):
        my_task.delay(1, 2, 3)

Notice the use of .delay() which puts the invocation on the celery que and doesn't slow down starting the server.

See: https://docs.djangoproject.com/en/3.2/ref/applications/#django.apps.AppConfig and https://docs.celeryproject.org/en/stable/userguide/calling.html.

Rune Kaagaard
  • 6,643
  • 2
  • 38
  • 29