2024-10-28-【研发】nestjs动态管理定时任务

背景

在实际的业务场景中,定时任务的管理往往是动态的,比如:邮件定时推送、数据统计定时刷新等,需要根据业务的变化,动态的添加、删除、修改定时任务执行时间及任务内容。为了解决通过@Cron(‘0 10 16 * * 1-5’)注解来实现定时任务固定执行时间的局限性。提高灵活性,及可扩展性。

1. 相关依赖包

安装依赖pnpm add node-fetch
对应版本"node-fetch": "^2.6.9",

2. 企微机器人推送服务

2.1 创建定时任务服务

包含创建任务、删除任务、判断任务是否存在等功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* 企业微信机器人API
* @param robotKey
* @param body
*/
qyapi(robotKey, body) {
fetch(`https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=${robotKey}`, {
headers: {
accept: 'application/json, text/plain, */*',
'accept-language': 'zh-CN,zh;q=0.9',
'cache-control': 'no-cache',
'content-type': 'application/json',
pragma: 'no-cache',
},
body: JSON.stringify(body),
referrerPolicy: 'strict-origin-when-cross-origin',
method: 'POST',
mode: 'cors',
credentials: 'include',
})
.then(async (response) => await response.json())
.then((data) => {
return data;
})
.catch((e) => this.logger.error(e));
}

2024-10-28-【研发】nestjs动态管理定时任务
https://zhangyingxuan.github.io/2024-10-28-【研发】nestjs企微机器人推送/
作者
blowsysun
许可协议