我的烧瓶应用程序在本地运行良好,但是当我将它部署到 Heroku 时,它无法连接到 Redis 队列。
这是我得到的例外:
2015-04-06T09:01:45.586197+00:00 app[web.1]: Traceback (most recent call last):
2015-04-06T09:01:45.586189+00:00 app[web.1]: [2015-04-06 09:01:45 +0000] [9] [ERROR] Error handling request
2015-04-06T09:01:45.586201+00:00 app[web.1]: self.handle_request(listener, req, client, addr)
2015-04-06T09:01:45.586199+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 130, in handle
2015-04-06T09:01:45.586204+00:00 app[web.1]: respiter = self.wsgi(environ, resp.start_response)
2015-04-06T09:01:45.586202+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 171, in handle_request
2015-04-06T09:01:45.586206+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
2015-04-06T09:01:45.586207+00:00 app[web.1]: return self.wsgi_app(environ, start_response)
2015-04-06T09:01:45.586209+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
2015-04-06T09:01:45.586210+00:00 app[web.1]: response = self.make_response(self.handle_exception(e))
2015-04-06T09:01:45.586212+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
2015-04-06T09:01:45.586214+00:00 app[web.1]: reraise(exc_type, exc_value, tb)
2015-04-06T09:01:45.586215+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
2015-04-06T09:01:45.586237+00:00 app[web.1]: response = self.full_dispatch_request()
2015-04-06T09:01:45.586239+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
2015-04-06T09:01:45.586240+00:00 app[web.1]: rv = self.handle_user_exception(e)
2015-04-06T09:01:45.586242+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
2015-04-06T09:01:45.586243+00:00 app[web.1]: reraise(exc_type, exc_value, tb)
2015-04-06T09:01:45.586244+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
2015-04-06T09:01:45.586246+00:00 app[web.1]: rv = self.dispatch_request()
2015-04-06T09:01:45.586247+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
2015-04-06T09:01:45.586248+00:00 app[web.1]: return self.view_functions[rule.endpoint](**req.view_args)
2015-04-06T09:01:45.586250+00:00 app[web.1]: File "/app/mailr.py", line 80, in send_message
2015-04-06T09:01:45.586252+00:00 app[web.1]: job = q.enqueue_call(func=mailers.send_message, kwargs=request.json, result_ttl=86400) # Store result for 1 day
2015-04-06T09:01:45.586253+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/rq/queue.py", line 212, in enqueue_call
2015-04-06T09:01:45.586254+00:00 app[web.1]: return self.enqueue_job(job, at_front=at_front)
2015-04-06T09:01:45.586256+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/rq/queue.py", line 259, in enqueue_job
2015-04-06T09:01:45.586259+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/redis/client.py", line 1477, in sadd
2015-04-06T09:01:45.586257+00:00 app[web.1]: self.connection.sadd(self.redis_queues_keys, self.key)
2015-04-06T09:01:45.586260+00:00 app[web.1]: return self.execute_command('SADD', name, *values)
2015-04-06T09:01:45.586261+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/redis/client.py", line 570, in execute_command
2015-04-06T09:01:45.586263+00:00 app[web.1]: connection.send_command(*args)
2015-04-06T09:01:45.586264+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/redis/connection.py", line 556, in send_command
2015-04-06T09:01:45.586265+00:00 app[web.1]: self.send_packed_command(self.pack_command(*args))
2015-04-06T09:01:45.586267+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/redis/connection.py", line 532, in send_packed_command
2015-04-06T09:01:45.586268+00:00 app[web.1]: self.connect()
2015-04-06T09:01:45.586270+00:00 app[web.1]: File "/app/.heroku/python/lib/python2.7/site-packages/redis/connection.py", line 436, in connect
2015-04-06T09:01:45.586271+00:00 app[web.1]: raise ConnectionError(self._error_message(e))
2015-04-06T09:01:45.586273+00:00 app[web.1]: ConnectionError: Error 111 connecting to localhost:6379. Connection refused.
我已经安装了 RedisToGo 插件,并且我还检查了 -import redis
运行后我可以heroku run python
这里一定出了什么问题?我必须手动启动redis服务器吗?
编辑:所以看起来应用程序正在尝试在本地连接到 redis,但我再次使用 shell 来确定应用程序是否在 env 变量中设置了 redistogo url,结果确实如此。所以现在我必须弄清楚为什么应用程序使用本地 url。我正在使用此处给出的相同代码:
编辑2:所以我尝试直接在我的工作代码中硬编码redistogo url,但我仍然看到这个错误。所以工人代码可能不是问题。我不知道是什么。 https://devcenter.heroku.com/articles/python-rq#create-a-worker
编辑3:看起来我的问题很像这个人的:
如何使用 Flask 在 Heroku 上使用 RedisCloud 设置 RQ 工作者
编辑4:
这是我的代码库: https ://bitbucket.org/elbee19/mailr/src
这就是我现在使用工人的方式:
import os
import redis
from rq import Worker, Queue, Connection
listen = ['default']
#redis_url = os.getenv('REDISTOGO_URL', 'redis://localhost:6379')
redis_url = 'redis://redistogo:58cxxxxxxb7de6653c4a7915a97115@cobia.redistogo.com:9630'
conn = redis.from_url(redis_url)
if __name__ == '__main__':
with Connection(conn):
worker = Worker(list(map(Queue, listen)))
worker.work()
这是现在主烧瓶文件中的代码。这是引发错误的代码:
q = Queue(connection=Redis('redis://redistogo:58c9xxx351fb7de6653c4a7915a97115@cobia.redistogo.com','9630'))
.
.
job = q.enqueue_call(func=mailers.send_message, kwargs=request.json, result_ttl=86400) # Store result for 1 day