我在Google应用程序引擎服务器上有一个nodejs,但我需要在向用户发送响应后立即执行一些后台任务。我一直在使用下面的示例代码和nodejs child-process
,如所述here但它仅在本地测试时运行良好,但无法在 Google 应用引擎中运行。
let {fork} = require('child_process');
const forked = fork('./worker.js');
router.post('/endpoint', (req, res) => {
let options = req.body.options;
let isResponded = false;
async_helper.doWork(options).then((s) => {
let reply = JSON.stringify(s);
isResponded = true;
res.status(200).send(reply).end();
return null
}).then(value => {
//This is the long task of sending email
forked.send(value);
return null
}).catch(err => {
console.log(err);
if (!isResponded) {
let errr = err;
res.status(500).send('Failed').end();
}
return null
});
});
在生产应用中实现此类任务的最佳方式是什么?为什么子进程方法在 App Engine 中不起作用?我必须使用任务队列吗?
任务队列:
在出现上述问题后,我也尝试过任务队列,但自 this official doc 以来我无法区分任务队列处理程序端点和我的服务器端点还表明处理程序也在监听端口,就像我预先存在的 app.js
入口点一样。
那么这是否意味着同一个 app.js
也是任务队列的入口点?
或者这是否意味着任务队列入口点是应用程序引擎中的另一个“服务器”?
几个小时以来我都没有找到明确的指南。
最佳答案
经过对任务队列的多次试验和错误,我发现它是相同的app.js,它是接收任务队列调用的相同端点。
关于node.js - Google 云任务队列端点位于哪里?有人可以使用 nodejs 子进程来代替吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54203733/