[Fixed]-Django celery task run at once on startup of celery server

17👍

How about using celeryd_after_setup or celeryd_init signal?

Follwing example code from the documentation:

from celery.signals import celeryd_init

@celeryd_init.connect(sender='worker12@example.com')
def configure_worker12(conf=None, **kwargs):
    ...

-1👍

I found the way to do this. It has one negative side – impossible to specify current year and task will run after year again. But usually server restarts more often, then this period.

from celery.task import PeriodicTask

class InitialTasksStarter(PeriodicTask):
    starttime = datetime.now() + timedelta(minutes=1)
    run_every = crontab(month_of_year=starttime.month, day_of_month=starttime.day, hour=starttime.hour, minute=starttime.minute)

    def run(self, **kwargs):
        ....
        return True

Leave a comment