node.js - 在一系列任务中产生更长延迟的更好方法是什么?
问题描述
我正在尝试构建一个工作流系统,它将处理一系列任务和延迟。可以从正在运行的工作流中更改或删除延迟。
在一系列任务中延长延迟的更好方法是什么?(比如 3-4 个月)。现在有两种方法在我脑海中萦绕:
预先计算并节省延迟时间。设置一个调度程序,将在特定时间间隔(可能 1 分钟)后重复检查延迟。这将进行大量的数据库查询,但延迟可以立即更改。
安排一个延迟的工作。这可以减少很多数据库查询,但问题是维护和更改这些长时间运行的作业的延迟。此外,这些作业需要在服务器崩溃或重启后幸存下来。
现在我不确定如何以更好的方式做到这一点,并且仍在研究它。如果有人有类似经历,请分享。
解决方案
You can store the tasks into the database, like :
{
_id: String,
status: Enum,
executionTime: timestamp,
}
When you declare a new task, push a new entry into the DB.
At your server start, or when a new task is declared, create a setTimeout
that will wake up your node.js when it's necessary.
Optimization
To avoid having X setTimeout
, with X the number of task to execute. Keep only one setTimeout
, with the time to wait equals to the closest task to execute.
For example, you have three task, one must run in 1 hour, one in 2 hour and one in 3 hour. Use a setTimeout
of 1 hour. When it get triggered, it execute the task 1 and then look at the remaining tasks to re-run.
推荐阅读
- typescript - Next.js - 将调用堆栈映射到 .ts 文件
- python - 从列表中的多个字典中获取字典值
- assembly - 获取用户字符串输入并以整数形式重新打印。ATT大会
- arrays - 在数组中添加多个元素
- javascript - 如何确定mxgraph中连接的哪一侧
- mysql - 无法在 MySQL LOAD 操作中使用 STR_TO_DATE() 读取日期时间数据
- html - HTML/CSS 下拉转换不起作用
- powershell - 为什么我无法在下面的代码中得到“真”的结果?
- extjs6-modern - ExtJS 6.5.3 - 在 Extjs 现代中找不到“->”的 xtype
- java - 在 Beyond Compare 中获取转换错误