首页 > 解决方案 > 如何确保 Redis 服务器上的这个 Python RQ 任务正在执行?

问题描述

在 Flask 应用程序中,我有一个函数,它接受一个列表列表并将每个列表作为一行添加到 Excel 文件中。当请求不会超时时,此功能在开发服务器上运行良好。在这里,它被包裹在try/except块中。这就是我需要在 prod 服务器上将其作为排队任务调用的方式。

def make_excel_file(file_title, lists_to_print):

  try:
    job = get_current_job()

    # returns a list of lists
    # the first list is the header row (titles of attributes)
    # the following lists are all lists of the relevant object attributes
    # as well as some joined object attributes
    # (e.g. user ID, user email, drive year)
    rows = wish_list_rows(lists_to_print)

    wb = Workbook()
    ws = wb.active

    tempfile = NamedTemporaryFile()
    tempfile.name = file_title

    num_rows = len(rows)

    _set_task_progress(0, "excel_progress") #This results in a valid notification
                                            # so I know the function reaches at least this point


    for r in rows:
      ws.append(r)
      wb.save(tempfile)
      tempfile.seek(0)

      _set_task_progress(100.0 * (num_rows/rows.index(r)), "excel_progress") #the number never
                                                                             #updates, so the for loop
                                                                             #must never begin
    stream = tempfile.read()

    response = make_response(stream)
    response.headers['Content-Disposition'] = "attachment; filename={}".format(
      file_title)
    response.mimetype = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  except:
    flash("Lists could not be exported to Excel.", "warning")
  finally:
    _set_task_progress(100, "excel_progress")
  if response:
    return response

使用本地服务器的 Redis CLI 和命令redis-cli monitor,我可以看到以下输出:

~$ redis-cli monitor
OK
1594476959.432554 [0 [::1]:61680] "HGETALL" "rq:job:cfabaad5-b586-4aba-90b1-61addb5c9ea9"
1594476959.485371 [0 [::1]:61680] "MULTI"
1594476959.485408 [0 [::1]:61680] "LREM" "rq:queue:spur-hcd-tasks" "1" "cfabaad5-b586-4aba-90b1-61addb5c9ea9"
1594476959.487109 [0 [::1]:61680] "EXEC"
1594476976.799187 [0 [::1]:61680] "MULTI"
1594476976.801143 [0 [::1]:61680] "SADD" "rq:queues" "rq:queue:spur-hcd-tasks"
1594476976.803906 [0 [::1]:61680] "HSET" "rq:job:18be4075-8e3c-409c-a5cf-f82f3d11ba42" "status" "queued"
1594476976.803958 [0 [::1]:61680] "HMSET"
"rq:job:18be4075-8e3c-409c-a5cf-f82f3d11ba42"
"created_at" "2020-07-11T14:16:15.310435Z"
"data" "x\x9c\x94\xbd[\xcc / GIANT BYTE STRING / "
"origin" "name-of-queue"
"description" "app.tasks.make_excel_file(1, file_title='All lists', lists_to_print=[\n            column_1_data, (\"column_2_data\"), column_3_data, column_5_data.\n            , \n..." "enqueued_at" "2020-07-11T14:16:16.749772Z"
"started_at" "" "ended_at" ""
"timeout" "180" "status" "queued"
1594476976.841367 [0 [::1]:61680] "RPUSH" "rq:queue:spur-hcd-tasks" "18be4075-8e3c-409c-a5cf-f82f3d11ba42"
1594476976.841410 [0 [::1]:61680] "EXEC"
1594476977.433481 [0 [::1]:61680] "HGETALL" "rq:job:18be4075-8e3c-409c-a5cf-f82f3d11ba42"

我不确定如何解释这一点。我还想帮助了解如何进一步调试和查看本地 Redis 服务器上该功能中发生的情况。

编辑:我现在看到配置给应用程序的队列可能有问题。当我使用 get_current_job() 从 RQ 获得工作时,我可以访问它。但是,应用程序队列的注册表都是空的。如果应用程序的队列确实存在于 redis-cli 和 Python RQ 中的 Redis 服务器上,什么会阻止应用程序的队列执行该作业?

标签: pythonflaskredisredis-clipython-rq

解决方案


推荐阅读