【问题标题】:Running RQScheduler with Flask App on Heroku在 Heroku 上使用 Flask 应用程序运行 RQScheduler
【发布时间】:2020-02-28 05:19:18
【问题描述】:

我正在尝试使用 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. 

我猜这个问题是因为我的 Procfile。我不知道 rqscheduler 是否真的在正常运行,或者 RedisToGo 是否正在运行 redis-server 并且 RQscheduler 没有正确连接到它。

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

【问题讨论】:

    标签: python heroku flask redis redistogo


    【解决方案1】:

    所以当 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>
    

    现在它正在工作。我需要做的就是弄清楚如何将环境变量添加到 Procfile 中。

    【讨论】:

      猜你喜欢
      • 2015-01-05
      • 1970-01-01
      • 2013-02-01
      • 2017-04-27
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 2020-07-15
      • 2021-08-31
      相关资源
      最近更新 更多