@celljs/schedule 4.6.1 → 4.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.zh-cn.md +38 -0
- package/package.json +2 -2
package/README.zh-cn.md
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Cell - 定时任务组件
|
|
2
|
+
|
|
3
|
+
Cell 后端的 Cron 任务,基于 [cron](https://github.com/kelektiv/node-cron) 包。
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @celljs/schedule
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import { Autowired } from '@celljs/core';
|
|
11
|
+
import { Cron, CronExpression, Scheduler } from '@celljs/schedule';
|
|
12
|
+
|
|
13
|
+
@Scheduler()
|
|
14
|
+
export class ReportScheduler {
|
|
15
|
+
@Autowired(ReportService)
|
|
16
|
+
protected readonly reports: ReportService;
|
|
17
|
+
|
|
18
|
+
@Cron(CronExpression.EVERY_5_MINUTES)
|
|
19
|
+
async refresh(): Promise<void> {
|
|
20
|
+
await this.reports.refresh();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
@Cron('0 30 2 * * *', { name: 'nightly', timeZone: 'Asia/Shanghai' })
|
|
24
|
+
nightly(): Promise<void> {
|
|
25
|
+
return this.reports.archive();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
像任何组件一样从后端 DI 模块导入该文件。
|
|
31
|
+
|
|
32
|
+
- `@Scheduler(cronJobOptions?, componentOption?)` 让该类成为一个组件,其 `@Cron` 方法是任务。它的 `cronJobOptions` 应用到该类的每个任务。
|
|
33
|
+
- `@Cron(cronTime, options?)`:`cronTime` 是一个可选包含秒字段的 cron 表达式(`'*/10 * * * * *'`)或 `Date`。`options` 是 cron 包的 `CronJob` 的选项(`timeZone`、`utcOffset`、`runOnInit`、...)加上 `name`。`CronExpression` 有常见的表达式(`EVERY_SECOND`、`EVERY_5_MINUTES`、`EVERY_DAY_AT_MIDNIGHT`、...)。
|
|
34
|
+
- 任务随应用启动而启动,随应用停止而停止。任务抛出的错误被记录;任务保持其调度。
|
|
35
|
+
- `schedule.disabled: true` 不启动任何任务,例如在 `cell.test.yml` 中。
|
|
36
|
+
- `SchedulerRegistry` 在运行时添加、获取和删除任务:`registry.add('cleanup', { cronTime: '0 * * * *', onTick: () => ... }).start()`。
|
|
37
|
+
|
|
38
|
+
任务在服务端进程内运行:只在它运行时才会运行。在无服务器 preset(`fc`、`scf`、`lambda`、`vercel`、`cloudflare`)上,实例由请求创建和停止,因此使用平台的定时器。
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@celljs/schedule",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.7.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cell-component"
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"watch": "tsc -b -w --preserveWatchOutput"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@celljs/core": "4.
|
|
35
|
+
"@celljs/core": "4.7.0",
|
|
36
36
|
"@types/cron": "^1.7.2",
|
|
37
37
|
"cron": "^1.8.2"
|
|
38
38
|
},
|