@athenna/cron 4.0.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/LICENSE.md +9 -0
- package/README.md +42 -0
- package/package.json +187 -0
- package/src/annotations/Scheduler.d.ts +14 -0
- package/src/annotations/Scheduler.js +38 -0
- package/src/commands/MakeSchedulerCommand.d.ts +15 -0
- package/src/commands/MakeSchedulerCommand.js +47 -0
- package/src/cron/CronBuilder.d.ts +123 -0
- package/src/cron/CronBuilder.js +185 -0
- package/src/cron/CronImpl.d.ts +72 -0
- package/src/cron/CronImpl.js +92 -0
- package/src/debug/index.d.ts +9 -0
- package/src/debug/index.js +10 -0
- package/src/facades/Cron.d.ts +10 -0
- package/src/facades/Cron.js +10 -0
- package/src/handlers/CronExceptionHandler.d.ts +26 -0
- package/src/handlers/CronExceptionHandler.js +59 -0
- package/src/index.d.ts +16 -0
- package/src/index.js +16 -0
- package/src/kernels/CronKernel.d.ts +28 -0
- package/src/kernels/CronKernel.js +95 -0
- package/src/providers/CronProvider.d.ts +13 -0
- package/src/providers/CronProvider.js +22 -0
- package/src/types/Context.d.ts +16 -0
- package/src/types/Context.js +9 -0
- package/src/types/CronHandler.d.ts +10 -0
- package/src/types/CronHandler.js +9 -0
- package/src/types/ScheduledTask.d.ts +18 -0
- package/src/types/ScheduledTask.js +9 -0
- package/src/types/index.d.ts +12 -0
- package/src/types/index.js +12 -0
- package/src/types/schedulers/SchedulerOptions.d.ts +76 -0
- package/src/types/schedulers/SchedulerOptions.js +9 -0
- package/templates/scheduler.edge +8 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 The Athenna Framework
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Cron ⏱️
|
|
2
|
+
|
|
3
|
+
> Athenna scheduler application. Built on top of node-cron.
|
|
4
|
+
|
|
5
|
+
[](https://github.com/athennaio?tab=followers)
|
|
6
|
+
[](https://github.com/AthennaIO/Cron/stargazers/)
|
|
7
|
+
|
|
8
|
+
<p>
|
|
9
|
+
<a href="https://www.buymeacoffee.com/athenna" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>
|
|
10
|
+
</p>
|
|
11
|
+
|
|
12
|
+
<p>
|
|
13
|
+
<img alt="GitHub language count" src="https://img.shields.io/github/languages/count/AthennaIO/Cron?style=for-the-badge&logo=appveyor">
|
|
14
|
+
|
|
15
|
+
<img alt="Repository size" src="https://img.shields.io/github/repo-size/AthennaIO/Cron?style=for-the-badge&logo=appveyor">
|
|
16
|
+
|
|
17
|
+
<img alt="License" src="https://img.shields.io/badge/license-MIT-brightgreen?style=for-the-badge&logo=appveyor">
|
|
18
|
+
|
|
19
|
+
<img alt="Commitizen" src="https://img.shields.io/badge/commitizen-friendly-brightgreen?style=for-the-badge&logo=appveyor">
|
|
20
|
+
</p>
|
|
21
|
+
|
|
22
|
+
<img src=".github/logo.svg" width="200px" align="right" hspace="30px" vspace="100px">
|
|
23
|
+
|
|
24
|
+
## Links
|
|
25
|
+
|
|
26
|
+
> For project documentation [click here](https://athenna.io). If something is not clear in the documentation please open an issue in the [documentation repository](https://github.com/athennaio/docs)
|
|
27
|
+
|
|
28
|
+
## Contributing
|
|
29
|
+
|
|
30
|
+
> If you want to contribute to this project, first read the [CONTRIBUTING.MD](https://github.com/AthennaIO/Cron/blob/develop/CONTRIBUTING.md) file. It will be a pleasure to receive your help.
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
34
|
+
<p align='center'>
|
|
35
|
+
With 💜 by <a href='https://github.com/AthennaIO'>Athenna community</a>
|
|
36
|
+
</p>
|
|
37
|
+
|
|
38
|
+
<p align='center'>
|
|
39
|
+
<a href='https://github.com/AthennaIO/Cron/graphs/contributors'>
|
|
40
|
+
<img src='https://contrib.rocks/image?repo=AthennaIO/Cron'/>
|
|
41
|
+
</a>
|
|
42
|
+
</p>
|
package/package.json
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@athenna/cron",
|
|
3
|
+
"version": "4.0.0",
|
|
4
|
+
"description": "Athenna scheduler application. Built on top of node-cron.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "João Lenon <lenon@athenna.io>",
|
|
7
|
+
"bugs": "https://github.com/AthennaIO/Cron/issues",
|
|
8
|
+
"repository": "https://github.com/AthennaIO/Cron.git",
|
|
9
|
+
"homepage": "https://github.com/AthennaIO/Cron#readme",
|
|
10
|
+
"keywords": [
|
|
11
|
+
"esm",
|
|
12
|
+
"athenna"
|
|
13
|
+
],
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=20.0.0"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "node node_modules/@athenna/tsconfig/src/build.js",
|
|
19
|
+
"lint:fix": "eslint \"{bin,src,configurer,tests}/**/*.ts\" --fix",
|
|
20
|
+
"test": "npm run --silent lint:fix && node --enable-source-maps --import=@athenna/tsconfig bin/test.ts",
|
|
21
|
+
"test:debug": "cross-env NODE_DEBUG=athenna:* node --inspect --enable-source-maps --import=@athenna/tsconfig bin/test.ts",
|
|
22
|
+
"test:coverage": "c8 npm run --silent test"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"src/*.js",
|
|
26
|
+
"src/*.d.ts",
|
|
27
|
+
"src/**/*.js",
|
|
28
|
+
"src/**/*.d.ts",
|
|
29
|
+
"templates",
|
|
30
|
+
"configurer"
|
|
31
|
+
],
|
|
32
|
+
"type": "module",
|
|
33
|
+
"main": "./src/index.js",
|
|
34
|
+
"types": "./src/index.d.ts",
|
|
35
|
+
"exports": {
|
|
36
|
+
".": "./src/index.js",
|
|
37
|
+
"./types": "./src/types/index.js",
|
|
38
|
+
"./package": "./package.json",
|
|
39
|
+
"./package.json": "./package.json",
|
|
40
|
+
"./testing/plugins": "./src/testing/plugins/index.js",
|
|
41
|
+
"./kernels/CronKernel": "./src/kernels/CronKernel.js",
|
|
42
|
+
"./handlers/CronExceptionHandler": "./src/handlers/CronExceptionHandler.js",
|
|
43
|
+
"./providers/CronProvider": "./src/providers/CronProvider.js",
|
|
44
|
+
"./commands/MakeSchedulerCommand": "./src/commands/MakeSchedulerCommand.js"
|
|
45
|
+
},
|
|
46
|
+
"imports": {
|
|
47
|
+
"#bin/*": "./bin/*.js",
|
|
48
|
+
"#bin": "./bin/index.js",
|
|
49
|
+
"#src/*": "./src/*.js",
|
|
50
|
+
"#src": "./src/index.js",
|
|
51
|
+
"#src/types": "./src/types/index.js",
|
|
52
|
+
"#src/debug": "./src/debug/index.js",
|
|
53
|
+
"#tests/*": "./tests/*.js",
|
|
54
|
+
"#tests": "./tests/index.js"
|
|
55
|
+
},
|
|
56
|
+
"dependencies": {
|
|
57
|
+
"node-cron": "^3.0.3"
|
|
58
|
+
},
|
|
59
|
+
"devDependencies": {
|
|
60
|
+
"@athenna/artisan": "^4.45.0",
|
|
61
|
+
"@athenna/common": "^4.46.0",
|
|
62
|
+
"@athenna/config": "^4.27.0",
|
|
63
|
+
"@athenna/ioc": "^4.27.0",
|
|
64
|
+
"@athenna/logger": "^4.29.0",
|
|
65
|
+
"@athenna/test": "^4.30.0",
|
|
66
|
+
"@athenna/tsconfig": "^4.19.0",
|
|
67
|
+
"@athenna/view": "^4.33.0",
|
|
68
|
+
"@types/node-cron": "^3.0.11",
|
|
69
|
+
"@typescript-eslint/eslint-plugin": "^7.18.0",
|
|
70
|
+
"@typescript-eslint/parser": "^7.18.0",
|
|
71
|
+
"cls-rtracer": "^2.6.3",
|
|
72
|
+
"commitizen": "^4.3.0",
|
|
73
|
+
"cz-conventional-changelog": "^3.3.0",
|
|
74
|
+
"eslint": "^8.57.0",
|
|
75
|
+
"eslint-config-prettier": "^8.10.0",
|
|
76
|
+
"eslint-config-standard": "^17.1.0",
|
|
77
|
+
"eslint-plugin-import": "^2.29.1",
|
|
78
|
+
"eslint-plugin-n": "^15.7.0",
|
|
79
|
+
"eslint-plugin-prettier": "^4.2.1",
|
|
80
|
+
"eslint-plugin-promise": "^6.6.0",
|
|
81
|
+
"husky": "^3.1.0",
|
|
82
|
+
"lint-staged": "^12.5.0",
|
|
83
|
+
"prettier": "^2.8.8"
|
|
84
|
+
},
|
|
85
|
+
"c8": {
|
|
86
|
+
"all": true,
|
|
87
|
+
"include": [
|
|
88
|
+
"src/**/*.ts"
|
|
89
|
+
],
|
|
90
|
+
"exclude": [
|
|
91
|
+
"src/types"
|
|
92
|
+
],
|
|
93
|
+
"reporter": [
|
|
94
|
+
"text-summary",
|
|
95
|
+
"html"
|
|
96
|
+
],
|
|
97
|
+
"report-dir": "./tests/coverage",
|
|
98
|
+
"check-coverage": true
|
|
99
|
+
},
|
|
100
|
+
"husky": {
|
|
101
|
+
"hooks": {
|
|
102
|
+
"prepare-commit-msg": "lint-staged && exec < /dev/tty && git cz --hook || true"
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
"lint-staged": {
|
|
106
|
+
"*.ts": [
|
|
107
|
+
"eslint --fix",
|
|
108
|
+
"git add"
|
|
109
|
+
],
|
|
110
|
+
"*.json": [
|
|
111
|
+
"prettier --write",
|
|
112
|
+
"git add"
|
|
113
|
+
]
|
|
114
|
+
},
|
|
115
|
+
"config": {
|
|
116
|
+
"commitizen": {
|
|
117
|
+
"path": "./node_modules/cz-conventional-changelog"
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
"prettier": {
|
|
121
|
+
"singleQuote": true,
|
|
122
|
+
"trailingComma": "none",
|
|
123
|
+
"arrowParens": "avoid",
|
|
124
|
+
"endOfLine": "lf",
|
|
125
|
+
"semi": false,
|
|
126
|
+
"printWidth": 80,
|
|
127
|
+
"overrides": [
|
|
128
|
+
{
|
|
129
|
+
"files": "tests/**/*",
|
|
130
|
+
"options": {
|
|
131
|
+
"printWidth": 120
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
]
|
|
135
|
+
},
|
|
136
|
+
"eslintIgnore": [],
|
|
137
|
+
"eslintConfig": {
|
|
138
|
+
"env": {
|
|
139
|
+
"es2021": true,
|
|
140
|
+
"node": true
|
|
141
|
+
},
|
|
142
|
+
"globals": {
|
|
143
|
+
"ioc": true,
|
|
144
|
+
"Env": true,
|
|
145
|
+
"Path": true,
|
|
146
|
+
"Config": true,
|
|
147
|
+
"container": true
|
|
148
|
+
},
|
|
149
|
+
"plugins": [
|
|
150
|
+
"prettier",
|
|
151
|
+
"@typescript-eslint"
|
|
152
|
+
],
|
|
153
|
+
"extends": [
|
|
154
|
+
"standard",
|
|
155
|
+
"eslint:recommended",
|
|
156
|
+
"plugin:prettier/recommended",
|
|
157
|
+
"plugin:@typescript-eslint/recommended",
|
|
158
|
+
"plugin:@typescript-eslint/eslint-recommended"
|
|
159
|
+
],
|
|
160
|
+
"parser": "@typescript-eslint/parser",
|
|
161
|
+
"rules": {
|
|
162
|
+
"camelcase": "off",
|
|
163
|
+
"dot-notation": "off",
|
|
164
|
+
"prettier/prettier": "error",
|
|
165
|
+
"no-useless-constructor": "off",
|
|
166
|
+
"@typescript-eslint/no-explicit-any": "off",
|
|
167
|
+
"@typescript-eslint/no-empty-function": "off",
|
|
168
|
+
"@typescript-eslint/no-unused-vars": [
|
|
169
|
+
"error",
|
|
170
|
+
{
|
|
171
|
+
"argsIgnorePattern": "^_",
|
|
172
|
+
"varsIgnorePattern": "^_",
|
|
173
|
+
"caughtErrorsIgnorePattern": "^_"
|
|
174
|
+
}
|
|
175
|
+
]
|
|
176
|
+
}
|
|
177
|
+
},
|
|
178
|
+
"athenna": {
|
|
179
|
+
"templates": {
|
|
180
|
+
"scheduler": "./templates/scheduler.edge"
|
|
181
|
+
},
|
|
182
|
+
"schedulers": [
|
|
183
|
+
"#tests/fixtures/schedulers/AnnotatedScheduler",
|
|
184
|
+
"#tests/fixtures/schedulers/HelloScheduler"
|
|
185
|
+
]
|
|
186
|
+
}
|
|
187
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @athenna/cron
|
|
3
|
+
*
|
|
4
|
+
* (c) João Lenon <lenon@athenna.io>
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
import 'reflect-metadata';
|
|
10
|
+
import type { SchedulerOptions } from '#src/types/schedulers/SchedulerOptions';
|
|
11
|
+
/**
|
|
12
|
+
* Create a scheduler inside the service provider.
|
|
13
|
+
*/
|
|
14
|
+
export declare function Scheduler(options: SchedulerOptions): ClassDecorator;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @athenna/cron
|
|
3
|
+
*
|
|
4
|
+
* (c) João Lenon <lenon@athenna.io>
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
import 'reflect-metadata';
|
|
10
|
+
import { debug } from '#src/debug';
|
|
11
|
+
import { Options } from '@athenna/common';
|
|
12
|
+
import { Annotation } from '@athenna/ioc';
|
|
13
|
+
/**
|
|
14
|
+
* Create a scheduler inside the service provider.
|
|
15
|
+
*/
|
|
16
|
+
export function Scheduler(options) {
|
|
17
|
+
return (target) => {
|
|
18
|
+
options = Options.create(options, {
|
|
19
|
+
name: target.name,
|
|
20
|
+
scheduled: true,
|
|
21
|
+
runOnInit: false,
|
|
22
|
+
recoverMissedExecutions: false,
|
|
23
|
+
alias: `App/Cron/Schedulers/${target.name}`,
|
|
24
|
+
type: 'transient'
|
|
25
|
+
});
|
|
26
|
+
debug('Registering scheduler metadata for the service container %o', {
|
|
27
|
+
name: target.name,
|
|
28
|
+
...options
|
|
29
|
+
});
|
|
30
|
+
if (ioc.has(options.alias) ||
|
|
31
|
+
ioc.has(options.camelAlias) ||
|
|
32
|
+
ioc.has(options.name)) {
|
|
33
|
+
debug('Skipping registration, scheduler is already registered.');
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
Annotation.defineMeta(target, options);
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @athenna/cron
|
|
3
|
+
*
|
|
4
|
+
* (c) João Lenon <lenon@athenna.io>
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
import { BaseCommand } from '@athenna/artisan';
|
|
10
|
+
export declare class MakeSchedulerCommand extends BaseCommand {
|
|
11
|
+
name: string;
|
|
12
|
+
static signature(): string;
|
|
13
|
+
static description(): string;
|
|
14
|
+
handle(): Promise<void>;
|
|
15
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @athenna/cron
|
|
3
|
+
*
|
|
4
|
+
* (c) João Lenon <lenon@athenna.io>
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
10
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
11
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
12
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
13
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
14
|
+
};
|
|
15
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
16
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
17
|
+
};
|
|
18
|
+
import { Path } from '@athenna/common';
|
|
19
|
+
import { BaseCommand, Argument } from '@athenna/artisan';
|
|
20
|
+
export class MakeSchedulerCommand extends BaseCommand {
|
|
21
|
+
static signature() {
|
|
22
|
+
return 'make:scheduler';
|
|
23
|
+
}
|
|
24
|
+
static description() {
|
|
25
|
+
return 'Make a new scheduler file.';
|
|
26
|
+
}
|
|
27
|
+
async handle() {
|
|
28
|
+
this.logger.simple('({bold,green} [ MAKING SCHEDULER ])\n');
|
|
29
|
+
const destination = Config.get('rc.commands.make:scheduler.destination', Path.schedulers());
|
|
30
|
+
const file = await this.generator
|
|
31
|
+
.fileName(this.name)
|
|
32
|
+
.destination(destination)
|
|
33
|
+
.template('scheduler')
|
|
34
|
+
.setNameProperties(true)
|
|
35
|
+
.make();
|
|
36
|
+
this.logger.success(`Scheduler ({yellow} "${file.name}") successfully created.`);
|
|
37
|
+
const importPath = this.generator.getImportPath();
|
|
38
|
+
await this.rc.pushTo('schedulers', importPath).save();
|
|
39
|
+
this.logger.success(`Athenna RC updated: ({dim,yellow} [ schedulers += "${importPath}" ])`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
__decorate([
|
|
43
|
+
Argument({
|
|
44
|
+
description: 'The scheduler name.'
|
|
45
|
+
}),
|
|
46
|
+
__metadata("design:type", String)
|
|
47
|
+
], MakeSchedulerCommand.prototype, "name", void 0);
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @athenna/cron
|
|
3
|
+
*
|
|
4
|
+
* (c) João Lenon <lenon@athenna.io>
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
import type { CronHandler } from '#src/types';
|
|
10
|
+
import type { CronExceptionHandler } from '#src/handlers/CronExceptionHandler';
|
|
11
|
+
export declare class CronBuilder {
|
|
12
|
+
static rTracerPlugin: any;
|
|
13
|
+
static exceptionHandler: CronExceptionHandler;
|
|
14
|
+
/**
|
|
15
|
+
* Register the cls-rtracer plugin into cron handlers.
|
|
16
|
+
*/
|
|
17
|
+
static registerRTracer(plugin: any): void;
|
|
18
|
+
private cron;
|
|
19
|
+
/**
|
|
20
|
+
* Defines the CRON expression that will determine when
|
|
21
|
+
* the task will run.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* Cron.schedule()
|
|
26
|
+
* .pattern('* * * * *')
|
|
27
|
+
* .handler(() => console.log('hey'))
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
pattern(pattern: string): this;
|
|
31
|
+
/**
|
|
32
|
+
* Register a scheduler handler for a dependency that exists
|
|
33
|
+
* inside "rc.schedulers".
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* const task = Cron.schedule()
|
|
38
|
+
* .pattern('* * * * *')
|
|
39
|
+
* .scheduler('MyCustomSchedulerClass')
|
|
40
|
+
*
|
|
41
|
+
* task.stop()
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
scheduler(name: string): import("node-cron").ScheduledTask;
|
|
45
|
+
/**
|
|
46
|
+
* Defines what operation the scheduler will run and
|
|
47
|
+
* register your scheduler with all options defined.
|
|
48
|
+
* This method also return an instance of your scheduler,
|
|
49
|
+
* you can use this instance to stop your scheduler.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* const task = Cron.schedule()
|
|
54
|
+
* .pattern('* * * * *')
|
|
55
|
+
* .handler(() => console.log('hey'))
|
|
56
|
+
*
|
|
57
|
+
* task.stop()
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
handler(handler: CronHandler): import("node-cron").ScheduledTask;
|
|
61
|
+
/**
|
|
62
|
+
* Defines if the task needs to be scheduled to run
|
|
63
|
+
* after registering it.
|
|
64
|
+
*
|
|
65
|
+
* @default true
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* Cron.schedule().pattern('* * * * *')
|
|
69
|
+
* .scheduled(true)
|
|
70
|
+
* .handler(() => console.log('hey'))
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
scheduled(scheduled: boolean): this;
|
|
74
|
+
/**
|
|
75
|
+
* Defines a name to your scheduler.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* Cron.schedule().pattern('* * * * *')
|
|
80
|
+
* .name('myScheduler')
|
|
81
|
+
* .handler(() => console.log('hey'))
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
name(name: string): this;
|
|
85
|
+
/**
|
|
86
|
+
* Defines the timezone that is used for the job
|
|
87
|
+
* scheduling.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* Cron.schedule().pattern('* * * * *')
|
|
92
|
+
* .timezone('America/Sao_Paulo')
|
|
93
|
+
* .handler(() => console.log('hey'))
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
timezone(timezone: string): this;
|
|
97
|
+
/**
|
|
98
|
+
* Runs the task immediately after registering, no matter
|
|
99
|
+
* the CRON pattern defined.
|
|
100
|
+
*
|
|
101
|
+
* @default false
|
|
102
|
+
* @example
|
|
103
|
+
* ```ts
|
|
104
|
+
* Cron.schedule().pattern('* * * * *')
|
|
105
|
+
* .runOnInit(true)
|
|
106
|
+
* .handler(() => console.log('hey'))
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
runOnInit(runOnInit: boolean): this;
|
|
110
|
+
/**
|
|
111
|
+
* Specifies whether to recover missed executions instead
|
|
112
|
+
* of skipping them.
|
|
113
|
+
*
|
|
114
|
+
* @default false
|
|
115
|
+
* @example
|
|
116
|
+
* ```ts
|
|
117
|
+
* Cron.schedule().pattern('* * * * *')
|
|
118
|
+
* .recoverMissingExecutions(true)
|
|
119
|
+
* .handler(() => console.log('hey'))
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
recoverMissedExecutions(recoverMissedExecutions?: boolean): this;
|
|
123
|
+
}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @athenna/cron
|
|
3
|
+
*
|
|
4
|
+
* (c) João Lenon <lenon@athenna.io>
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
import { schedule } from 'node-cron';
|
|
10
|
+
import { Options } from '@athenna/common';
|
|
11
|
+
export class CronBuilder {
|
|
12
|
+
constructor() {
|
|
13
|
+
this.cron = {};
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Register the cls-rtracer plugin into cron handlers.
|
|
17
|
+
*/
|
|
18
|
+
static registerRTracer(plugin) {
|
|
19
|
+
this.rTracerPlugin = plugin;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Defines the CRON expression that will determine when
|
|
23
|
+
* the task will run.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* Cron.schedule()
|
|
28
|
+
* .pattern('* * * * *')
|
|
29
|
+
* .handler(() => console.log('hey'))
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
pattern(pattern) {
|
|
33
|
+
this.cron.pattern = pattern;
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Register a scheduler handler for a dependency that exists
|
|
38
|
+
* inside "rc.schedulers".
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* const task = Cron.schedule()
|
|
43
|
+
* .pattern('* * * * *')
|
|
44
|
+
* .scheduler('MyCustomSchedulerClass')
|
|
45
|
+
*
|
|
46
|
+
* task.stop()
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
scheduler(name) {
|
|
50
|
+
return this.handler(ctx => {
|
|
51
|
+
const scheduler = ioc.use(name) || ioc.safeUse(`App/Cron/Schedulers/${name}`);
|
|
52
|
+
return scheduler.handle(ctx);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Defines what operation the scheduler will run and
|
|
57
|
+
* register your scheduler with all options defined.
|
|
58
|
+
* This method also return an instance of your scheduler,
|
|
59
|
+
* you can use this instance to stop your scheduler.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* const task = Cron.schedule()
|
|
64
|
+
* .pattern('* * * * *')
|
|
65
|
+
* .handler(() => console.log('hey'))
|
|
66
|
+
*
|
|
67
|
+
* task.stop()
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
handler(handler) {
|
|
71
|
+
const register = () => {
|
|
72
|
+
const getCtx = () => ({
|
|
73
|
+
name: this.cron.name,
|
|
74
|
+
traceId: CronBuilder.rTracerPlugin
|
|
75
|
+
? CronBuilder.rTracerPlugin.id()
|
|
76
|
+
: null,
|
|
77
|
+
pattern: this.cron.pattern,
|
|
78
|
+
timezone: this.cron.timezone,
|
|
79
|
+
runOnInit: this.cron.runOnInit,
|
|
80
|
+
recoverMissedExecutions: this.cron.recoverMissedExecutions
|
|
81
|
+
});
|
|
82
|
+
const options = Options.create({
|
|
83
|
+
name: this.cron.name,
|
|
84
|
+
timezone: this.cron.timezone,
|
|
85
|
+
runOnInit: this.cron.runOnInit,
|
|
86
|
+
scheduled: this.cron.scheduled,
|
|
87
|
+
recoverMissedExecutions: this.cron.recoverMissedExecutions
|
|
88
|
+
});
|
|
89
|
+
if (CronBuilder.rTracerPlugin) {
|
|
90
|
+
return schedule(this.cron.pattern, () => CronBuilder.rTracerPlugin.runWithId(() => this.cron.handler(getCtx())), options);
|
|
91
|
+
}
|
|
92
|
+
return schedule(this.cron.pattern, () => this.cron.handler(getCtx()), options);
|
|
93
|
+
};
|
|
94
|
+
if (!CronBuilder.exceptionHandler) {
|
|
95
|
+
this.cron.handler = handler;
|
|
96
|
+
return register();
|
|
97
|
+
}
|
|
98
|
+
this.cron.handler = async (...args) => {
|
|
99
|
+
try {
|
|
100
|
+
await handler(...args);
|
|
101
|
+
}
|
|
102
|
+
catch (err) {
|
|
103
|
+
CronBuilder.exceptionHandler.handle(err);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
return register();
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Defines if the task needs to be scheduled to run
|
|
110
|
+
* after registering it.
|
|
111
|
+
*
|
|
112
|
+
* @default true
|
|
113
|
+
* @example
|
|
114
|
+
* ```ts
|
|
115
|
+
* Cron.schedule().pattern('* * * * *')
|
|
116
|
+
* .scheduled(true)
|
|
117
|
+
* .handler(() => console.log('hey'))
|
|
118
|
+
* ```
|
|
119
|
+
*/
|
|
120
|
+
scheduled(scheduled) {
|
|
121
|
+
this.cron.scheduled = scheduled;
|
|
122
|
+
return this;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Defines a name to your scheduler.
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```ts
|
|
129
|
+
* Cron.schedule().pattern('* * * * *')
|
|
130
|
+
* .name('myScheduler')
|
|
131
|
+
* .handler(() => console.log('hey'))
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
name(name) {
|
|
135
|
+
this.cron.name = name;
|
|
136
|
+
return this;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Defines the timezone that is used for the job
|
|
140
|
+
* scheduling.
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```ts
|
|
144
|
+
* Cron.schedule().pattern('* * * * *')
|
|
145
|
+
* .timezone('America/Sao_Paulo')
|
|
146
|
+
* .handler(() => console.log('hey'))
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
timezone(timezone) {
|
|
150
|
+
this.cron.timezone = timezone;
|
|
151
|
+
return this;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* Runs the task immediately after registering, no matter
|
|
155
|
+
* the CRON pattern defined.
|
|
156
|
+
*
|
|
157
|
+
* @default false
|
|
158
|
+
* @example
|
|
159
|
+
* ```ts
|
|
160
|
+
* Cron.schedule().pattern('* * * * *')
|
|
161
|
+
* .runOnInit(true)
|
|
162
|
+
* .handler(() => console.log('hey'))
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
runOnInit(runOnInit) {
|
|
166
|
+
this.cron.runOnInit = runOnInit;
|
|
167
|
+
return this;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Specifies whether to recover missed executions instead
|
|
171
|
+
* of skipping them.
|
|
172
|
+
*
|
|
173
|
+
* @default false
|
|
174
|
+
* @example
|
|
175
|
+
* ```ts
|
|
176
|
+
* Cron.schedule().pattern('* * * * *')
|
|
177
|
+
* .recoverMissingExecutions(true)
|
|
178
|
+
* .handler(() => console.log('hey'))
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
181
|
+
recoverMissedExecutions(recoverMissedExecutions) {
|
|
182
|
+
this.cron.recoverMissedExecutions = recoverMissedExecutions;
|
|
183
|
+
return this;
|
|
184
|
+
}
|
|
185
|
+
}
|