redis 在Heroku上使用Flask应用程序运行RQL

uttx8gqw  于 2023-10-15  发布在  Redis
关注(0)|答案(2)|浏览(117)

我正在尝试使用RedisToGo在Heroku上托管的Flask应用程序上运行RQscheduler
我已经建立了一个worker.py文件如下

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()

我的应用程序将工作与

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)

我在本地工作得很好,但是在Heroku上,我得到了以下错误

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

我猜问题出在我的程序文件上我不知道rqscheduler是否真的正常运行,或者RedisToGo是否正在运行redis服务器,而RQ调度程序没有正确连接到它。

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

bakd9h0s1#

所以当redistogo实际上不在localhost上时,rqscheduler正在使用localhost。
所以我更新了app.py,

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

和Procfile,

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

现在起作用了我所需要做的就是弄清楚如何将env变量添加到Procfile中。

apeeds0o

apeeds0o2#

对于Heroku上的Flask,可以传递Redis URL(我使用Heroku Data for Redis;如果你使用其他东西,你的env变量可能会有所不同):

scheduler: rqscheduler --url $REDIS_URL

PS:此外,不要忘记打开调度dyno,这将规定.

相关问题