@tsed/agenda 7.0.0-beta.11 → 7.0.0-beta.14
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/jest.config.js +14 -0
- package/lib/cjs/AgendaModule.js +12 -9
- package/lib/cjs/AgendaModule.js.map +1 -1
- package/lib/cjs/decorators/agenda.js +1 -1
- package/lib/cjs/decorators/agenda.js.map +1 -1
- package/lib/esm/AgendaModule.js +12 -9
- package/lib/esm/AgendaModule.js.map +1 -1
- package/lib/esm/decorators/agenda.js +1 -1
- package/lib/esm/decorators/agenda.js.map +1 -1
- package/package.json +10 -7
- package/readme.md +2 -1
- package/src/AgendaModule.ts +13 -14
- package/src/decorators/agenda.spec.ts +2 -5
- package/src/decorators/agenda.ts +1 -1
- package/src/decorators/define.spec.ts +2 -3
- package/src/decorators/every.spec.ts +2 -3
- package/.mocharc.js +0 -5
- package/.nycrc +0 -27
package/jest.config.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// For a detailed explanation regarding each configuration property, visit:
|
|
2
|
+
// https://jestjs.io/docs/en/configuration.html
|
|
3
|
+
|
|
4
|
+
module.exports = {
|
|
5
|
+
...require("@tsed/jest-config")(__dirname, "agenda"),
|
|
6
|
+
coverageThreshold: {
|
|
7
|
+
global: {
|
|
8
|
+
statements: 96.96,
|
|
9
|
+
branches: 86.66,
|
|
10
|
+
functions: 100,
|
|
11
|
+
lines: 96.77
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
};
|
package/lib/cjs/AgendaModule.js
CHANGED
|
@@ -7,6 +7,10 @@ const di_1 = require("@tsed/di");
|
|
|
7
7
|
const constants_1 = require("./constants/constants");
|
|
8
8
|
const AgendaFactory_1 = require("./services/AgendaFactory");
|
|
9
9
|
let AgendaModule = class AgendaModule {
|
|
10
|
+
logger;
|
|
11
|
+
injector;
|
|
12
|
+
agenda;
|
|
13
|
+
loadAgenda;
|
|
10
14
|
async $afterListen() {
|
|
11
15
|
if (this.loadAgenda) {
|
|
12
16
|
const providers = this.getProviders();
|
|
@@ -14,7 +18,7 @@ let AgendaModule = class AgendaModule {
|
|
|
14
18
|
this.logger.info("Agenda add definitions...");
|
|
15
19
|
await this.agenda.start();
|
|
16
20
|
this.logger.info("Agenda add scheduled jobs...");
|
|
17
|
-
providers.
|
|
21
|
+
await Promise.all(providers.map((provider) => this.scheduleJobsForProvider(provider)));
|
|
18
22
|
}
|
|
19
23
|
else {
|
|
20
24
|
this.logger.info("Agenda jobs disabled...");
|
|
@@ -31,28 +35,27 @@ let AgendaModule = class AgendaModule {
|
|
|
31
35
|
return this.injector.getProviders(constants_1.PROVIDER_TYPE_AGENDA);
|
|
32
36
|
}
|
|
33
37
|
addAgendaDefinitionsForProvider(provider) {
|
|
34
|
-
const store = provider.store.get("agenda");
|
|
38
|
+
const store = provider.store.get("agenda", {});
|
|
35
39
|
if (!store.define) {
|
|
36
40
|
return;
|
|
37
41
|
}
|
|
38
|
-
|
|
39
|
-
for (const [propertyKey, { name, ...options }] of jobsToDefine) {
|
|
42
|
+
Object.entries(store.define).forEach(([propertyKey, { name, ...options }]) => {
|
|
40
43
|
const instance = this.injector.get(provider.token);
|
|
41
44
|
const jobProcessor = instance[propertyKey].bind(instance);
|
|
42
45
|
const jobName = this.getNameForJob(propertyKey, store.namespace, name);
|
|
43
46
|
this.agenda.define(jobName, options, jobProcessor);
|
|
44
|
-
}
|
|
47
|
+
});
|
|
45
48
|
}
|
|
46
49
|
async scheduleJobsForProvider(provider) {
|
|
47
|
-
const store = provider.store.get("agenda");
|
|
50
|
+
const store = provider.store.get("agenda", {});
|
|
48
51
|
if (!store.every) {
|
|
49
52
|
return;
|
|
50
53
|
}
|
|
51
|
-
const
|
|
52
|
-
await Promise.all(jobsToSchedule.map(([propertyKey, { interval, name, ...options }]) => {
|
|
54
|
+
const promises = Object.entries(store.every).map(([propertyKey, { interval, name, ...options }]) => {
|
|
53
55
|
const jobName = this.getNameForJob(propertyKey, store.namespace, name);
|
|
54
56
|
return this.agenda.every(interval, jobName, {}, options);
|
|
55
|
-
})
|
|
57
|
+
});
|
|
58
|
+
await Promise.all(promises);
|
|
56
59
|
}
|
|
57
60
|
getNameForJob(propertyKey, namespace, customName) {
|
|
58
61
|
const name = customName || propertyKey;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AgendaModule.js","sourceRoot":"","sources":["../../src/AgendaModule.ts"],"names":[],"mappings":";;;;AAAA,yCAA4D;AAC5D,iCAA6E;AAE7E,qDAA2D;AAE3D,4DAAuD;AAGvD,IAAa,YAAY,GAAzB,MAAa,YAAY;
|
|
1
|
+
{"version":3,"file":"AgendaModule.js","sourceRoot":"","sources":["../../src/AgendaModule.ts"],"names":[],"mappings":";;;;AAAA,yCAA4D;AAC5D,iCAA6E;AAE7E,qDAA2D;AAE3D,4DAAuD;AAGvD,IAAa,YAAY,GAAzB,MAAa,YAAY;IAEb,MAAM,CAAS;IAGf,QAAQ,CAAkB;IAG1B,MAAM,CAAgB;IAGxB,UAAU,CAAU;IAE5B,KAAK,CAAC,YAAY;QAChB,IAAI,IAAI,CAAC,UAAU,EAAE;YACnB,MAAM,SAAS,GAAG,IAAI,CAAC,YAAY,EAAE,CAAC;YACtC,SAAS,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,IAAI,CAAC,+BAA+B,CAAC,QAAQ,CAAC,CAAC,CAAC;YAEhF,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,2BAA2B,CAAC,CAAC;YAC9C,MAAM,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;YAE1B,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,8BAA8B,CAAC,CAAC;YACjD,MAAM,OAAO,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,IAAI,CAAC,uBAAuB,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;SACxF;aAAM;YACL,IAAI,C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AK,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAc,QAAQ,EAAE,EAAE,CAAC,CAAC;QAE5D,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE;YAChB,OAAO;SACR;QAED,MAAM,QAAQ,GAAG,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,WAAW,EAAE,EAAC,QAAQ,EAAE,IAAI,EAAE,GAAG,OAAO,EAAC,CAAC,EAAE,EAAE;YAC/F,MAAM,OAAO,GAAG,IAAI,CAAC,aAAa,CAAC,WAAW,EAAE,KAAK,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;YAEvE,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,EAAE,OAAO,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC;QAC3D,CAAC,CAAC,CAAC;QAEH,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC9B,CAAC;IAES,aAAa,CAAC,WAAmB,EAAE,SAAkB,EAAE,UAAmB;QAClF,MAAM,IAAI,GAAG,UAAU,IAAI,WAAW,CAAC;QACvC,OAAO,SAAS,CAAC,CAAC,CAAC,GAAG,SAAS,IAAI,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;IACnD,CAAC;CACF,CAAA;AA3EC;IADC,IAAA,WAAM,GAAE;sCACS,eAAM;4CAAC;AAGzB;IADC,IAAA,WAAM,GAAE;sCACW,oBAAe;8CAAC;AAGpC;IADC,IAAA,WAAM,GAAE;sCACS,6BAAa;4CAAC;AAGhC;IADC,IAAA,aAAQ,EAAC,gBAAgB,EAAE,KAAK,CAAC;;gDACN;AAXjB,YAAY;IADxB,IAAA,WAAM,GAAE;GACI,YAAY,CA6ExB;AA7EY,oCAAY","sourcesContent":["import {AfterListen, Logger, OnDestroy} from \"@tsed/common\";\nimport {Constant, Inject, InjectorService, Module, Provider} from \"@tsed/di\";\nimport {Processor} from \"agenda\";\nimport {PROVIDER_TYPE_AGENDA} from \"./constants/constants\";\nimport {AgendaStore} from \"./interfaces/AgendaStore\";\nimport {AgendaService} from \"./services/AgendaFactory\";\n\n@Module()\nexport class AgendaModule implements OnDestroy, AfterListen {\n @Inject()\n protected logger: Logger;\n\n @Inject()\n protected injector: InjectorService;\n\n @Inject()\n protected agenda: AgendaService;\n\n @Constant(\"agenda.enabled\", false)\n private loadAgenda: boolean;\n\n async $afterListen(): Promise<any> {\n if (this.loadAgenda) {\n const providers = this.getProviders();\n providers.forEach((provider) => this.addAgendaDefinitionsForProvider(provider));\n\n this.logger.info(\"Agenda add definitions...\");\n await this.agenda.start();\n\n this.logger.info(\"Agenda add scheduled jobs...\");\n await Promise.all(providers.map((provider) => this.scheduleJobsForProvider(provider)));\n } else {\n this.logger.info(\"Agenda jobs disabled...\");\n }\n }\n\n async $onDestroy(): Promise<any> {\n if (this.loadAgenda) {\n await this.agenda.stop();\n await this.agenda.close({force: true});\n\n this.logger.info(\"Agenda jobs stopped...\");\n }\n }\n\n protected getProviders(): Provider<any>[] {\n return this.injector.getProviders(PROVIDER_TYPE_AGENDA);\n }\n\n protected addAgendaDefinitionsForProvider(provider: Provider): void {\n const store = provider.store.get<AgendaStore>(\"agenda\", {});\n\n if (!store.define) {\n return;\n }\n\n Object.entries(store.define).forEach(([propertyKey, {name, ...options}]) => {\n const instance = this.injector.get(provider.token);\n\n const jobProcessor: Processor = instance[propertyKey].bind(instance) as Processor;\n const jobName = this.getNameForJob(propertyKey, store.namespace, name);\n this.agenda.define(jobName, options, jobProcessor);\n });\n }\n\n protected async scheduleJobsForProvider(provider: Provider<any>): Promise<void> {\n const store = provider.store.get<AgendaStore>(\"agenda\", {});\n\n if (!store.every) {\n return;\n }\n\n const promises = Object.entries(store.every).map(([propertyKey, {interval, name, ...options}]) => {\n const jobName = this.getNameForJob(propertyKey, store.namespace, name);\n\n return this.agenda.every(interval, jobName, {}, options);\n });\n\n await Promise.all(promises);\n }\n\n protected getNameForJob(propertyKey: string, namespace?: string, customName?: string): string {\n const name = customName || propertyKey;\n return namespace ? `${namespace}.${name}` : name;\n }\n}\n"]}
|
|
@@ -5,7 +5,7 @@ const core_1 = require("@tsed/core");
|
|
|
5
5
|
const di_1 = require("@tsed/di");
|
|
6
6
|
const constants_1 = require("../constants/constants");
|
|
7
7
|
function Agenda(options) {
|
|
8
|
-
return (0, core_1.useDecorators)((0, core_1.StoreMerge)("agenda",
|
|
8
|
+
return (0, core_1.useDecorators)(options?.namespace && (0, core_1.StoreMerge)("agenda", options), (0, di_1.Injectable)({
|
|
9
9
|
type: constants_1.PROVIDER_TYPE_AGENDA
|
|
10
10
|
}));
|
|
11
11
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agenda.js","sourceRoot":"","sources":["../../../src/decorators/agenda.ts"],"names":[],"mappings":";;;AAAA,qCAAqD;AACrD,iCAAoC;AAEpC,sDAA4D;AAiB5D,SAAgB,MAAM,CAAC,OAAuB;IAC5C,OAAO,IAAA,oBAAa,EAClB,IAAA,iBAAU,EAAC,QAAQ,EAAE,
|
|
1
|
+
{"version":3,"file":"agenda.js","sourceRoot":"","sources":["../../../src/decorators/agenda.ts"],"names":[],"mappings":";;;AAAA,qCAAqD;AACrD,iCAAoC;AAEpC,sDAA4D;AAiB5D,SAAgB,MAAM,CAAC,OAAuB;IAC5C,OAAO,IAAA,oBAAa,EAClB,OAAO,EAAE,SAAS,IAAI,IAAA,iBAAU,EAAC,QAAQ,EAAE,OAAO,CAAC,EACnD,IAAA,eAAU,EAAC;QACT,IAAI,EAAE,gCAAoB;KAC3B,CAAC,CACH,CAAC;AACJ,CAAC;AAPD,wBAOC","sourcesContent":["import {StoreMerge, useDecorators} from \"@tsed/core\";\nimport {Injectable} from \"@tsed/di\";\nimport {AgendaConfig} from \"agenda\";\nimport {PROVIDER_TYPE_AGENDA} from \"../constants/constants\";\n\ndeclare global {\n namespace TsED {\n interface Configuration {\n agenda?: {\n /** * Enable Agenda jobs. Default false. */\n enabled?: boolean;\n } & AgendaConfig;\n }\n }\n}\n\ninterface AgendaOptions {\n namespace?: string;\n}\n\nexport function Agenda(options?: AgendaOptions): ClassDecorator {\n return useDecorators(\n options?.namespace && StoreMerge(\"agenda\", options),\n Injectable({\n type: PROVIDER_TYPE_AGENDA\n })\n );\n}\n"]}
|
package/lib/esm/AgendaModule.js
CHANGED
|
@@ -4,6 +4,10 @@ import { Constant, Inject, InjectorService, Module } from "@tsed/di";
|
|
|
4
4
|
import { PROVIDER_TYPE_AGENDA } from "./constants/constants.js";
|
|
5
5
|
import { AgendaService } from "./services/AgendaFactory.js";
|
|
6
6
|
let AgendaModule = class AgendaModule {
|
|
7
|
+
logger;
|
|
8
|
+
injector;
|
|
9
|
+
agenda;
|
|
10
|
+
loadAgenda;
|
|
7
11
|
async $afterListen() {
|
|
8
12
|
if (this.loadAgenda) {
|
|
9
13
|
const providers = this.getProviders();
|
|
@@ -11,7 +15,7 @@ let AgendaModule = class AgendaModule {
|
|
|
11
15
|
this.logger.info("Agenda add definitions...");
|
|
12
16
|
await this.agenda.start();
|
|
13
17
|
this.logger.info("Agenda add scheduled jobs...");
|
|
14
|
-
providers.
|
|
18
|
+
await Promise.all(providers.map((provider) => this.scheduleJobsForProvider(provider)));
|
|
15
19
|
}
|
|
16
20
|
else {
|
|
17
21
|
this.logger.info("Agenda jobs disabled...");
|
|
@@ -28,28 +32,27 @@ let AgendaModule = class AgendaModule {
|
|
|
28
32
|
return this.injector.getProviders(PROVIDER_TYPE_AGENDA);
|
|
29
33
|
}
|
|
30
34
|
addAgendaDefinitionsForProvider(provider) {
|
|
31
|
-
const store = provider.store.get("agenda");
|
|
35
|
+
const store = provider.store.get("agenda", {});
|
|
32
36
|
if (!store.define) {
|
|
33
37
|
return;
|
|
34
38
|
}
|
|
35
|
-
|
|
36
|
-
for (const [propertyKey, { name, ...options }] of jobsToDefine) {
|
|
39
|
+
Object.entries(store.define).forEach(([propertyKey, { name, ...options }]) => {
|
|
37
40
|
const instance = this.injector.get(provider.token);
|
|
38
41
|
const jobProcessor = instance[propertyKey].bind(instance);
|
|
39
42
|
const jobName = this.getNameForJob(propertyKey, store.namespace, name);
|
|
40
43
|
this.agenda.define(jobName, options, jobProcessor);
|
|
41
|
-
}
|
|
44
|
+
});
|
|
42
45
|
}
|
|
43
46
|
async scheduleJobsForProvider(provider) {
|
|
44
|
-
const store = provider.store.get("agenda");
|
|
47
|
+
const store = provider.store.get("agenda", {});
|
|
45
48
|
if (!store.every) {
|
|
46
49
|
return;
|
|
47
50
|
}
|
|
48
|
-
const
|
|
49
|
-
await Promise.all(jobsToSchedule.map(([propertyKey, { interval, name, ...options }]) => {
|
|
51
|
+
const promises = Object.entries(store.every).map(([propertyKey, { interval, name, ...options }]) => {
|
|
50
52
|
const jobName = this.getNameForJob(propertyKey, store.namespace, name);
|
|
51
53
|
return this.agenda.every(interval, jobName, {}, options);
|
|
52
|
-
})
|
|
54
|
+
});
|
|
55
|
+
await Promise.all(promises);
|
|
53
56
|
}
|
|
54
57
|
getNameForJob(propertyKey, namespace, customName) {
|
|
55
58
|
const name = customName || propertyKey;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AgendaModule.js","sourceRoot":"","sources":["../../src/AgendaModule.ts"],"names":[],"mappings":";AAAA,OAAO,EAAc,MAAM,EAAY,MAAM,cAAc,CAAC;AAC5D,OAAO,EAAC,QAAQ,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,EAAW,MAAM,UAAU,CAAC;AAE7E,OAAO,EAAC,oBAAoB,EAAC,MAAM,uBAAuB,CAAC;AAE3D,OAAO,EAAC,aAAa,EAAC,MAAM,0BAA0B,CAAC;AAGvD,IAAa,YAAY,GAAzB,MAAa,YAAY;
|
|
1
|
+
{"version":3,"file":"AgendaModule.js","sourceRoot":"","sources":["../../src/AgendaModule.ts"],"names":[],"mappings":";AAAA,OAAO,EAAc,MAAM,EAAY,MAAM,cAAc,CAAC;AAC5D,OAAO,EAA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C,aAAa,CAAC,WAAW,EAAE,KAAK,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;YAEvE,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,EAAE,OAAO,EAAE,EAAE,EAAE,OAAO,CAAC,CAAC;QAC3D,CAAC,CAAC,CAAC;QAEH,MAAM,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC9B,CAAC;IAES,aAAa,CAAC,WAAmB,EAAE,SAAkB,EAAE,UAAmB;QAClF,MAAM,IAAI,GAAG,UAAU,IAAI,WAAW,CAAC;QACvC,OAAO,SAAS,CAAC,CAAC,CAAC,GAAG,SAAS,IAAI,IAAI,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC;IACnD,CAAC;CACF,CAAA;AA3EC;IADC,MAAM,EAAE;8BACS,MAAM;4CAAC;AAGzB;IADC,MAAM,EAAE;8BACW,eAAe;8CAAC;AAGpC;IADC,MAAM,EAAE;8BACS,aAAa;4CAAC;AAGhC;IADC,QAAQ,CAAC,gBAAgB,EAAE,KAAK,CAAC;;gDACN;AAXjB,YAAY;IADxB,MAAM,EAAE;GACI,YAAY,CA6ExB;SA7EY,YAAY","sourcesContent":["import {AfterListen, Logger, OnDestroy} from \"@tsed/common\";\nimport {Constant, Inject, InjectorService, Module, Provider} from \"@tsed/di\";\nimport {Processor} from \"agenda\";\nimport {PROVIDER_TYPE_AGENDA} from \"./constants/constants\";\nimport {AgendaStore} from \"./interfaces/AgendaStore\";\nimport {AgendaService} from \"./services/AgendaFactory\";\n\n@Module()\nexport class AgendaModule implements OnDestroy, AfterListen {\n @Inject()\n protected logger: Logger;\n\n @Inject()\n protected injector: InjectorService;\n\n @Inject()\n protected agenda: AgendaService;\n\n @Constant(\"agenda.enabled\", false)\n private loadAgenda: boolean;\n\n async $afterListen(): Promise<any> {\n if (this.loadAgenda) {\n const providers = this.getProviders();\n providers.forEach((provider) => this.addAgendaDefinitionsForProvider(provider));\n\n this.logger.info(\"Agenda add definitions...\");\n await this.agenda.start();\n\n this.logger.info(\"Agenda add scheduled jobs...\");\n await Promise.all(providers.map((provider) => this.scheduleJobsForProvider(provider)));\n } else {\n this.logger.info(\"Agenda jobs disabled...\");\n }\n }\n\n async $onDestroy(): Promise<any> {\n if (this.loadAgenda) {\n await this.agenda.stop();\n await this.agenda.close({force: true});\n\n this.logger.info(\"Agenda jobs stopped...\");\n }\n }\n\n protected getProviders(): Provider<any>[] {\n return this.injector.getProviders(PROVIDER_TYPE_AGENDA);\n }\n\n protected addAgendaDefinitionsForProvider(provider: Provider): void {\n const store = provider.store.get<AgendaStore>(\"agenda\", {});\n\n if (!store.define) {\n return;\n }\n\n Object.entries(store.define).forEach(([propertyKey, {name, ...options}]) => {\n const instance = this.injector.get(provider.token);\n\n const jobProcessor: Processor = instance[propertyKey].bind(instance) as Processor;\n const jobName = this.getNameForJob(propertyKey, store.namespace, name);\n this.agenda.define(jobName, options, jobProcessor);\n });\n }\n\n protected async scheduleJobsForProvider(provider: Provider<any>): Promise<void> {\n const store = provider.store.get<AgendaStore>(\"agenda\", {});\n\n if (!store.every) {\n return;\n }\n\n const promises = Object.entries(store.every).map(([propertyKey, {interval, name, ...options}]) => {\n const jobName = this.getNameForJob(propertyKey, store.namespace, name);\n\n return this.agenda.every(interval, jobName, {}, options);\n });\n\n await Promise.all(promises);\n }\n\n protected getNameForJob(propertyKey: string, namespace?: string, customName?: string): string {\n const name = customName || propertyKey;\n return namespace ? `${namespace}.${name}` : name;\n }\n}\n"]}
|
|
@@ -2,7 +2,7 @@ import { StoreMerge, useDecorators } from "@tsed/core";
|
|
|
2
2
|
import { Injectable } from "@tsed/di";
|
|
3
3
|
import { PROVIDER_TYPE_AGENDA } from "../constants/constants.js";
|
|
4
4
|
export function Agenda(options) {
|
|
5
|
-
return useDecorators(StoreMerge("agenda",
|
|
5
|
+
return useDecorators(options?.namespace && StoreMerge("agenda", options), Injectable({
|
|
6
6
|
type: PROVIDER_TYPE_AGENDA
|
|
7
7
|
}));
|
|
8
8
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agenda.js","sourceRoot":"","sources":["../../../src/decorators/agenda.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAE,aAAa,EAAC,MAAM,YAAY,CAAC;AACrD,OAAO,EAAC,UAAU,EAAC,MAAM,UAAU,CAAC;AAEpC,OAAO,EAAC,oBAAoB,EAAC,MAAM,wBAAwB,CAAC;AAiB5D,MAAM,UAAU,MAAM,CAAC,OAAuB;IAC5C,OAAO,aAAa,CAClB,
|
|
1
|
+
{"version":3,"file":"agenda.js","sourceRoot":"","sources":["../../../src/decorators/agenda.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAE,aAAa,EAAC,MAAM,YAAY,CAAC;AACrD,OAAO,EAAC,UAAU,EAAC,MAAM,UAAU,CAAC;AAEpC,OAAO,EAAC,oBAAoB,EAAC,MAAM,wBAAwB,CAAC;AAiB5D,MAAM,UAAU,MAAM,CAAC,OAAuB;IAC5C,OAAO,aAAa,CAClB,OAAO,EAAE,SAAS,IAAI,UAAU,CAAC,QAAQ,EAAE,OAAO,CAAC,EACnD,UAAU,CAAC;QACT,IAAI,EAAE,oBAAoB;KAC3B,CAAC,CACH,CAAC;AACJ,CAAC","sourcesContent":["import {StoreMerge, useDecorators} from \"@tsed/core\";\nimport {Injectable} from \"@tsed/di\";\nimport {AgendaConfig} from \"agenda\";\nimport {PROVIDER_TYPE_AGENDA} from \"../constants/constants\";\n\ndeclare global {\n namespace TsED {\n interface Configuration {\n agenda?: {\n /** * Enable Agenda jobs. Default false. */\n enabled?: boolean;\n } & AgendaConfig;\n }\n }\n}\n\ninterface AgendaOptions {\n namespace?: string;\n}\n\nexport function Agenda(options?: AgendaOptions): ClassDecorator {\n return useDecorators(\n options?.namespace && StoreMerge(\"agenda\", options),\n Injectable({\n type: PROVIDER_TYPE_AGENDA\n })\n );\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsed/agenda",
|
|
3
|
-
"version": "7.0.0-beta.
|
|
3
|
+
"version": "7.0.0-beta.14",
|
|
4
4
|
"description": "Agenda package for Ts.ED framework",
|
|
5
5
|
"source": "./src/index.ts",
|
|
6
6
|
"main": "./lib/cjs/index.js",
|
|
@@ -17,16 +17,16 @@
|
|
|
17
17
|
"build:cjs": "tsc --build tsconfig.compile.json",
|
|
18
18
|
"build:esm": "tsc --build tsconfig.compile.esm.json",
|
|
19
19
|
"barrels": "yarn barrelsby --delete -d ./src -e \"\\.spec\\.ts\" -e \"__mock__\" -e \".benchmark.ts\"",
|
|
20
|
-
"test": "cross-env NODE_ENV=test
|
|
20
|
+
"test": "cross-env NODE_ENV=test yarn jest --runInBand"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"tslib": "2.
|
|
23
|
+
"tslib": "2.4.0"
|
|
24
24
|
},
|
|
25
25
|
"private": false,
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@tsed/common": "7.0.0-beta.
|
|
28
|
-
"@tsed/core": "7.0.0-beta.
|
|
29
|
-
"@tsed/di": "7.0.0-beta.
|
|
27
|
+
"@tsed/common": "7.0.0-beta.14",
|
|
28
|
+
"@tsed/core": "7.0.0-beta.14",
|
|
29
|
+
"@tsed/di": "7.0.0-beta.14",
|
|
30
30
|
"agenda": "^4.1.3"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
@@ -38,5 +38,8 @@
|
|
|
38
38
|
},
|
|
39
39
|
"homepage": "https://github.com/tsedio/tsed/tree/beta/packages/third-parties/agenda",
|
|
40
40
|
"author": "Romain Lenzotti",
|
|
41
|
-
"license": "MIT"
|
|
41
|
+
"license": "MIT",
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"tag": "beta"
|
|
44
|
+
}
|
|
42
45
|
}
|
package/readme.md
CHANGED
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
[](https://badge.fury.io/js/%40tsed%2Fcommon)
|
|
13
13
|
[](https://github.com/semantic-release/semantic-release)
|
|
14
14
|
[](https://github.com/prettier/prettier)
|
|
15
|
-
[](https://github.com/sponsors/romakita)
|
|
16
|
+
[](https://opencollective.com/tsed)
|
|
16
17
|
|
|
17
18
|
</div>
|
|
18
19
|
|
package/src/AgendaModule.ts
CHANGED
|
@@ -28,7 +28,7 @@ export class AgendaModule implements OnDestroy, AfterListen {
|
|
|
28
28
|
await this.agenda.start();
|
|
29
29
|
|
|
30
30
|
this.logger.info("Agenda add scheduled jobs...");
|
|
31
|
-
providers.
|
|
31
|
+
await Promise.all(providers.map((provider) => this.scheduleJobsForProvider(provider)));
|
|
32
32
|
} else {
|
|
33
33
|
this.logger.info("Agenda jobs disabled...");
|
|
34
34
|
}
|
|
@@ -48,36 +48,35 @@ export class AgendaModule implements OnDestroy, AfterListen {
|
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
protected addAgendaDefinitionsForProvider(provider: Provider): void {
|
|
51
|
-
const store = provider.store.get<AgendaStore>("agenda");
|
|
51
|
+
const store = provider.store.get<AgendaStore>("agenda", {});
|
|
52
52
|
|
|
53
53
|
if (!store.define) {
|
|
54
54
|
return;
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
for (const [propertyKey, {name, ...options}] of jobsToDefine) {
|
|
57
|
+
Object.entries(store.define).forEach(([propertyKey, {name, ...options}]) => {
|
|
60
58
|
const instance = this.injector.get(provider.token);
|
|
61
59
|
|
|
62
60
|
const jobProcessor: Processor = instance[propertyKey].bind(instance) as Processor;
|
|
63
61
|
const jobName = this.getNameForJob(propertyKey, store.namespace, name);
|
|
64
62
|
this.agenda.define(jobName, options, jobProcessor);
|
|
65
|
-
}
|
|
63
|
+
});
|
|
66
64
|
}
|
|
67
65
|
|
|
68
66
|
protected async scheduleJobsForProvider(provider: Provider<any>): Promise<void> {
|
|
69
|
-
const store = provider.store.get<AgendaStore>("agenda");
|
|
67
|
+
const store = provider.store.get<AgendaStore>("agenda", {});
|
|
68
|
+
|
|
70
69
|
if (!store.every) {
|
|
71
70
|
return;
|
|
72
71
|
}
|
|
73
72
|
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
);
|
|
73
|
+
const promises = Object.entries(store.every).map(([propertyKey, {interval, name, ...options}]) => {
|
|
74
|
+
const jobName = this.getNameForJob(propertyKey, store.namespace, name);
|
|
75
|
+
|
|
76
|
+
return this.agenda.every(interval, jobName, {}, options);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
await Promise.all(promises);
|
|
81
80
|
}
|
|
82
81
|
|
|
83
82
|
protected getNameForJob(propertyKey: string, namespace?: string, customName?: string): string {
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import {Store} from "@tsed/core";
|
|
2
|
-
import {expect} from "chai";
|
|
3
2
|
import {Agenda} from "./agenda";
|
|
4
3
|
|
|
5
4
|
describe("@Agenda()", () => {
|
|
@@ -8,16 +7,14 @@ describe("@Agenda()", () => {
|
|
|
8
7
|
class Test {}
|
|
9
8
|
|
|
10
9
|
const store = Store.from(Test);
|
|
11
|
-
expect(store.get("agenda")).
|
|
12
|
-
namespace: undefined
|
|
13
|
-
});
|
|
10
|
+
expect(store.get("agenda")).toEqual(undefined);
|
|
14
11
|
});
|
|
15
12
|
it("should set namespace metadata", () => {
|
|
16
13
|
@Agenda({namespace: "nsp1"})
|
|
17
14
|
class Test {}
|
|
18
15
|
|
|
19
16
|
const store = Store.from(Test);
|
|
20
|
-
expect(store.get("agenda")).
|
|
17
|
+
expect(store.get("agenda")).toEqual({
|
|
21
18
|
namespace: "nsp1"
|
|
22
19
|
});
|
|
23
20
|
});
|
package/src/decorators/agenda.ts
CHANGED
|
@@ -20,7 +20,7 @@ interface AgendaOptions {
|
|
|
20
20
|
|
|
21
21
|
export function Agenda(options?: AgendaOptions): ClassDecorator {
|
|
22
22
|
return useDecorators(
|
|
23
|
-
StoreMerge("agenda",
|
|
23
|
+
options?.namespace && StoreMerge("agenda", options),
|
|
24
24
|
Injectable({
|
|
25
25
|
type: PROVIDER_TYPE_AGENDA
|
|
26
26
|
})
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import {Store} from "@tsed/core";
|
|
2
|
-
import {expect} from "chai";
|
|
3
2
|
import {Agenda} from "./agenda";
|
|
4
3
|
import {Define} from "./define";
|
|
5
4
|
|
|
@@ -20,7 +19,7 @@ describe("@Define()", () => {
|
|
|
20
19
|
|
|
21
20
|
const store = Store.from(Test);
|
|
22
21
|
|
|
23
|
-
expect(store.get("agenda").define).
|
|
22
|
+
expect(store.get("agenda").define).toEqual({
|
|
24
23
|
testDefineDecorator: {},
|
|
25
24
|
testDefineDecorator2: {}
|
|
26
25
|
});
|
|
@@ -39,7 +38,7 @@ describe("@Define()", () => {
|
|
|
39
38
|
}
|
|
40
39
|
|
|
41
40
|
const store = Store.from(Test);
|
|
42
|
-
expect(store.get("agenda").define).
|
|
41
|
+
expect(store.get("agenda").define).toEqual({
|
|
43
42
|
test: {
|
|
44
43
|
name: "testDefineDecoratorCustomName",
|
|
45
44
|
priority: 20
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import {descriptorOf, Store} from "@tsed/core";
|
|
2
|
-
import {expect} from "chai";
|
|
3
2
|
import {Agenda} from "./agenda";
|
|
4
3
|
import {Every} from "./every";
|
|
5
4
|
|
|
@@ -20,12 +19,12 @@ describe("@Every()", () => {
|
|
|
20
19
|
|
|
21
20
|
const store = Store.from(Test);
|
|
22
21
|
|
|
23
|
-
expect(store.get("agenda").define).
|
|
22
|
+
expect(store.get("agenda").define).toEqual({
|
|
24
23
|
testEveryDecorator: {},
|
|
25
24
|
testEveryDecorator2: {}
|
|
26
25
|
});
|
|
27
26
|
|
|
28
|
-
expect(store.get("agenda").every).
|
|
27
|
+
expect(store.get("agenda").every).toEqual({
|
|
29
28
|
testEveryDecorator: {
|
|
30
29
|
interval: "60 seconds"
|
|
31
30
|
},
|
package/.mocharc.js
DELETED
package/.nycrc
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"include": [
|
|
3
|
-
"src/**/*.ts"
|
|
4
|
-
],
|
|
5
|
-
"exclude": [
|
|
6
|
-
"**/*.spec.ts",
|
|
7
|
-
"**/*.d.ts",
|
|
8
|
-
"node_modules",
|
|
9
|
-
"**/interfaces/**",
|
|
10
|
-
"**/index.ts"
|
|
11
|
-
],
|
|
12
|
-
"extension": [
|
|
13
|
-
".ts"
|
|
14
|
-
],
|
|
15
|
-
"require": [],
|
|
16
|
-
"reporter": [
|
|
17
|
-
"text-summary",
|
|
18
|
-
"html",
|
|
19
|
-
"lcov",
|
|
20
|
-
"json"
|
|
21
|
-
],
|
|
22
|
-
"check-coverage": true,
|
|
23
|
-
"statements": 100,
|
|
24
|
-
"branches": 90.62,
|
|
25
|
-
"functions": 100,
|
|
26
|
-
"lines": 100
|
|
27
|
-
}
|