My flask app works perfectly locally but when I deploy it to Heroku, it can't connect to the Redis Queue.
THis is the exception I get:
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.
I've installed the RedisToGo add on, and I also checked - I can import redis
after running heroku run python
What must be going wrong here? Do I have to start redis server manually?
Edit: So looks like the app is trying to connect to redis locally, but I used the shell again to find out if the app has the redistogo url setup in the env variables, and turns out it does. So now I have to figure why is the app using the local url. I'm using the same code given here:
Edit 2: So I tried hardcode the redistogo url directly in my worker code, and I still see this error. So the worker code is probably not the problem. I can't figure what is. https://devcenter.heroku.com/articles/python-rq#create-a-worker
Edit 3: Looks like my problem is a lot like this guy's:
How to setup an RQ worker on Heroku with RedisCloud using Flask
Edit 4:
This is my codebase: https://bitbucket.org/elbee19/mailr/src
This is how I'm using the worker right now:
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()
And this is the code in the main flask file right now. This is the code that's throwing an error:
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
Looks like you need to change the main flask code to:
q = Queue(connection=conn)
Or if you must open a new connection, something like (replace with getenv):
q = Queue(connection=redis.Redis('redis://redistogo:58c9xxx351fb7de6653c4a7915a97115@cobia.redistogo.com','9630'))