gpt4 book ai didi

javascript - Node JS : Passing Response Object to Bull Queue for Server Side Events

转载 作者:行者123 更新时间:2023-12-04 12:23:19 25 4
gpt4 key购买 nike

我被困在一个架构决定上。我有 Node + Express 应用程序,它有一个用于上传文件的 API。上传完成后,响应关闭,上传的文件在 Bull Queue + Redis 的帮助下由 FFMPEG 批量处理。这种结构工作正常,但最近我开始测试服务器端事件,以便向最终用户提供有关处理的更新。但我无法将响应对象传递给 Bull Queue 以将定期更新从服务器写入用户。
1. 进口

import childProcess from 'child_process';
import Bull from 'bull'
const Queue = new Bull('background_job', {redis: {port: process.env.port, host: process.env.host, password: process.env.password}});
2.上传功能
const uploadVideo = async(req, res) => {
try{
const result = await authUser(req);
const result2 = await checkUploadFile(result);
const result3 = await insertPost(result2, res);
await Queue.add(result3.data, result3.opts)
} catch(err){
res.status(403).send(err);
}
}
3. promise
const authUser = (req) => {
return new Promise((resolve, reject) => {
//do some work
})
}

const checkUploadFile = (result) => {
return new Promise((resolve, reject) => {
//do some more work
})
}

const insertPost= (result, res) => {
return new Promise((resolve, reject) => {
//do final work
...........
//preparing server side events
const headers = {
'Content-Type': 'text/event-stream',
'Connection': 'keep-alive',
'Cache-Control': 'no-cache',
'Access-Control-Allow-Origin': '*'
};
res.writeHead(200, headers);
res.write(JSON.stringify({status: true, id: 1})); //testing server side events for the first time

//Let's continue to Bull
const data = {res: res} <- error here: TypeError: Converting circular structure to JSON
const opts = {removeOnComplete: true, removeOnFail: true}
resolve({data: data, opts: opts});
})
}
4.排队处理
Queue.process((job, done) => {
const res = job.data.res
childProcess.execFile('someScript.sh', [`some`, `arguments`], { stdio: ['pipe', 'pipe', 'ignore']}, (err, stderr, stdout) => {
if(err){
done(new Error("Failed: " + err))
res.write(JSON.stringify({status: true, id: 2})); //here using SSE
res.end()
} else {
done()
res.write(JSON.stringify({status: false})); //here using SSE
res.end()
}
})
})
5. PM2 记录的错误
TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Socket'
| property 'parser' -> object with constructor 'HTTPParser'
--- property 'socket' closes the circle
我尝试使用 JSON.stringify(res)将响应对象作为 JSON 传递,但这也不起作用。现在我正在考虑这种方法是否正确,或者我应该使用 Socket.io(这对于简单的服务器端事件来说是一种过度杀伤)
谢谢

最佳答案

你为什么还要写这一行:

const data = {res: res} <- error here: TypeError: Converting circular structure to JSON.
您仍然可以访问调用 insertPost 的 uploadVideo 函数中的响应对象。所以它可以简单地是:
await Queue.add(res, result3.opts).
例如:
const uploadVideo = async(req, res) => {
try{
const result = await authUser(req);
const result2 = await checkUploadFile(result);
const result3 = await insertPost(result2, res);
await Queue.add(res, result3.opts); // still have access to res
} catch(err){
res.status(403).send(err);
}
删除这一行:
const data = {res: res} <- error here: TypeError: Converting circular structure to JSON 
只需使用响应
Queue.process((res, done) => {
//const res = job.data.res
childProcess.execFile('someScript.sh', [`some`, `arguments`], { stdio: ['pipe', 'pipe', 'ignore']}, (err, stderr, stdout) => {
if(err){
done(new Error("Failed: " + err))
res.write(JSON.stringify({status: true, id: 2})); //here using SSE
res.end()
} else {
done()
res.write(JSON.stringify({status: false})); //here using SSE
res.end()
}
})
});
编辑:
我明白你的意思了。看了一下公牛模块。为什么你不能做这样的事情。
const uploadVideo = async(req, res) => {
try{
res.jobId = 0; // we need a way to know if job completed is our request const result = await authUser(req);
const result2 = await checkUploadFile(result);
const result3 = await insertPost(result2, res);
Queue.add({id: res.jobId, somedatafromresult3: 'result3.somedata' }, result3.opts);
Queue.on("completed", (err, data) => {
if (data.id === res.jobId) { // check to see if completed job is our one.
res.write(JSON.stringify(data)); //here using SSE
res.end()
}
console.log(data);
});
} catch(err){
res.status(403).send(err);
}
}
然后在您的流程函数中,只需返回将发出的数据。 IE
  videoQueue.process(function(job, done){
childProcess.execFile('someScript.sh', [`some`, `arguments`], { stdio: ['pipe', 'pipe', 'ignore']}, (err, stderr, stdout) => {
if(err){
done(err, {status: true, id: job.data.id});
} else {
done(null, {status: false, id: job.data.id});
}
})
})
;

关于javascript - Node JS : Passing Response Object to Bull Queue for Server Side Events,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65047576/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com