How do I restart celery workers gracefully?

前端 未结 7 1709
别跟我提以往
别跟我提以往 2020-12-07 13:47

While issuing a new build to update code in workers how do I restart celery workers gracefully?

Edit: What I intend to do is to something like this

相关标签:
7条回答
  • 2020-12-07 14:26

    You can do:

    celery multi restart w1 -A your_project -l info  # restart workers
    

    Example

    0 讨论(0)
  • 2020-12-07 14:28

    I have repeatedly tested the -HUP solution using an automated script, but find that about 5% of the time, the worker stops picking up new jobs after being restarted.

    A more reliable solution is:

    stop <celery_service>
    start <celery_service>

    which I have used hundreds of times now without any issues.

    From within Python, you can run:

    import subprocess
    service_name = 'celery_service'
    for command in ['stop', 'start']:
        subprocess.check_call(command + ' ' + service_name, shell=True)
    
    0 讨论(0)
  • 2020-12-07 14:29
    celery multi start 1 -A proj -l info -c4 --pidfile=/var/run/celery/%n.pid
    celery multi restart 1 --pidfile=/var/run/celery/%n.pid
    

    http://docs.celeryproject.org/en/latest/userguide/workers.html#restarting-the-worker

    0 讨论(0)
  • 2020-12-07 14:29

    What should happen to long running tasks? I like it this way: long running tasks should do their job. Don't interrupt them, only new tasks should get the new code.

    But this is not possible at the moment: https://groups.google.com/d/msg/celery-users/uTalKMszT2Q/-MHleIY7WaIJ

    0 讨论(0)
  • 2020-12-07 14:32

    You should look at Celery's autoreloading

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

    The new recommended method of restarting a worker is documented in here http://docs.celeryproject.org/en/latest/userguide/workers.html#restarting-the-worker

    $ celery multi start 1 -A proj -l info -c4 --pidfile=/var/run/celery/%n.pid
    $ celery multi restart 1 --pidfile=/var/run/celery/%n.pid
    

    According to http://ask.github.com/celery/userguide/workers.html#restarting-the-worker you can restart a worker sending a HUP signal

     ps auxww | grep celeryd | grep -v "grep" | awk '{print $2}' | xargs kill -HUP
    
    0 讨论(0)
提交回复
热议问题