简体   繁体   English

重新启动芹菜工作者与代理的消费者连接

[英]Restart celery worker consumer connection to broker

I've found an example of how to restart worker connection in celery docs ( http://docs.openquake.org/celery-2.4.6/userguide/workers.html#writing-your-own-remote-control-commands ): 我在celery文档中找到了一个如何重新启动工作程序连接的示例( http://docs.openquake.org/celery-2.4.6/userguide/workers.html#writing-your-own-remote-control-commands ) :

from celery.worker.control import Panel

@Panel.register
def reset_connection(panel):
    panel.logger.critical("Connection reset by remote control.")
    panel.consumer.reset_connection()
    return {"ok": "connection reset"}

But it's only available in Celery 3.0 and earlier. 但是它仅在Celery 3.0和更早版本中可用。 There is no such function in Consumer class on Celery 3.1. Celery 3.1的Consumer类中没有此类功能。

How do I re-establish connection to broker now? 现在如何重新建立与代理的连接?

The worker internals was rewritten to use "bootsteps" in 3.1 (see http://docs.celeryproject.org/en/latest/userguide/extending.html ) 在3.1中将工作人员内部构件重写为使用“ bootsteps”(请参阅http://docs.celeryproject.org/en/latest/userguide/extending.html

I think best way to reset the broker connection from a remote control command would be to use panel.consumer.connection.close() instead: 我认为从远程控制命令重置代理连接的最佳方法是使用panel.consumer.connection.close()代替:

from celery.worker.control import Panel, logger as control_logger

@Panel.register
def reset_connection(state):
    control_logger.critical("Connection reset by remote control.")
    state.consumer.connection.close()
    return {"ok": "connection reset"}

This will force the worker to restart all Consumer related bootsteps (see the graph in the link above) 这将迫使工作人员重新启动所有与消费者相关的引导步骤(请参见上面链接中的图)

Incidentally, the panel argument was renamed state but you can name it however you like. 顺便说一句, panel参数被重命名为state但是您可以随意命名。 The logger is no longer available on the state, so you have to import it. 该记录器在该州不再可用,因此您必须导入它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM