简体   繁体   中英

Running RQScheduler with Flask App on Heroku

I'm trying to run RQscheduler on a Flask app hosted on Heroku with RedisToGo.

I have set up a worker.py file as follows

import os   
import redis
from rq import Worker, Queue, Connection

listen = ['high', 'default', 'low']

redis_url = os.getenv('REDISTOGO_URL', 'redis://localhost:6379')

conn = redis.from_url(redis_url)

if __name__ == '__main__':
    with Connection(conn):
        worker = Worker(map(Queue, listen))
        worker.work()

My app queues the work with

from redis import Redis
from rq_scheduler import Scheduler

@app.route('/products/create', methods=['POST'])
def product_create_sort():

   scheduler = Scheduler(connection=Redis())
   scheduler.enqueue_in(timedelta(minutes=5), sort_collection, queue_data)

   return Response(status=200)

I have this working locally perfectly fine, but on Heroku I get the following error

redis.exceptions.ConnectionError: Error 111 connecting to localhost:6379. Connection refused. 

I'm guessing the issue is because of my Procfile. I don't know if rqscheduler is actually being run properly, or if RedisToGo is running the redis-server and RQscheduler isn't connected properly to it.

scheduler: rqscheduler
worker: python worker.py 
web: python app.py

So rqscheduler was working with localhost when redistogo isn't actually on localhost.

So I updated app.py with

scheduler = Scheduler(connection=Redis(host=<host>, port=<port>, db=0, password=<pass>))

and the Procfile with

scheduler: rqscheduler --host <host> --port <port> --password <pass>

Now it's working. All I need to do is figure out how to add env variables into a Procfile.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM