@navios/schedule 0.4.0 → 0.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.
Files changed (40) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/LICENSE +7 -0
  3. package/README.md +221 -317
  4. package/dist/src/cron.constants.d.mts +25 -0
  5. package/dist/src/cron.constants.d.mts.map +1 -1
  6. package/dist/src/decorators/cron.decorator.d.mts +48 -0
  7. package/dist/src/decorators/cron.decorator.d.mts.map +1 -1
  8. package/dist/src/decorators/schedulable.decorator.d.mts +25 -0
  9. package/dist/src/decorators/schedulable.decorator.d.mts.map +1 -1
  10. package/dist/src/scheduler.service.d.mts +102 -0
  11. package/dist/src/scheduler.service.d.mts.map +1 -1
  12. package/dist/tsconfig.tsbuildinfo +1 -1
  13. package/lib/index.cjs +626 -0
  14. package/lib/index.cjs.map +1 -0
  15. package/lib/index.d.cts +264 -0
  16. package/lib/index.d.cts.map +1 -0
  17. package/lib/index.d.mts +264 -14
  18. package/lib/index.d.mts.map +1 -0
  19. package/lib/index.mjs +592 -209
  20. package/lib/index.mjs.map +1 -1
  21. package/package.json +6 -6
  22. package/project.json +2 -2
  23. package/src/__tests__/schedule.spec.mts +5 -3
  24. package/src/cron.constants.mts +25 -0
  25. package/src/decorators/cron.decorator.mts +48 -1
  26. package/src/decorators/schedulable.decorator.mts +25 -0
  27. package/src/scheduler.service.mts +105 -10
  28. package/tsdown.config.mts +33 -0
  29. package/dist/tsdown.config.d.mts +0 -3
  30. package/dist/tsdown.config.d.mts.map +0 -1
  31. package/dist/tsup.config.d.mts +0 -3
  32. package/dist/tsup.config.d.mts.map +0 -1
  33. package/dist/vitest.config.d.mts +0 -3
  34. package/dist/vitest.config.d.mts.map +0 -1
  35. package/lib/_tsup-dts-rollup.d.mts +0 -105
  36. package/lib/_tsup-dts-rollup.d.ts +0 -105
  37. package/lib/index.d.ts +0 -14
  38. package/lib/index.js +0 -240
  39. package/lib/index.js.map +0 -1
  40. package/tsup.config.mts +0 -12
@@ -1,105 +0,0 @@
1
- import type { ClassType } from '@navios/core';
2
- import { CronJob } from 'cron';
3
- import type { CronJobParams } from 'cron';
4
-
5
- declare function Cron(cronTime: CronJobParams['cronTime'], options?: CronOptions): (target: () => Promise<void>, context: ClassMethodDecoratorContext) => () => Promise<void>;
6
- export { Cron }
7
- export { Cron as Cron_alias_1 }
8
- export { Cron as Cron_alias_2 }
9
-
10
- declare interface CronMetadata {
11
- classMethod: string;
12
- cronTime: CronJobParams['cronTime'] | null;
13
- disabled: boolean;
14
- }
15
- export { CronMetadata }
16
- export { CronMetadata as CronMetadata_alias_1 }
17
- export { CronMetadata as CronMetadata_alias_2 }
18
-
19
- declare const CronMetadataKey: unique symbol;
20
- export { CronMetadataKey }
21
- export { CronMetadataKey as CronMetadataKey_alias_1 }
22
- export { CronMetadataKey as CronMetadataKey_alias_2 }
23
-
24
- declare interface CronOptions {
25
- disabled?: boolean;
26
- }
27
- export { CronOptions }
28
- export { CronOptions as CronOptions_alias_1 }
29
- export { CronOptions as CronOptions_alias_2 }
30
-
31
- declare function extractScheduleMetadata(target: ClassType): ScheduleMetadata;
32
- export { extractScheduleMetadata }
33
- export { extractScheduleMetadata as extractScheduleMetadata_alias_1 }
34
- export { extractScheduleMetadata as extractScheduleMetadata_alias_2 }
35
-
36
- declare function getAllCronMetadata(context: ClassMethodDecoratorContext | ClassDecoratorContext): Set<CronMetadata>;
37
- export { getAllCronMetadata }
38
- export { getAllCronMetadata as getAllCronMetadata_alias_1 }
39
- export { getAllCronMetadata as getAllCronMetadata_alias_2 }
40
-
41
- declare function getCronMetadata(target: Function, context: ClassMethodDecoratorContext): CronMetadata;
42
- export { getCronMetadata }
43
- export { getCronMetadata as getCronMetadata_alias_1 }
44
- export { getCronMetadata as getCronMetadata_alias_2 }
45
-
46
- declare function getScheduleMetadata(target: ClassType, context: ClassDecoratorContext): ScheduleMetadata;
47
- export { getScheduleMetadata }
48
- export { getScheduleMetadata as getScheduleMetadata_alias_1 }
49
- export { getScheduleMetadata as getScheduleMetadata_alias_2 }
50
-
51
- declare function hasScheduleMetadata(target: ClassType): boolean;
52
- export { hasScheduleMetadata }
53
- export { hasScheduleMetadata as hasScheduleMetadata_alias_1 }
54
- export { hasScheduleMetadata as hasScheduleMetadata_alias_2 }
55
-
56
- declare function Schedulable(): (target: ClassType, context: ClassDecoratorContext) => ClassType;
57
- export { Schedulable }
58
- export { Schedulable as Schedulable_alias_1 }
59
- export { Schedulable as Schedulable_alias_2 }
60
-
61
- declare enum Schedule {
62
- EveryMinute = "*/1 * * * *",
63
- EveryFiveMinutes = "*/5 * * * *",
64
- EveryTenMinutes = "*/10 * * * *",
65
- EveryFifteenMinutes = "*/15 * * * *",
66
- EveryThirtyMinutes = "*/30 * * * *",
67
- EveryHour = "0 * * * *",
68
- EveryTwoHours = "0 */2 * * *",
69
- EveryThreeHours = "0 */3 * * *",
70
- EveryFourHours = "0 */4 * * *",
71
- EverySixHours = "0 */6 * * *",
72
- EveryTwelveHours = "0 */12 * * *",
73
- EveryDay = "0 0 * * *",
74
- EveryWeek = "0 0 * * 0",
75
- EveryMonth = "0 0 1 * *"
76
- }
77
- export { Schedule }
78
- export { Schedule as Schedule_alias_1 }
79
-
80
- declare interface ScheduleMetadata {
81
- name: string;
82
- jobs: Set<CronMetadata>;
83
- }
84
- export { ScheduleMetadata }
85
- export { ScheduleMetadata as ScheduleMetadata_alias_1 }
86
- export { ScheduleMetadata as ScheduleMetadata_alias_2 }
87
-
88
- declare const ScheduleMetadataKey: unique symbol;
89
- export { ScheduleMetadataKey }
90
- export { ScheduleMetadataKey as ScheduleMetadataKey_alias_1 }
91
- export { ScheduleMetadataKey as ScheduleMetadataKey_alias_2 }
92
-
93
- declare class SchedulerService {
94
- private readonly logger;
95
- private readonly jobs;
96
- register(service: ClassType): void;
97
- getJob<T extends ClassType>(service: T, method: keyof InstanceType<T>): CronJob | undefined;
98
- private registerJobs;
99
- startAll(): void;
100
- stopAll(): void;
101
- }
102
- export { SchedulerService }
103
- export { SchedulerService as SchedulerService_alias_1 }
104
-
105
- export { }
package/lib/index.d.ts DELETED
@@ -1,14 +0,0 @@
1
- export { Cron } from './_tsup-dts-rollup.js';
2
- export { CronOptions } from './_tsup-dts-rollup.js';
3
- export { Schedulable } from './_tsup-dts-rollup.js';
4
- export { getAllCronMetadata } from './_tsup-dts-rollup.js';
5
- export { getCronMetadata } from './_tsup-dts-rollup.js';
6
- export { CronMetadataKey } from './_tsup-dts-rollup.js';
7
- export { CronMetadata } from './_tsup-dts-rollup.js';
8
- export { getScheduleMetadata } from './_tsup-dts-rollup.js';
9
- export { extractScheduleMetadata } from './_tsup-dts-rollup.js';
10
- export { hasScheduleMetadata } from './_tsup-dts-rollup.js';
11
- export { ScheduleMetadataKey } from './_tsup-dts-rollup.js';
12
- export { ScheduleMetadata } from './_tsup-dts-rollup.js';
13
- export { Schedule_alias_1 as Schedule } from './_tsup-dts-rollup.js';
14
- export { SchedulerService } from './_tsup-dts-rollup.js';
package/lib/index.js DELETED
@@ -1,240 +0,0 @@
1
- 'use strict';
2
-
3
- var core = require('@navios/core');
4
- var cron = require('cron');
5
-
6
- var __create = Object.create;
7
- var __defProp = Object.defineProperty;
8
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
9
- var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
10
- var __typeError = (msg) => {
11
- throw TypeError(msg);
12
- };
13
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
14
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
15
- var __decoratorStart = (base) => [, , , __create(null)];
16
- var __decoratorStrings = ["class", "method", "getter", "setter", "accessor", "field", "value", "get", "set"];
17
- var __expectFn = (fn) => fn !== void 0 && typeof fn !== "function" ? __typeError("Function expected") : fn;
18
- var __decoratorContext = (kind, name, done, metadata, fns) => ({ kind: __decoratorStrings[kind], name, metadata, addInitializer: (fn) => done._ ? __typeError("Already initialized") : fns.push(__expectFn(fn || null)) });
19
- var __decoratorMetadata = (array, target) => __defNormalProp(target, __knownSymbol("metadata"), array[3]);
20
- var __runInitializers = (array, flags, self, value) => {
21
- for (var i = 0, fns = array[flags >> 1], n = fns && fns.length; i < n; i++) fns[i].call(self) ;
22
- return value;
23
- };
24
- var __decorateElement = (array, flags, name, decorators, target, extra) => {
25
- var it, done, ctx, k = flags & 7, p = false;
26
- var j = 0;
27
- var extraInitializers = array[j] || (array[j] = []);
28
- var desc = k && ((target = target.prototype), k < 5 && (k > 3 || !p) && __getOwnPropDesc(target , name));
29
- __name(target, name);
30
- for (var i = decorators.length - 1; i >= 0; i--) {
31
- ctx = __decoratorContext(k, name, done = {}, array[3], extraInitializers);
32
- it = (0, decorators[i])(target, ctx), done._ = 1;
33
- __expectFn(it) && (target = it);
34
- }
35
- return __decoratorMetadata(array, target), desc && __defProp(target, name, desc), p ? k ^ 4 ? extra : desc : target;
36
- };
37
-
38
- // src/metadata/cron.metadata.mts
39
- var CronMetadataKey = Symbol("CronMetadataKey");
40
- function getAllCronMetadata(context) {
41
- if (context.metadata) {
42
- const metadata = context.metadata[CronMetadataKey];
43
- if (metadata) {
44
- return metadata;
45
- } else {
46
- context.metadata[CronMetadataKey] = /* @__PURE__ */ new Set();
47
- return context.metadata[CronMetadataKey];
48
- }
49
- }
50
- throw new Error("[Navios-Schedule] Wrong environment.");
51
- }
52
- function getCronMetadata(target, context) {
53
- if (context.metadata) {
54
- const metadata = getAllCronMetadata(context);
55
- if (metadata) {
56
- const endpointMetadata = Array.from(metadata).find(
57
- (item) => item.classMethod === target.name
58
- );
59
- if (endpointMetadata) {
60
- return endpointMetadata;
61
- } else {
62
- const newMetadata = {
63
- classMethod: target.name,
64
- cronTime: null,
65
- disabled: false
66
- };
67
- metadata.add(newMetadata);
68
- return newMetadata;
69
- }
70
- }
71
- }
72
- throw new Error("[Navios-Schedule] Wrong environment.");
73
- }
74
-
75
- // src/metadata/schedule.metadata.mts
76
- var ScheduleMetadataKey = Symbol("ControllerMetadataKey");
77
- function getScheduleMetadata(target, context) {
78
- if (context.metadata) {
79
- const metadata = context.metadata[ScheduleMetadataKey];
80
- if (metadata) {
81
- return metadata;
82
- } else {
83
- const jobsMetadata = getAllCronMetadata(context);
84
- const newMetadata = {
85
- name: target.name,
86
- jobs: jobsMetadata
87
- };
88
- context.metadata[ScheduleMetadataKey] = newMetadata;
89
- target[ScheduleMetadataKey] = newMetadata;
90
- return newMetadata;
91
- }
92
- }
93
- throw new Error("[Navios-Schedule] Wrong environment.");
94
- }
95
- function extractScheduleMetadata(target) {
96
- const metadata = target[ScheduleMetadataKey];
97
- if (!metadata) {
98
- throw new Error(
99
- "[Navios-Schedule] Controller metadata not found. Make sure to use @Controller decorator."
100
- );
101
- }
102
- return metadata;
103
- }
104
- function hasScheduleMetadata(target) {
105
- const metadata = target[ScheduleMetadataKey];
106
- return !!metadata;
107
- }
108
-
109
- // src/decorators/cron.decorator.mts
110
- function Cron(cronTime, options) {
111
- return (target, context) => {
112
- if (context.kind !== "method") {
113
- throw new Error(
114
- `Cron can only be applied to methods, not ${context.kind}`
115
- );
116
- }
117
- if (context.metadata) {
118
- const metadata = getCronMetadata(target, context);
119
- metadata.cronTime = cronTime;
120
- metadata.disabled = options?.disabled ?? false;
121
- }
122
- return target;
123
- };
124
- }
125
- function Schedulable() {
126
- return (target, context) => {
127
- if (context.kind !== "class") {
128
- throw new Error(
129
- `SchedulableDecorator can only be applied to classes, not ${context.kind}`
130
- );
131
- }
132
- if (context.metadata) {
133
- getScheduleMetadata(target, context);
134
- }
135
- return core.Injectable()(target, context);
136
- };
137
- }
138
-
139
- // src/cron.constants.mts
140
- var Schedule = /* @__PURE__ */ ((Schedule2) => {
141
- Schedule2["EveryMinute"] = "*/1 * * * *";
142
- Schedule2["EveryFiveMinutes"] = "*/5 * * * *";
143
- Schedule2["EveryTenMinutes"] = "*/10 * * * *";
144
- Schedule2["EveryFifteenMinutes"] = "*/15 * * * *";
145
- Schedule2["EveryThirtyMinutes"] = "*/30 * * * *";
146
- Schedule2["EveryHour"] = "0 * * * *";
147
- Schedule2["EveryTwoHours"] = "0 */2 * * *";
148
- Schedule2["EveryThreeHours"] = "0 */3 * * *";
149
- Schedule2["EveryFourHours"] = "0 */4 * * *";
150
- Schedule2["EverySixHours"] = "0 */6 * * *";
151
- Schedule2["EveryTwelveHours"] = "0 */12 * * *";
152
- Schedule2["EveryDay"] = "0 0 * * *";
153
- Schedule2["EveryWeek"] = "0 0 * * 0";
154
- Schedule2["EveryMonth"] = "0 0 1 * *";
155
- return Schedule2;
156
- })(Schedule || {});
157
- var _SchedulerService_decorators, _init;
158
- _SchedulerService_decorators = [core.Injectable()];
159
- var _SchedulerService = class _SchedulerService {
160
- // private readonly configService = syncInject(EnvConfigProvider)
161
- logger = core.syncInject(core.Logger, {
162
- context: _SchedulerService.name
163
- });
164
- jobs = /* @__PURE__ */ new Map();
165
- register(service) {
166
- if (!hasScheduleMetadata(service)) {
167
- throw new Error(
168
- `[Navios-Schedule] Service ${service.name} is not schedulable. Make sure to use @Schedulable decorator.`
169
- );
170
- }
171
- const metadata = extractScheduleMetadata(service);
172
- this.logger.debug("Scheduling service", metadata.name);
173
- this.registerJobs(service, metadata);
174
- }
175
- getJob(service, method) {
176
- const metadata = extractScheduleMetadata(service);
177
- const jobName = `${metadata.name}.${method}()`;
178
- return this.jobs.get(jobName);
179
- }
180
- registerJobs(service, metadata) {
181
- const jobs = metadata.jobs;
182
- for (const job of jobs) {
183
- if (!job.cronTime) {
184
- this.logger.debug("Skipping job", job.classMethod);
185
- continue;
186
- }
187
- const name = `${metadata.name}.${job.classMethod}()`;
188
- const self = this;
189
- const cronJob = cron.CronJob.from({
190
- cronTime: job.cronTime,
191
- name,
192
- async onTick() {
193
- try {
194
- self.logger.debug("Executing job", name);
195
- const instance = await core.inject(service);
196
- await instance[job.classMethod]();
197
- } catch (error) {
198
- self.logger.error("Error executing job", name, error);
199
- }
200
- },
201
- start: !(job.disabled)
202
- });
203
- this.jobs.set(name, cronJob);
204
- }
205
- }
206
- startAll() {
207
- for (const job of this.jobs.values()) {
208
- if (job.isActive) {
209
- continue;
210
- }
211
- job.start();
212
- }
213
- }
214
- stopAll() {
215
- for (const job of this.jobs.values()) {
216
- if (!job.isActive) {
217
- continue;
218
- }
219
- job.stop();
220
- }
221
- }
222
- };
223
- _init = __decoratorStart();
224
- _SchedulerService = __decorateElement(_init, 0, "SchedulerService", _SchedulerService_decorators, _SchedulerService);
225
- __runInitializers(_init, 1, _SchedulerService);
226
- var SchedulerService = _SchedulerService;
227
-
228
- exports.Cron = Cron;
229
- exports.CronMetadataKey = CronMetadataKey;
230
- exports.Schedulable = Schedulable;
231
- exports.Schedule = Schedule;
232
- exports.ScheduleMetadataKey = ScheduleMetadataKey;
233
- exports.SchedulerService = SchedulerService;
234
- exports.extractScheduleMetadata = extractScheduleMetadata;
235
- exports.getAllCronMetadata = getAllCronMetadata;
236
- exports.getCronMetadata = getCronMetadata;
237
- exports.getScheduleMetadata = getScheduleMetadata;
238
- exports.hasScheduleMetadata = hasScheduleMetadata;
239
- //# sourceMappingURL=index.js.map
240
- //# sourceMappingURL=index.js.map
package/lib/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/metadata/cron.metadata.mts","../src/metadata/schedule.metadata.mts","../src/decorators/cron.decorator.mts","../src/decorators/schedulable.decorator.mts","../src/cron.constants.mts","../src/scheduler.service.mts"],"names":["Injectable","Schedule","syncInject","Logger","CronJob","inject"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEO,IAAM,eAAA,GAAkB,OAAO,iBAAiB;AAQhD,SAAS,mBACd,OAAA,EACmB;AACnB,EAAA,IAAI,QAAQ,QAAA,EAAU;AACpB,IAAA,MAAM,QAAA,GAAW,OAAA,CAAQ,QAAA,CAAS,eAAe,CAAA;AAGjD,IAAA,IAAI,QAAA,EAAU;AACZ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{ CronJobParams } from 'cron'\n\nexport const CronMetadataKey = Symbol('CronMetadataKey')\n\nexport interface CronMetadata {\n classMethod: string\n cronTime: CronJobParams['cronTime'] | null\n disabled: boolean\n}\n\nexport function getAllCronMetadata(\n context: ClassMethodDecoratorContext | ClassDecoratorContext,\n): Set<CronMetadata> {\n if (context.metadata) {\n const metadata = context.metadata[CronMetadataKey] as\n | Set<CronMetadata>\n | undefined\n if (metadata) {\n return metadata\n } else {\n context.metadata[CronMetadataKey] = new Set<CronMetadata>()\n return context.metadata[CronMetadataKey] as Set<CronMetadata>\n }\n }\n throw new Error('[Navios-Schedule] Wrong environment.')\n}\n\nexport function getCronMetadata(\n target: Function,\n context: ClassMethodDecoratorContext,\n): CronMetadata {\n if (context.metadata) {\n const metadata = getAllCronMetadata(context)\n if (metadata) {\n const endpointMetadata = Array.from(metadata).find(\n (item) => item.classMethod === target.name,\n )\n if (endpointMetadata) {\n return endpointMetadata\n } else {\n const newMetadata: CronMetadata = {\n classMethod: target.name,\n cronTime: null,\n disabled: false,\n }\n metadata.add(newMetadata)\n return newMetadata\n }\n }\n }\n throw new Error('[Navios-Schedule] Wrong environment.')\n}\n","import type { ClassType } from '@navios/core'\n\nimport type { CronMetadata } from './cron.metadata.mjs'\n\nimport { getAllCronMetadata } from './cron.metadata.mjs'\n\nexport const ScheduleMetadataKey = Symbol('ControllerMetadataKey')\n\nexport interface ScheduleMetadata {\n name: string\n jobs: Set<CronMetadata>\n}\n\nexport function getScheduleMetadata(\n target: ClassType,\n context: ClassDecoratorContext,\n): ScheduleMetadata {\n if (context.metadata) {\n const metadata = context.metadata[ScheduleMetadataKey] as\n | ScheduleMetadata\n | undefined\n if (metadata) {\n return metadata\n } else {\n const jobsMetadata = getAllCronMetadata(context)\n const newMetadata: ScheduleMetadata = {\n name: target.name,\n jobs: jobsMetadata,\n }\n context.metadata[ScheduleMetadataKey] = newMetadata\n // @ts-expect-error We add a custom metadata key to the target\n target[ScheduleMetadataKey] = newMetadata\n return newMetadata\n }\n }\n throw new Error('[Navios-Schedule] Wrong environment.')\n}\n\nexport function extractScheduleMetadata(target: ClassType): ScheduleMetadata {\n // @ts-expect-error We add a custom metadata key to the target\n const metadata = target[ScheduleMetadataKey] as ScheduleMetadata | undefined\n if (!metadata) {\n throw new Error(\n '[Navios-Schedule] Controller metadata not found. Make sure to use @Controller decorator.',\n )\n }\n return metadata\n}\n\nexport function hasScheduleMetadata(target: ClassType): boolean {\n // @ts-expect-error We add a custom metadata key to the target\n const metadata = target[ScheduleMetadataKey] as ScheduleMetadata | undefined\n\n return !!metadata\n}\n","import type { ClassType } from '@navios/core'\nimport type { CronJobParams } from 'cron'\n\nimport { getCronMetadata } from '../metadata/index.mjs'\n\nexport interface CronOptions {\n disabled?: boolean\n}\n\nexport function Cron(\n cronTime: CronJobParams['cronTime'],\n options?: CronOptions,\n) {\n return (\n target: () => Promise<void>,\n context: ClassMethodDecoratorContext,\n ) => {\n if (context.kind !== 'method') {\n throw new Error(\n `Cron can only be applied to methods, not ${context.kind}`,\n )\n }\n if (context.metadata) {\n const metadata = getCronMetadata(target, context)\n metadata.cronTime = cronTime\n metadata.disabled = options?.disabled ?? false\n }\n return target\n }\n}\n","import type { ClassType } from '@navios/core'\n\nimport { Injectable } from '@navios/core'\n\nimport { getScheduleMetadata } from '../metadata/index.mjs'\n\nexport function Schedulable() {\n return (target: ClassType, context: ClassDecoratorContext) => {\n if (context.kind !== 'class') {\n throw new Error(\n `SchedulableDecorator can only be applied to classes, not ${context.kind}`,\n )\n }\n if (context.metadata) {\n getScheduleMetadata(target, context)\n }\n return Injectable()(target, context)\n }\n}\n","export enum Schedule {\n EveryMinute = '*/1 * * * *',\n EveryFiveMinutes = '*/5 * * * *',\n EveryTenMinutes = '*/10 * * * *',\n EveryFifteenMinutes = '*/15 * * * *',\n EveryThirtyMinutes = '*/30 * * * *',\n EveryHour = '0 * * * *',\n EveryTwoHours = '0 */2 * * *',\n EveryThreeHours = '0 */3 * * *',\n EveryFourHours = '0 */4 * * *',\n EverySixHours = '0 */6 * * *',\n EveryTwelveHours = '0 */12 * * *',\n EveryDay = '0 0 * * *',\n EveryWeek = '0 0 * * 0',\n EveryMonth = '0 0 1 * *',\n}\n","import type { ClassType } from '@navios/core'\n\nimport {\n EnvConfigProvider,\n inject,\n Injectable,\n Logger,\n syncInject,\n} from '@navios/core'\n\nimport { CronJob } from 'cron'\n\nimport type { ScheduleMetadata } from './metadata/index.mjs'\n\nimport {\n extractScheduleMetadata,\n hasScheduleMetadata,\n} from './metadata/index.mjs'\n\n@Injectable()\nexport class SchedulerService {\n // private readonly configService = syncInject(EnvConfigProvider)\n private readonly logger = syncInject(Logger, {\n context: SchedulerService.name,\n })\n private readonly jobs: Map<string, CronJob> = new Map()\n\n register(service: ClassType) {\n if (!hasScheduleMetadata(service)) {\n throw new Error(\n `[Navios-Schedule] Service ${service.name} is not schedulable. Make sure to use @Schedulable decorator.`,\n )\n }\n const metadata = extractScheduleMetadata(service)\n this.logger.debug('Scheduling service', metadata.name)\n this.registerJobs(service, metadata)\n }\n\n getJob<T extends ClassType>(\n service: T,\n method: keyof InstanceType<T>,\n ): CronJob | undefined {\n const metadata = extractScheduleMetadata(service)\n const jobName = `${metadata.name}.${method as string}()`\n return this.jobs.get(jobName)\n }\n\n private registerJobs(service: ClassType, metadata: ScheduleMetadata) {\n const jobs = metadata.jobs\n for (const job of jobs) {\n if (!job.cronTime) {\n this.logger.debug('Skipping job', job.classMethod)\n continue\n }\n const name = `${metadata.name}.${job.classMethod}()`\n const self = this\n const defaultDisabled = false\n const cronJob = CronJob.from({\n cronTime: job.cronTime,\n name,\n async onTick() {\n try {\n self.logger.debug('Executing job', name)\n const instance = await inject(service)\n await instance[job.classMethod]()\n } catch (error) {\n self.logger.error('Error executing job', name, error)\n }\n },\n start: !(defaultDisabled || job.disabled),\n })\n this.jobs.set(name, cronJob)\n }\n }\n\n startAll() {\n for (const job of this.jobs.values()) {\n if (job.isActive) {\n continue\n }\n job.start()\n }\n }\n\n stopAll() {\n for (const job of this.jobs.values()) {\n if (!job.isActive) {\n continue\n }\n job.stop()\n }\n }\n}\n"]}
package/tsup.config.mts DELETED
@@ -1,12 +0,0 @@
1
- import { defineConfig } from 'tsup'
2
-
3
- export default defineConfig({
4
- entry: ['src/index.mts'],
5
- outDir: 'lib',
6
- format: ['esm', 'cjs'],
7
- clean: true,
8
- treeshake: 'smallest',
9
- sourcemap: true,
10
- platform: 'node',
11
- experimentalDts: true,
12
- })