简体   繁体   English

如何使用 Bull.js 队列返回生成的图像?

[英]How to return a generated image with Bull.js queue?

My use case is this: I want to create screenshots of parts of a page.我的用例是这样的:我想创建页面部分内容的屏幕截图。 For technical reasons, it cannot be done on the client-side (see related question below) but needs puppeteer on the server.由于技术原因,它不能在客户端完成(请参阅下面的相关问题),但需要在服务器上使用 puppeteer。

As I'm running this on Heroku, I have the additional restriction of a quite small timeout window. Heroku recommends therefore to implement a queueing system based on bull.js and use worker processes for longer-running tasks as explained here .当我在 Heroku 上运行它时,我有一个非常小的超时 window 的额外限制。因此 Heroku 建议实现一个基于bull.js的排队系统,并使用worker进程来执行更长时间运行的任务,如此处所述

I have two endpoints (implemented with Express), one that receives a POST request with some configuration JSON, and another one that responds to GET when provided with a job identifier (slightly modified for brevity):我有两个端点(使用 Express 实现),一个接收配置为 JSON 的 POST 请求,另一个在提供作业标识符时响应 GET(为简洁起见略作修改):

This adds the job to the queue:这会将作业添加到队列中:

router.post('/', async function(req, res, next) {
    let job = await workQueue.add(req.body.chartConfig)
    res.json({ id: job.id })
})

This returns info about the job这将返回有关工作的信息

router.get('/:id', async(req, res) => {
    let id = req.params.id;
    let job = await workQueue.getJob(id);
    
    let state = await job.getState();
    let progress = job._progress;
    let reason = job.failedReason;
    res.json({ id, state, progress, reason });
})

In a different file:在不同的文件中:

const start = () => {
    let workQueue = new queue('work', REDIS_URL);
    workQueue.process(maxJobsPerWorker, getPNG)
}

const getPNG = async(job) => {

    const { url, width, height, chart: chartConfig, chartUrl } = job.data

    // ... snipped for brevity

    const png = await page.screenshot({
        type: 'png',
        fullPage: true
    })
    await page.close()
    job.progress(100)
    return Promise.resolve({ png })
}

// ...

throng({ count: workers, worker: start })

module.exports.getPNG = getPNG

The throng invocation at the end specifies the start function as the worker function to be called when picking a job from the queue.最后的throng调用指定start function 作为工作人员 function 从队列中挑选工作时调用。 start itself specifies getPNG to be called when treating a job. start本身指定在处理作业时要调用的getPNG

My question now is: how do I get the generated image ( png )?我现在的问题是:如何获取生成的图像 ( png )? I guess ideally I'd like to be able to call the GET endpoint above which would return the image, but I don't know how to pass the image object.我想理想情况下我希望能够调用上面的 GET 端点来返回图像,但我不知道如何传递图像 object。

As a more complex fall-back solution I could imagine posting the image to an image hosting service like imgur, and then returning the URL upon request of the GET endpoint.作为一个更复杂的回退解决方案,我可以想象将图像发布到像 imgur 这样的图像托管服务,然后根据 GET 端点的请求返回 URL。 But I'd prefer, if possible, to keep things simple.但如果可能的话,我宁愿让事情变得简单。

This question is a follow-up from this one:这个问题是这个问题的后续问题:

Issue with browser-side conversion SVG -> PNG 浏览器端转换问题 SVG -> PNG

I've opened a ticket on the GitHub repository of the bull project.我在bull项目的GitHub仓库开了工 The developers said that the preferred practice is to store the binary object somewhere else, and to add only the link metadata to the job's data store.开发人员表示,首选做法是将二进制 object 存储在其他地方,并仅将链接元数据添加到作业的数据存储中。

However, they also said that the storage limit of a job object appears to be 512 Mb.但是,他们还说作业 object 的存储限制似乎是 512 Mb。 So it is also quite possible to store an image of a reasonable size as a base64-encoded string.所以也很有可能将一个合理大小的图像存储为 base64 编码的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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