我第一次使用RabbitMQ,我必须误解一些简单的配置设置。 请注意,我现在正在本地运行应用程序时遇到此问题;我还没有尝试通过Heroku启动生产。
对于这个应用程序,每20秒我想在数据库中查找一些未发送的消息,并通过Twilio发送它们。如果我从下面的示例中遗漏了一些相关代码,请提前道歉。我已经遵循了所有Celery设置/配置说明。这是我目前的设置:
BROKER_URL = 'amqp://VflhnMEP:8wGLOrNBP.........Bhshs' # Truncated URL string
from datetime import timedelta
CELERYBEAT_SCHEDULE = {
'send_queued_messages_every_20_seconds': {
'task': 'comm.tasks.send_queued_messages',
'schedule': timedelta(seconds=20),
# 'schedule': crontab(seconds='*/20')
},
}
CELERY_TIMEZONE = 'UTC'
我很确定这些任务正在RabbitMQ中进行;这是我可以看到的所有累积消息的短划线:
应该每隔20秒调用一次'send_queued_messages'函数。
通信/ tasks.py
导入日期时间 来自celery.decorators import periodic_task
from comm.utils import get_user_mobile_number
from comm.api import get_twilio_connection, send_message
from dispatch.models import Message
@periodic_task
def send_queued_messages(run_every=datetime.timedelta(seconds=20)):
unsent_messages = Message.objects.filter(sent_success=False)
connection = get_twilio_connection()
for message in unsent_messages:
mobile_number = get_user_mobile_number(message=message)
try:
send_message(
connection=connection,
mobile_number=mobile_number,
message=message.raw_text
)
message.sent_success=True
message.save()
except BaseException as e:
raise e
pass
我很确定我在RabbitMQ或我的Heroku项目设置中有错误配置,但我不确定如何继续进行故障排除。当我运行'celery -A myproject beat'时一切似乎都在顺利进行。
(venv)josephs-mbp:myproject josephfusaro$ celery -A myproject beat
celery beat v3.1.18 (Cipater) is starting.
__ - ... __ - _
Configuration ->
. broker -> amqp://VflhnMEP:**@happ...Bhshs
. loader -> celery.loaders.app.AppLoader
. scheduler -> celery.beat.PersistentScheduler
. db -> celerybeat-schedule
. logfile -> [stderr]@%INFO
. maxinterval -> now (0s)
[2015-05-27 03:01:53,810: INFO/MainProcess] beat: Starting...
[2015-05-27 03:02:13,941: INFO/MainProcess] Scheduler: Sending due task send_queued_messages_every_20_seconds (comm.tasks.send_queued_messages)
[2015-05-27 03:02:34,036: INFO/MainProcess] Scheduler: Sending due task send_queued_messages_every_20_seconds (comm.tasks.send_queued_messages)
那么为什么不执行任务而不执行Celery *?
我的Procfile:
web:gunicorn myproject.wsgi --log-file - 工人:芹菜 - 一个myproject beat
*我已确认我的代码按预期执行,而不涉及Celery!
答案 0 :(得分:0)
特别感谢@MauroRocco推动我朝着正确的方向发展。我缺少的部分在本教程中得到了最好的解释:https://www.rabbitmq.com/tutorials/tutorial-one-python.html
注意:我需要修改教程中的一些代码以使用URLParameters,传入我的设置文件中定义的资源URL。
send.py和receive.py中唯一的一行是:
connection = pika.BlockingConnection(pika.URLParameters(BROKER_URL))
当然我们需要从settings.py
导入BROKER_URL变量from settings import BROKER_URL
<强> settings.py 强>
BROKER_URL = 'amqp://VflhnMEP:8wGLOrNBP...4.bigwig.lshift.net:10791/sdklsfssd'
<强> send.py 强>
import pika
from settings import BROKER_URL
connection = pika.BlockingConnection(pika.URLParameters(BROKER_URL))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
print " [x] Sent 'Hello World!'"
connection.close()
<强> receive.py 强>
import pika
from settings import BROKER_URL
connection = pika.BlockingConnection(pika.URLParameters(BROKER_URL))
channel = connection.channel()
channel.queue_declare(queue='hello')
print ' [*] Waiting for messages. To exit press CTRL+C'
def callback(ch, method, properties, body):
print " [x] Received %r" % (body,)
channel.basic_consume(callback,
queue='hello',
no_ack=True)
channel.start_consuming()