这是设置 - 使用celery的django项目和执行消息代理的CloudAMQP rabbitMQ worker。
我的芹菜/ RabbitMQ设置:
# RabbitMQ & Celery settings
BROKER_URL = 'ampq://guest:guest@localhost:5672/' # Understandably fake
BROKER_POOL_LIMIT = 1
BROKER_CONNECTION_TIMEOUT = 30
BROKER_HEARTBEAT = 30
CELERY_SEND_EVENTS = False
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
使用以下命令运行芹菜的docker容器:
bash -c 'cd django && celery -A pkm_main worker -E -l info --concurrency=3'
shared_task定义:
from __future__ import absolute_import
from celery import shared_task
@shared_task
def push_notification(user_id, message):
logging.critical('Push notifications sent')
return {'status': 'success'}
我确实在事情发生时调用它(我省略了一些代码,因为它似乎没有相关性):
from notificatons.tasks import push_notification
def like_this(self, **args):
# Do like stuff and then do .delay()
push_notification.delay(media.user.id, request.user.username + ' has liked your item')
所以当它运行时 - 一切看起来都很好而且花花公子 - 输出看起来像这样:
worker_1 | [2016-03-25 09:03:34,888: INFO/MainProcess] Received task: notifications.tasks.push_notification[8443bd88-fa02-4ea4-9bff-8fbec8c91516]
worker_1 | [2016-03-25 09:03:35,333: CRITICAL/Worker-1] Push notifications sent
worker_1 | [2016-03-25 09:03:35,336: INFO/MainProcess] Task notifications.tasks.push_notification[8443bd88-fa02-4ea4-9bff-8fbec8c91516] succeeded in 0.444933412999s: {'status': 'success'}
因此,从我收集的内容中,任务已经运行并正确执行,应该停止消息并且RabbitMQ应该停止。
但在我的RabbitMQ管理中,我看到消息不停发布和传递:
所以我从中收集到的是RabbitMQ正试图发送某种确认,失败并重试?有没有办法实际关闭此行为?
热烈欢迎所有帮助和建议。
编辑:忘了提到一些重要的东西 - 直到我调用push_notification.delay()消息选项卡为空,除了每隔30秒进出的心跳。只有在我调用.delay()之后才会发生这种情况。编辑2:CELERYBEAT_SCHEDULE设置(我尝试使用和不使用它们运行 - 没有区别,只是为了以防添加它们)
CELERYBEAT_SCHEDULE = {
"minutely_process_all_notifications": {
'task': 'transmissions.tasks.process_all_notifications',
'schedule': crontab(minute='*')
}
}
编辑3:添加了查看代码。我也没有使用CELERYBEAT_SCHEDULE。我只是将配置保留在代码中以供将来的计划任务使用
from notifications.tasks import push_notification
class MediaLikesView(BaseView):
def post(self, request, media_id):
media = self.get_object(media_id)
data = {}
data['media'] = media.id
data['user'] = request.user.id
serializer = MediaLikeSerializer(data=data)
if serializer.is_valid():
like = serializer.save()
push_notification.delay(media.user.id, request.user.username + ' has liked your item')
serializer = MediaGetLikeSerializer(like)
return self.get_mocked_pagination_response(status=status.HTTP_204_NO_CONTENT)
return self.get_mocked_pagination_response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
答案 0 :(得分:2)
--without-gossip --without-mingle --without-heartbeat
来禁用。
当您在命令行上禁用心跳时,也不要忘记设置BROKER_HEARTBEAT = None
,否则您将在30秒后断开连接。依靠TCP keepalive然后AMQP心跳,甚至更糟的是Celery自己的心跳,最常见的更好。