@midwayjs/cron 3.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/README.md +12 -0
- package/dist/configuration.d.ts +9 -0
- package/dist/configuration.js +67 -0
- package/dist/constants.d.ts +2 -0
- package/dist/constants.js +6 -0
- package/dist/decorator.d.ts +5 -0
- package/dist/decorator.js +28 -0
- package/dist/framework.d.ts +18 -0
- package/dist/framework.js +115 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +25 -0
- package/dist/interface.d.ts +25 -0
- package/dist/interface.js +6 -0
- package/index.d.ts +8 -0
- package/package.json +36 -0
package/README.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# midwayjs cron module
|
|
2
|
+
|
|
3
|
+
[](http://packagequality.com/#?package=midway-core)
|
|
4
|
+
[](https://github.com/midwayjs/midway/pulls)
|
|
5
|
+
|
|
6
|
+
this is a sub package for midway.
|
|
7
|
+
|
|
8
|
+
Document: [https://midwayjs.org](https://midwayjs.org)
|
|
9
|
+
|
|
10
|
+
## License
|
|
11
|
+
|
|
12
|
+
[MIT]((http://github.com/midwayjs/midway/blob/master/LICENSE))
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { MidwayDecoratorService } from '@midwayjs/core';
|
|
2
|
+
import { CronFramework } from './framework';
|
|
3
|
+
export declare class CronConfiguration {
|
|
4
|
+
framework: CronFramework;
|
|
5
|
+
decoratorService: MidwayDecoratorService;
|
|
6
|
+
init(): Promise<void>;
|
|
7
|
+
onReady(): Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=configuration.d.ts.map
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
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;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.CronConfiguration = void 0;
|
|
13
|
+
const core_1 = require("@midwayjs/core");
|
|
14
|
+
const framework_1 = require("./framework");
|
|
15
|
+
const constants_1 = require("./constants");
|
|
16
|
+
let CronConfiguration = class CronConfiguration {
|
|
17
|
+
async init() {
|
|
18
|
+
this.decoratorService.registerPropertyHandler(constants_1.CRON_JOB_KEY, (propertyName, meta) => {
|
|
19
|
+
return this.framework.getJob(meta.jobName);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
async onReady() {
|
|
23
|
+
this.framework.loadConfig();
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
__decorate([
|
|
27
|
+
(0, core_1.Inject)(),
|
|
28
|
+
__metadata("design:type", framework_1.CronFramework)
|
|
29
|
+
], CronConfiguration.prototype, "framework", void 0);
|
|
30
|
+
__decorate([
|
|
31
|
+
(0, core_1.Inject)(),
|
|
32
|
+
__metadata("design:type", core_1.MidwayDecoratorService)
|
|
33
|
+
], CronConfiguration.prototype, "decoratorService", void 0);
|
|
34
|
+
__decorate([
|
|
35
|
+
(0, core_1.Init)(),
|
|
36
|
+
__metadata("design:type", Function),
|
|
37
|
+
__metadata("design:paramtypes", []),
|
|
38
|
+
__metadata("design:returntype", Promise)
|
|
39
|
+
], CronConfiguration.prototype, "init", null);
|
|
40
|
+
CronConfiguration = __decorate([
|
|
41
|
+
(0, core_1.Configuration)({
|
|
42
|
+
namespace: 'cron',
|
|
43
|
+
importConfigs: [
|
|
44
|
+
{
|
|
45
|
+
default: {
|
|
46
|
+
cron: {
|
|
47
|
+
defaultCronJobOptions: {},
|
|
48
|
+
contextLoggerApplyLogger: 'cronLogger',
|
|
49
|
+
},
|
|
50
|
+
midwayLogger: {
|
|
51
|
+
clients: {
|
|
52
|
+
cronLogger: {
|
|
53
|
+
fileLogName: 'midway-cron.log',
|
|
54
|
+
contextFormat: info => {
|
|
55
|
+
const { jobId, from } = info.ctx;
|
|
56
|
+
return `${info.timestamp} ${info.LEVEL} ${info.pid} [${jobId} ${from.name}}] ${info.message}`;
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
],
|
|
64
|
+
})
|
|
65
|
+
], CronConfiguration);
|
|
66
|
+
exports.CronConfiguration = CronConfiguration;
|
|
67
|
+
//# sourceMappingURL=configuration.js.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { CronJobOptions, IJob } from './interface';
|
|
2
|
+
export declare function Job(jobOptions?: CronJobOptions): ClassDecorator;
|
|
3
|
+
export declare function Job(jobName: string, jobOptions?: CronJobOptions): ClassDecorator;
|
|
4
|
+
export declare function InjectJob(jobName: string | (new (...args: any[]) => IJob)): PropertyDecorator;
|
|
5
|
+
//# sourceMappingURL=decorator.d.ts.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InjectJob = exports.Job = void 0;
|
|
4
|
+
const core_1 = require("@midwayjs/core");
|
|
5
|
+
const constants_1 = require("./constants");
|
|
6
|
+
function Job(jobName, jobOptions) {
|
|
7
|
+
return function (target) {
|
|
8
|
+
if (typeof jobName !== 'string') {
|
|
9
|
+
jobOptions = jobName;
|
|
10
|
+
jobName = undefined;
|
|
11
|
+
}
|
|
12
|
+
(0, core_1.saveModule)(constants_1.CRON_JOB_KEY, target);
|
|
13
|
+
(0, core_1.saveClassMetadata)(constants_1.CRON_JOB_KEY, {
|
|
14
|
+
jobOptions,
|
|
15
|
+
jobName,
|
|
16
|
+
}, target);
|
|
17
|
+
(0, core_1.Provide)()(target);
|
|
18
|
+
(0, core_1.Scope)(core_1.ScopeEnum.Request)(target);
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
exports.Job = Job;
|
|
22
|
+
function InjectJob(jobName) {
|
|
23
|
+
return (0, core_1.createCustomPropertyDecorator)(constants_1.CRON_JOB_KEY, {
|
|
24
|
+
jobName,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
exports.InjectJob = InjectJob;
|
|
28
|
+
//# sourceMappingURL=decorator.js.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { BaseFramework, IMidwayBootstrapOptions } from '@midwayjs/core';
|
|
2
|
+
import { Application, Context, JobNameOrClz } from './interface';
|
|
3
|
+
import { CronJob, CronJobParameters } from 'cron';
|
|
4
|
+
export declare class CronFramework extends BaseFramework<Application, Context, any> {
|
|
5
|
+
private defaultCronJobConfig;
|
|
6
|
+
private jobs;
|
|
7
|
+
applicationInitialize(options: IMidwayBootstrapOptions): Promise<void>;
|
|
8
|
+
loadConfig(): void;
|
|
9
|
+
configure(): any;
|
|
10
|
+
getFrameworkName(): string;
|
|
11
|
+
run(): Promise<void>;
|
|
12
|
+
protected beforeStop(): Promise<void>;
|
|
13
|
+
addJob(name: JobNameOrClz, jobOptions?: Partial<CronJobParameters>): CronJob;
|
|
14
|
+
getJob(name: JobNameOrClz): CronJob;
|
|
15
|
+
deleteJob(name: JobNameOrClz): Promise<void>;
|
|
16
|
+
private getJobName;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=framework.d.ts.map
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
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;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.CronFramework = void 0;
|
|
10
|
+
const core_1 = require("@midwayjs/core");
|
|
11
|
+
const cron_1 = require("cron");
|
|
12
|
+
const constants_1 = require("./constants");
|
|
13
|
+
let CronFramework = class CronFramework extends core_1.BaseFramework {
|
|
14
|
+
constructor() {
|
|
15
|
+
super(...arguments);
|
|
16
|
+
this.jobs = new Map();
|
|
17
|
+
}
|
|
18
|
+
async applicationInitialize(options) {
|
|
19
|
+
this.app = {};
|
|
20
|
+
}
|
|
21
|
+
loadConfig() {
|
|
22
|
+
this.defaultCronJobConfig = this.configService.getConfiguration('cron.defaultCronJobOptions');
|
|
23
|
+
}
|
|
24
|
+
configure() {
|
|
25
|
+
return this.configService.getConfiguration('cron');
|
|
26
|
+
}
|
|
27
|
+
getFrameworkName() {
|
|
28
|
+
return 'cron';
|
|
29
|
+
}
|
|
30
|
+
async run() {
|
|
31
|
+
const jobModules = (0, core_1.listModule)(constants_1.CRON_JOB_KEY);
|
|
32
|
+
for (const mod of jobModules) {
|
|
33
|
+
this.addJob(mod);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
async beforeStop() {
|
|
37
|
+
// loop queueMap and stop all queue
|
|
38
|
+
for (const name of this.jobs.keys()) {
|
|
39
|
+
await this.deleteJob(name);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
addJob(name, jobOptions = {}) {
|
|
43
|
+
let jobName;
|
|
44
|
+
if (typeof name === 'string') {
|
|
45
|
+
jobName = name;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
const options = (0, core_1.getClassMetadata)(constants_1.CRON_JOB_KEY, name);
|
|
49
|
+
jobName = options.name || (0, core_1.getProviderUUId)(name);
|
|
50
|
+
jobOptions = (0, core_1.extend)(true, {}, this.defaultCronJobConfig, options.jobOptions, jobOptions);
|
|
51
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
52
|
+
const self = this;
|
|
53
|
+
jobOptions.onTick = function () {
|
|
54
|
+
(async () => {
|
|
55
|
+
var _a;
|
|
56
|
+
const ctx = self.app.createAnonymousContext({
|
|
57
|
+
job: this,
|
|
58
|
+
});
|
|
59
|
+
ctx.logger.info(`start job ${name.name}`);
|
|
60
|
+
const isPassed = await self.app
|
|
61
|
+
.getFramework()
|
|
62
|
+
.runGuard(ctx, name, 'onTick');
|
|
63
|
+
if (!isPassed) {
|
|
64
|
+
throw new core_1.MidwayInvokeForbiddenError('onTick', name);
|
|
65
|
+
}
|
|
66
|
+
const service = await ctx.requestContext.getAsync(name);
|
|
67
|
+
const fn = await self.applyMiddleware(async (ctx) => {
|
|
68
|
+
return await core_1.Utils.toAsyncFunction(service.onTick.bind(service))();
|
|
69
|
+
});
|
|
70
|
+
try {
|
|
71
|
+
const result = await Promise.resolve(await fn(ctx));
|
|
72
|
+
ctx.logger.info(`complete job ${name.name}`);
|
|
73
|
+
await ((_a = service.onComplete) === null || _a === void 0 ? void 0 : _a.call(service, result));
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
catch (err) {
|
|
77
|
+
ctx.logger.error(err);
|
|
78
|
+
}
|
|
79
|
+
})().catch(err => {
|
|
80
|
+
self.logger.error(`error in job from ${name.name}: ${err}`);
|
|
81
|
+
});
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
const job = new cron_1.CronJob(jobOptions);
|
|
85
|
+
this.jobs.set(jobName, job);
|
|
86
|
+
return job;
|
|
87
|
+
}
|
|
88
|
+
getJob(name) {
|
|
89
|
+
return this.jobs.get(this.getJobName(name));
|
|
90
|
+
}
|
|
91
|
+
async deleteJob(name) {
|
|
92
|
+
const jobName = this.getJobName(name);
|
|
93
|
+
try {
|
|
94
|
+
this.jobs.get(jobName).stop();
|
|
95
|
+
this.jobs.delete(jobName);
|
|
96
|
+
}
|
|
97
|
+
catch (err) {
|
|
98
|
+
this.logger.error(`error in trying to stop job: ${jobName}: ${err}`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
getJobName(name) {
|
|
102
|
+
if (typeof name === 'string') {
|
|
103
|
+
return name;
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
const options = (0, core_1.getClassMetadata)(constants_1.CRON_JOB_KEY, name);
|
|
107
|
+
return options.name || (0, core_1.getProviderUUId)(name);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
CronFramework = __decorate([
|
|
112
|
+
(0, core_1.Framework)()
|
|
113
|
+
], CronFramework);
|
|
114
|
+
exports.CronFramework = CronFramework;
|
|
115
|
+
//# sourceMappingURL=framework.js.map
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.Framework = exports.Configuration = void 0;
|
|
18
|
+
__exportStar(require("./interface"), exports);
|
|
19
|
+
var configuration_1 = require("./configuration");
|
|
20
|
+
Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.CronConfiguration; } });
|
|
21
|
+
var framework_1 = require("./framework");
|
|
22
|
+
Object.defineProperty(exports, "Framework", { enumerable: true, get: function () { return framework_1.CronFramework; } });
|
|
23
|
+
__exportStar(require("./constants"), exports);
|
|
24
|
+
__exportStar(require("./decorator"), exports);
|
|
25
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { IMidwayApplication, IMidwayContext, NextFunction as BaseNextFunction } from '@midwayjs/core';
|
|
2
|
+
import { CronJob, CronJobParameters } from 'cron';
|
|
3
|
+
export { CronJob } from 'cron';
|
|
4
|
+
export type CronJobOptions = Omit<CronJobParameters, 'onTick' | 'onComplete'>;
|
|
5
|
+
export interface CronOptions {
|
|
6
|
+
defaultCronJobOptions?: CronJobOptions;
|
|
7
|
+
}
|
|
8
|
+
export interface IJob {
|
|
9
|
+
/**
|
|
10
|
+
* The function to fire at the specified time. If an onComplete callback was provided, onTick will receive it as an argument. onTick may call onComplete when it has finished its work.
|
|
11
|
+
*/
|
|
12
|
+
onTick(data: any): any;
|
|
13
|
+
/**
|
|
14
|
+
* A function that will fire when the job is stopped with job.stop(), and may also be called by onTick at the end of each run.
|
|
15
|
+
*/
|
|
16
|
+
onComplete?(result: any): any;
|
|
17
|
+
}
|
|
18
|
+
export type JobNameOrClz = string | (new (...args: any[]) => IJob);
|
|
19
|
+
export interface Application extends IMidwayApplication<Context> {
|
|
20
|
+
}
|
|
21
|
+
export type NextFunction = BaseNextFunction;
|
|
22
|
+
export interface Context extends IMidwayContext {
|
|
23
|
+
job: CronJob;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=interface.d.ts.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CronJob = void 0;
|
|
4
|
+
var cron_1 = require("cron");
|
|
5
|
+
Object.defineProperty(exports, "CronJob", { enumerable: true, get: function () { return cron_1.CronJob; } });
|
|
6
|
+
//# sourceMappingURL=interface.js.map
|
package/index.d.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@midwayjs/cron",
|
|
3
|
+
"version": "3.0.0",
|
|
4
|
+
"description": "Midway Component for Cron",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"typings": "index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"test": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand",
|
|
10
|
+
"cov": "node --require=ts-node/register ../../node_modules/.bin/jest --runInBand --coverage --forceExit",
|
|
11
|
+
"ci": "npm run test"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"midway",
|
|
15
|
+
"component",
|
|
16
|
+
"cron"
|
|
17
|
+
],
|
|
18
|
+
"author": "Harry Chen <czy88840616@gmail.com>",
|
|
19
|
+
"files": [
|
|
20
|
+
"dist/**/*.js",
|
|
21
|
+
"dist/**/*.d.ts",
|
|
22
|
+
"index.d.ts"
|
|
23
|
+
],
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=12"
|
|
26
|
+
},
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"cron": "2.2.0",
|
|
30
|
+
"@types/cron": "2.0.0"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@midwayjs/core": "^3.10.10",
|
|
34
|
+
"@midwayjs/mock": "^3.10.13"
|
|
35
|
+
}
|
|
36
|
+
}
|