Question

I need to find how to specify a kind of initial celery task, that will start all other tasks in specially defined way. This initial task should be run immediately at once on celery server startup and never run again.

Était-ce utile?

La solution

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):
    ...

Autres conseils

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
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top