如何在部署到 Heroku 的 Node.js 环境中创建 Redis (Bull) 队列?

问题描述

我已经浏览了所有文档,但在找到正确实现的示例时遇到了一些麻烦。首先,我有 Heroku Redis 插件,如下所示:

https://i.stack.imgur.com/9kcXW.png

我在我的 Node.js 环境中安装了 Redis 和 Bull,并按如下方式设置了 Redis

const redis = require('redis');
const Queue = require('bull');
const redisClient = redis.createClient(process.env.REdis_URL,{
    tls: {
        rejectUnauthorized: false
    }
});
const bullQueue = new Queue('queue',process.env.REdis_URL)

我正在尝试将以下函数作为后台任务运行(这是 Heroku 对需要超过 0.5 秒才能完成的函数的建议):

app.post('/',async function(request,response) {
  const client = await pool.connect() //pool is just a node-postgres pool
  try {
    await client.query('BEGIN')
    let data = await function1(argumentList);
    await function2(data);
    await client.query('COMMIT')
  } catch(err) {
    await client.query('ROLLBACK')
    console.log(err)
  }
  try {
    await client.query('BEGIN')
    const setofItems = await function3()
    var array = []
    for (item of setofItems) {
      if (conditional) {
        array.push(item)
      }
    }
    await client.query('COMMIT')
    let job = await bullQueue.add()
    response.send(JSON.stringify(array))
  } catch(err) {
    await client.query('ROLLBACK')
    console.log(err)
  }
});

这个函数会做一些网页抓取、数据库调用和其他事情,所以就像我说的,它需要几秒钟才能完成。我应该如何更改我的函数以将请求添加后台队列,然后将 JSON.stringified 'array' 返回给用户。对不起,如果这是一个菜鸟 Redis 问题,但我已经查看了所有文档,但我真的不知道如何继续。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)