How to restart Celery gracefully without delaying tasks

前端 未结 7 530
面向向阳花
面向向阳花 2020-12-24 07:14

We use Celery with our Django webapp to manage offline tasks; some of these tasks can run up to 120 seconds.

Whenever we make any code modifications, we need to rest

相关标签:
7条回答
  • 2020-12-24 07:36

    I've recently fixed the bug with SIGHUP: https://github.com/celery/celery/pull/662

    0 讨论(0)
  • 2020-12-24 07:37

    Can you launch it with a custom pid file name. Possibly timestamped, and key off of that to know which PID to kill?

    CELERYD_PID_FILE="/var/run/celery/%n_{timestamp}.pid"

    ^I dont know the timestamp syntax but maybe you do or you can find it?

    then use the current system time to kill off any old pids and launch a new one?

    0 讨论(0)
  • 2020-12-24 07:41
    rm *.pyc
    

    This causes the updated tasks to be reloaded. I discovered this trick recently, I just hope there are no nasty side effects.

    0 讨论(0)
  • 2020-12-24 07:42

    I think you can try this:

    kill -s HUP ``cat /var/run/celeryd.pid`` 
    python manage.py celeryd --pidfile=/var/run/celeryd.pid
    

    HUP may recycle every free worker and leave executing workers keep running and HUP will let these workers be trusted. Then you can safely restart a new celery worker main process and workers. Old workers may be killed itself when task has been finished.

    I've use this way in our production and it seems safe now. Hope this can help you!

    0 讨论(0)
  • 2020-12-24 07:47

    Well you using SIGHUP (1) for warm shutdown of celery. I am not sure if it actually causes a warm shutdown. But SIGINT (2) would cause a warm shutdown. Try SIGINT in place of SIGHUP and then start celery manually in your script (I guess).

    0 讨论(0)
  • 2020-12-24 07:53

    celeryd has --autoreload option. If enabled, celery worker (main process) will detect changes in celery modules and restart all worker processes. In contrast to SIGHUP signal, autoreload restarts each process independently when the current executing task finishes. It means while one worker process is restarting the remaining processes can execute tasks.

    http://celery.readthedocs.org/en/latest/userguide/workers.html#autoreloading

    0 讨论(0)
提交回复
热议问题