【发布时间】:2019-04-21 20:16:15
【问题描述】:
我想在调用特定端点后延迟 1 小时或 5 小时触发我的 express 应用中的函数。
我尝试在我的 enpoint 的 then 链中使用一个简单的 setTimeout,并在我的控制器中使用类似这样的东西:
function(req, res, next) {
return mailer.sendEmail(req.user, 'firstEmail') // returns a promise
.then(function(emailResult) {
if (emailResult == 'ok') {
res.sendStatus(200).json({ message: 'You will receive an email in 1 hour' }) // set positive API json response
} else {
res.sendStatus(200).json({ message: 'You will receive an email in 5 hour' }) // set negative API json response
}
return emailResult
})
.then(function(emailResult){
return new Promise((resolve, reject) => {
setTimeout(function(){
return return mailer.sendEmail(req.user, 'secondEmail')
}, emailResult == 'ok' ? 60 * 60 * 1000 : 5 * 60 * 60 * 1000) // 1 hour or 5 hours
})
})
})
而且似乎有效,但我有两个主要问题:
- 如果我使用 (pm2 startOrReload prod.json) 重新启动我的应用程序的 pm2 进程,所有超时都会取消吗?
- 如果我为此系统安排了类似 600 次超时,我会遇到任何性能问题吗?
有类似的经验吗?
【问题讨论】:
-
您可能应该为此考虑某种专用队列,例如kue。
-
似乎是个不错的选择,谢谢建议
-
最后我决定用 kue 来管理作业并延迟它们
标签: node.js express promise settimeout