我的烧瓶应用程序在本地完美运行,但当我将其部署到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。我使用的是相同的代码:
编辑2:所以我尝试在我的工作代码中直接硬编码redistogo网址,我仍然看到这个错误。所以工人代码可能不是问题。我无法确定是什么。 https://devcenter.heroku.com/articles/python-rq#create-a-worker
编辑3:看起来我的问题很像这个人:
How to setup an RQ worker on Heroku with RedisCloud using Flask
编辑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
答案 0 :(得分:2)
看起来您需要将主要烧瓶代码更改为:
q = Queue(connection=conn)
或者如果你必须打开一个新连接,比如(替换为getenv):
q = Queue(connection=redis.Redis('redis://redistogo:58c9xxx351fb7de6653c4a7915a97115@cobia.redistogo.com','9630'))