@navios/schedule 0.5.0 → 0.7.1
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/CHANGELOG.md +30 -0
- package/dist/src/cron.constants.d.mts +25 -0
- package/dist/src/cron.constants.d.mts.map +1 -1
- package/dist/src/decorators/cron.decorator.d.mts +48 -0
- package/dist/src/decorators/cron.decorator.d.mts.map +1 -1
- package/dist/src/decorators/schedulable.decorator.d.mts +25 -0
- package/dist/src/decorators/schedulable.decorator.d.mts.map +1 -1
- package/dist/src/scheduler.service.d.mts +101 -0
- package/dist/src/scheduler.service.d.mts.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/lib/index.cjs +626 -0
- package/lib/index.cjs.map +1 -0
- package/lib/index.d.cts +264 -0
- package/lib/index.d.cts.map +1 -0
- package/lib/index.d.mts +264 -14
- package/lib/index.d.mts.map +1 -0
- package/lib/index.mjs +592 -209
- package/lib/index.mjs.map +1 -1
- package/package.json +9 -9
- package/project.json +2 -2
- package/src/cron.constants.mts +25 -0
- package/src/decorators/cron.decorator.mts +48 -0
- package/src/decorators/schedulable.decorator.mts +25 -0
- package/src/scheduler.service.mts +101 -0
- package/tsdown.config.mts +33 -0
- package/lib/_tsup-dts-rollup.d.mts +0 -106
- package/lib/_tsup-dts-rollup.d.ts +0 -106
- package/lib/index.d.ts +0 -14
- package/lib/index.js +0 -240
- package/lib/index.js.map +0 -1
- package/tsup.config.mts +0 -12
package/lib/index.mjs
CHANGED
|
@@ -1,228 +1,611 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { CronJob } from
|
|
1
|
+
import { Container, Injectable, Logger, inject } from "@navios/core";
|
|
2
|
+
import { CronJob } from "cron";
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
7
|
-
var __knownSymbol = (name, symbol) => (symbol = Symbol[name]) ? symbol : Symbol.for("Symbol." + name);
|
|
8
|
-
var __typeError = (msg) => {
|
|
9
|
-
throw TypeError(msg);
|
|
10
|
-
};
|
|
11
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
12
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
13
|
-
var __decoratorStart = (base) => [, , , __create(null)];
|
|
14
|
-
var __decoratorStrings = ["class", "method", "getter", "setter", "accessor", "field", "value", "get", "set"];
|
|
15
|
-
var __expectFn = (fn) => fn !== void 0 && typeof fn !== "function" ? __typeError("Function expected") : fn;
|
|
16
|
-
var __decoratorContext = (kind, name, done, metadata, fns) => ({ kind: __decoratorStrings[kind], name, metadata, addInitializer: (fn) => done._ ? __typeError("Already initialized") : fns.push(__expectFn(fn || null)) });
|
|
17
|
-
var __decoratorMetadata = (array, target) => __defNormalProp(target, __knownSymbol("metadata"), array[3]);
|
|
18
|
-
var __runInitializers = (array, flags, self, value) => {
|
|
19
|
-
for (var i = 0, fns = array[flags >> 1], n = fns && fns.length; i < n; i++) fns[i].call(self) ;
|
|
20
|
-
return value;
|
|
21
|
-
};
|
|
22
|
-
var __decorateElement = (array, flags, name, decorators, target, extra) => {
|
|
23
|
-
var it, done, ctx, k = flags & 7, p = false;
|
|
24
|
-
var j = 0;
|
|
25
|
-
var extraInitializers = array[j] || (array[j] = []);
|
|
26
|
-
var desc = k && ((target = target.prototype), k < 5 && (k > 3 || !p) && __getOwnPropDesc(target , name));
|
|
27
|
-
__name(target, name);
|
|
28
|
-
for (var i = decorators.length - 1; i >= 0; i--) {
|
|
29
|
-
ctx = __decoratorContext(k, name, done = {}, array[3], extraInitializers);
|
|
30
|
-
it = (0, decorators[i])(target, ctx), done._ = 1;
|
|
31
|
-
__expectFn(it) && (target = it);
|
|
32
|
-
}
|
|
33
|
-
return __decoratorMetadata(array, target), desc && __defProp(target, name, desc), p ? k ^ 4 ? extra : desc : target;
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
// src/metadata/cron.metadata.mts
|
|
37
|
-
var CronMetadataKey = Symbol("CronMetadataKey");
|
|
4
|
+
//#region src/metadata/cron.metadata.mts
|
|
5
|
+
const CronMetadataKey = Symbol("CronMetadataKey");
|
|
38
6
|
function getAllCronMetadata(context) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
throw new Error("[Navios-Schedule] Wrong environment.");
|
|
7
|
+
if (context.metadata) {
|
|
8
|
+
const metadata = context.metadata[CronMetadataKey];
|
|
9
|
+
if (metadata) return metadata;
|
|
10
|
+
else {
|
|
11
|
+
context.metadata[CronMetadataKey] = /* @__PURE__ */ new Set();
|
|
12
|
+
return context.metadata[CronMetadataKey];
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
throw new Error("[Navios-Schedule] Wrong environment.");
|
|
49
16
|
}
|
|
50
17
|
function getCronMetadata(target, context) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
throw new Error("[Navios-Schedule] Wrong environment.");
|
|
18
|
+
if (context.metadata) {
|
|
19
|
+
const metadata = getAllCronMetadata(context);
|
|
20
|
+
if (metadata) {
|
|
21
|
+
const endpointMetadata = Array.from(metadata).find((item) => item.classMethod === target.name);
|
|
22
|
+
if (endpointMetadata) return endpointMetadata;
|
|
23
|
+
else {
|
|
24
|
+
const newMetadata = {
|
|
25
|
+
classMethod: target.name,
|
|
26
|
+
cronTime: null,
|
|
27
|
+
disabled: false
|
|
28
|
+
};
|
|
29
|
+
metadata.add(newMetadata);
|
|
30
|
+
return newMetadata;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
throw new Error("[Navios-Schedule] Wrong environment.");
|
|
71
35
|
}
|
|
72
36
|
|
|
73
|
-
|
|
74
|
-
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region src/metadata/schedule.metadata.mts
|
|
39
|
+
const ScheduleMetadataKey = Symbol("ControllerMetadataKey");
|
|
75
40
|
function getScheduleMetadata(target, context) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
throw new Error("[Navios-Schedule] Wrong environment.");
|
|
41
|
+
if (context.metadata) {
|
|
42
|
+
const metadata = context.metadata[ScheduleMetadataKey];
|
|
43
|
+
if (metadata) return metadata;
|
|
44
|
+
else {
|
|
45
|
+
const jobsMetadata = getAllCronMetadata(context);
|
|
46
|
+
const newMetadata = {
|
|
47
|
+
name: target.name,
|
|
48
|
+
jobs: jobsMetadata
|
|
49
|
+
};
|
|
50
|
+
context.metadata[ScheduleMetadataKey] = newMetadata;
|
|
51
|
+
target[ScheduleMetadataKey] = newMetadata;
|
|
52
|
+
return newMetadata;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
throw new Error("[Navios-Schedule] Wrong environment.");
|
|
92
56
|
}
|
|
93
57
|
function extractScheduleMetadata(target) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
"[Navios-Schedule] Controller metadata not found. Make sure to use @Controller decorator."
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
|
-
return metadata;
|
|
58
|
+
const metadata = target[ScheduleMetadataKey];
|
|
59
|
+
if (!metadata) throw new Error("[Navios-Schedule] Controller metadata not found. Make sure to use @Controller decorator.");
|
|
60
|
+
return metadata;
|
|
101
61
|
}
|
|
102
62
|
function hasScheduleMetadata(target) {
|
|
103
|
-
|
|
104
|
-
return !!metadata;
|
|
63
|
+
return !!target[ScheduleMetadataKey];
|
|
105
64
|
}
|
|
106
65
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/decorators/cron.decorator.mts
|
|
68
|
+
/**
|
|
69
|
+
* Decorator that marks a method to run on a cron schedule.
|
|
70
|
+
*
|
|
71
|
+
* The method must be in a class decorated with `@Schedulable()`.
|
|
72
|
+
* The method will be automatically executed according to the provided cron expression.
|
|
73
|
+
*
|
|
74
|
+
* @param cronTime - Cron expression (5 or 6 fields) or a pre-defined Schedule constant
|
|
75
|
+
* @param options - Optional configuration for the cron job
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* @Schedulable()
|
|
80
|
+
* class TaskService {
|
|
81
|
+
* // Run daily at midnight
|
|
82
|
+
* @Cron('0 0 * * *')
|
|
83
|
+
* async dailyTask() {
|
|
84
|
+
* console.log('Running daily task')
|
|
85
|
+
* }
|
|
86
|
+
*
|
|
87
|
+
* // Use pre-defined schedule
|
|
88
|
+
* @Cron(Schedule.EveryFiveMinutes)
|
|
89
|
+
* async frequentTask() {
|
|
90
|
+
* console.log('Running every 5 minutes')
|
|
91
|
+
* }
|
|
92
|
+
*
|
|
93
|
+
* // Disabled job (won't start automatically)
|
|
94
|
+
* @Cron('0 2 * * *', { disabled: true })
|
|
95
|
+
* async maintenanceTask() {
|
|
96
|
+
* console.log('Maintenance task')
|
|
97
|
+
* }
|
|
98
|
+
* }
|
|
99
|
+
* ```
|
|
100
|
+
*
|
|
101
|
+
* @throws {Error} If applied to something other than a method
|
|
102
|
+
*
|
|
103
|
+
* @public
|
|
104
|
+
*/ function Cron(cronTime, options) {
|
|
105
|
+
return (target, context) => {
|
|
106
|
+
if (context.kind !== "method") throw new Error(`Cron can only be applied to methods, not ${context.kind}`);
|
|
107
|
+
if (context.metadata) {
|
|
108
|
+
const metadata = getCronMetadata(target, context);
|
|
109
|
+
metadata.cronTime = cronTime;
|
|
110
|
+
metadata.disabled = options?.disabled ?? false;
|
|
111
|
+
}
|
|
112
|
+
return target;
|
|
113
|
+
};
|
|
122
114
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
115
|
+
|
|
116
|
+
//#endregion
|
|
117
|
+
//#region src/decorators/schedulable.decorator.mts
|
|
118
|
+
/**
|
|
119
|
+
* Decorator that marks a class as schedulable and makes it injectable.
|
|
120
|
+
*
|
|
121
|
+
* Classes decorated with `@Schedulable()` can contain methods decorated with `@Cron()`
|
|
122
|
+
* that will be automatically scheduled and executed. This decorator also applies
|
|
123
|
+
* the `@Injectable()` decorator, making the class available for dependency injection.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```typescript
|
|
127
|
+
* @Schedulable()
|
|
128
|
+
* class TaskService {
|
|
129
|
+
* @Cron('0 0 * * *')
|
|
130
|
+
* async dailyTask() {
|
|
131
|
+
* // This will run daily at midnight
|
|
132
|
+
* }
|
|
133
|
+
* }
|
|
134
|
+
*
|
|
135
|
+
* // Register the service
|
|
136
|
+
* schedulerService.register(TaskService)
|
|
137
|
+
* ```
|
|
138
|
+
*
|
|
139
|
+
* @throws {Error} If applied to something other than a class
|
|
140
|
+
*
|
|
141
|
+
* @public
|
|
142
|
+
*/ function Schedulable() {
|
|
143
|
+
return (target, context) => {
|
|
144
|
+
if (context.kind !== "class") throw new Error(`SchedulableDecorator can only be applied to classes, not ${context.kind}`);
|
|
145
|
+
if (context.metadata) getScheduleMetadata(target, context);
|
|
146
|
+
return Injectable()(target, context);
|
|
147
|
+
};
|
|
135
148
|
}
|
|
136
149
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
150
|
+
//#endregion
|
|
151
|
+
//#region src/cron.constants.mts
|
|
152
|
+
/**
|
|
153
|
+
* Pre-defined cron schedule constants for common scheduling patterns.
|
|
154
|
+
*
|
|
155
|
+
* These constants provide convenient shortcuts for frequently used cron expressions,
|
|
156
|
+
* making it easier to schedule jobs without manually writing cron expressions.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```typescript
|
|
160
|
+
* import { Schedule } from '@navios/schedule'
|
|
161
|
+
*
|
|
162
|
+
* @Schedulable()
|
|
163
|
+
* class TaskService {
|
|
164
|
+
* @Cron(Schedule.EveryMinute)
|
|
165
|
+
* async everyMinute() {}
|
|
166
|
+
*
|
|
167
|
+
* @Cron(Schedule.EveryHour)
|
|
168
|
+
* async hourly() {}
|
|
169
|
+
*
|
|
170
|
+
* @Cron(Schedule.EveryDay)
|
|
171
|
+
* async daily() {}
|
|
172
|
+
* }
|
|
173
|
+
* ```
|
|
174
|
+
*
|
|
175
|
+
* @public
|
|
176
|
+
*/ var Schedule = /* @__PURE__ */ function(Schedule$1) {
|
|
177
|
+
Schedule$1["EveryMinute"] = "*/1 * * * *";
|
|
178
|
+
Schedule$1["EveryFiveMinutes"] = "*/5 * * * *";
|
|
179
|
+
Schedule$1["EveryTenMinutes"] = "*/10 * * * *";
|
|
180
|
+
Schedule$1["EveryFifteenMinutes"] = "*/15 * * * *";
|
|
181
|
+
Schedule$1["EveryThirtyMinutes"] = "*/30 * * * *";
|
|
182
|
+
Schedule$1["EveryHour"] = "0 * * * *";
|
|
183
|
+
Schedule$1["EveryTwoHours"] = "0 */2 * * *";
|
|
184
|
+
Schedule$1["EveryThreeHours"] = "0 */3 * * *";
|
|
185
|
+
Schedule$1["EveryFourHours"] = "0 */4 * * *";
|
|
186
|
+
Schedule$1["EverySixHours"] = "0 */6 * * *";
|
|
187
|
+
Schedule$1["EveryTwelveHours"] = "0 */12 * * *";
|
|
188
|
+
Schedule$1["EveryDay"] = "0 0 * * *";
|
|
189
|
+
Schedule$1["EveryWeek"] = "0 0 * * 0";
|
|
190
|
+
Schedule$1["EveryMonth"] = "0 0 1 * *";
|
|
191
|
+
return Schedule$1;
|
|
192
|
+
}({});
|
|
193
|
+
|
|
194
|
+
//#endregion
|
|
195
|
+
//#region src/scheduler.service.mts
|
|
196
|
+
function applyDecs2203RFactory() {
|
|
197
|
+
function createAddInitializerMethod(initializers, decoratorFinishedRef) {
|
|
198
|
+
return function addInitializer(initializer) {
|
|
199
|
+
assertNotFinished(decoratorFinishedRef, "addInitializer");
|
|
200
|
+
assertCallable(initializer, "An initializer");
|
|
201
|
+
initializers.push(initializer);
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
function memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, metadata, value) {
|
|
205
|
+
var kindStr;
|
|
206
|
+
switch (kind) {
|
|
207
|
+
case 1:
|
|
208
|
+
kindStr = "accessor";
|
|
209
|
+
break;
|
|
210
|
+
case 2:
|
|
211
|
+
kindStr = "method";
|
|
212
|
+
break;
|
|
213
|
+
case 3:
|
|
214
|
+
kindStr = "getter";
|
|
215
|
+
break;
|
|
216
|
+
case 4:
|
|
217
|
+
kindStr = "setter";
|
|
218
|
+
break;
|
|
219
|
+
default: kindStr = "field";
|
|
220
|
+
}
|
|
221
|
+
var ctx = {
|
|
222
|
+
kind: kindStr,
|
|
223
|
+
name: isPrivate ? "#" + name : name,
|
|
224
|
+
static: isStatic,
|
|
225
|
+
private: isPrivate,
|
|
226
|
+
metadata
|
|
227
|
+
};
|
|
228
|
+
var decoratorFinishedRef = { v: false };
|
|
229
|
+
ctx.addInitializer = createAddInitializerMethod(initializers, decoratorFinishedRef);
|
|
230
|
+
var get, set;
|
|
231
|
+
if (kind === 0) if (isPrivate) {
|
|
232
|
+
get = desc.get;
|
|
233
|
+
set = desc.set;
|
|
234
|
+
} else {
|
|
235
|
+
get = function() {
|
|
236
|
+
return this[name];
|
|
237
|
+
};
|
|
238
|
+
set = function(v) {
|
|
239
|
+
this[name] = v;
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
else if (kind === 2) get = function() {
|
|
243
|
+
return desc.value;
|
|
244
|
+
};
|
|
245
|
+
else {
|
|
246
|
+
if (kind === 1 || kind === 3) get = function() {
|
|
247
|
+
return desc.get.call(this);
|
|
248
|
+
};
|
|
249
|
+
if (kind === 1 || kind === 4) set = function(v) {
|
|
250
|
+
desc.set.call(this, v);
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
ctx.access = get && set ? {
|
|
254
|
+
get,
|
|
255
|
+
set
|
|
256
|
+
} : get ? { get } : { set };
|
|
257
|
+
try {
|
|
258
|
+
return dec(value, ctx);
|
|
259
|
+
} finally {
|
|
260
|
+
decoratorFinishedRef.v = true;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
function assertNotFinished(decoratorFinishedRef, fnName) {
|
|
264
|
+
if (decoratorFinishedRef.v) throw new Error("attempted to call " + fnName + " after decoration was finished");
|
|
265
|
+
}
|
|
266
|
+
function assertCallable(fn, hint) {
|
|
267
|
+
if (typeof fn !== "function") throw new TypeError(hint + " must be a function");
|
|
268
|
+
}
|
|
269
|
+
function assertValidReturnValue(kind, value) {
|
|
270
|
+
var type = typeof value;
|
|
271
|
+
if (kind === 1) {
|
|
272
|
+
if (type !== "object" || value === null) throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0");
|
|
273
|
+
if (value.get !== void 0) assertCallable(value.get, "accessor.get");
|
|
274
|
+
if (value.set !== void 0) assertCallable(value.set, "accessor.set");
|
|
275
|
+
if (value.init !== void 0) assertCallable(value.init, "accessor.init");
|
|
276
|
+
} else if (type !== "function") {
|
|
277
|
+
var hint;
|
|
278
|
+
if (kind === 0) hint = "field";
|
|
279
|
+
else if (kind === 10) hint = "class";
|
|
280
|
+
else hint = "method";
|
|
281
|
+
throw new TypeError(hint + " decorators must return a function or void 0");
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
function applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers, metadata) {
|
|
285
|
+
var decs = decInfo[0];
|
|
286
|
+
var desc, init, value;
|
|
287
|
+
if (isPrivate) if (kind === 0 || kind === 1) desc = {
|
|
288
|
+
get: decInfo[3],
|
|
289
|
+
set: decInfo[4]
|
|
290
|
+
};
|
|
291
|
+
else if (kind === 3) desc = { get: decInfo[3] };
|
|
292
|
+
else if (kind === 4) desc = { set: decInfo[3] };
|
|
293
|
+
else desc = { value: decInfo[3] };
|
|
294
|
+
else if (kind !== 0) desc = Object.getOwnPropertyDescriptor(base, name);
|
|
295
|
+
if (kind === 1) value = {
|
|
296
|
+
get: desc.get,
|
|
297
|
+
set: desc.set
|
|
298
|
+
};
|
|
299
|
+
else if (kind === 2) value = desc.value;
|
|
300
|
+
else if (kind === 3) value = desc.get;
|
|
301
|
+
else if (kind === 4) value = desc.set;
|
|
302
|
+
var newValue, get, set;
|
|
303
|
+
if (typeof decs === "function") {
|
|
304
|
+
newValue = memberDec(decs, name, desc, initializers, kind, isStatic, isPrivate, metadata, value);
|
|
305
|
+
if (newValue !== void 0) {
|
|
306
|
+
assertValidReturnValue(kind, newValue);
|
|
307
|
+
if (kind === 0) init = newValue;
|
|
308
|
+
else if (kind === 1) {
|
|
309
|
+
init = newValue.init;
|
|
310
|
+
get = newValue.get || value.get;
|
|
311
|
+
set = newValue.set || value.set;
|
|
312
|
+
value = {
|
|
313
|
+
get,
|
|
314
|
+
set
|
|
315
|
+
};
|
|
316
|
+
} else value = newValue;
|
|
317
|
+
}
|
|
318
|
+
} else for (var i = decs.length - 1; i >= 0; i--) {
|
|
319
|
+
var dec = decs[i];
|
|
320
|
+
newValue = memberDec(dec, name, desc, initializers, kind, isStatic, isPrivate, metadata, value);
|
|
321
|
+
if (newValue !== void 0) {
|
|
322
|
+
assertValidReturnValue(kind, newValue);
|
|
323
|
+
var newInit;
|
|
324
|
+
if (kind === 0) newInit = newValue;
|
|
325
|
+
else if (kind === 1) {
|
|
326
|
+
newInit = newValue.init;
|
|
327
|
+
get = newValue.get || value.get;
|
|
328
|
+
set = newValue.set || value.set;
|
|
329
|
+
value = {
|
|
330
|
+
get,
|
|
331
|
+
set
|
|
332
|
+
};
|
|
333
|
+
} else value = newValue;
|
|
334
|
+
if (newInit !== void 0) if (init === void 0) init = newInit;
|
|
335
|
+
else if (typeof init === "function") init = [init, newInit];
|
|
336
|
+
else init.push(newInit);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
if (kind === 0 || kind === 1) {
|
|
340
|
+
if (init === void 0) init = function(instance, init$1) {
|
|
341
|
+
return init$1;
|
|
342
|
+
};
|
|
343
|
+
else if (typeof init !== "function") {
|
|
344
|
+
var ownInitializers = init;
|
|
345
|
+
init = function(instance, init$1) {
|
|
346
|
+
var value$1 = init$1;
|
|
347
|
+
for (var i$1 = 0; i$1 < ownInitializers.length; i$1++) value$1 = ownInitializers[i$1].call(instance, value$1);
|
|
348
|
+
return value$1;
|
|
349
|
+
};
|
|
350
|
+
} else {
|
|
351
|
+
var originalInitializer = init;
|
|
352
|
+
init = function(instance, init$1) {
|
|
353
|
+
return originalInitializer.call(instance, init$1);
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
ret.push(init);
|
|
357
|
+
}
|
|
358
|
+
if (kind !== 0) {
|
|
359
|
+
if (kind === 1) {
|
|
360
|
+
desc.get = value.get;
|
|
361
|
+
desc.set = value.set;
|
|
362
|
+
} else if (kind === 2) desc.value = value;
|
|
363
|
+
else if (kind === 3) desc.get = value;
|
|
364
|
+
else if (kind === 4) desc.set = value;
|
|
365
|
+
if (isPrivate) if (kind === 1) {
|
|
366
|
+
ret.push(function(instance, args) {
|
|
367
|
+
return value.get.call(instance, args);
|
|
368
|
+
});
|
|
369
|
+
ret.push(function(instance, args) {
|
|
370
|
+
return value.set.call(instance, args);
|
|
371
|
+
});
|
|
372
|
+
} else if (kind === 2) ret.push(value);
|
|
373
|
+
else ret.push(function(instance, args) {
|
|
374
|
+
return value.call(instance, args);
|
|
375
|
+
});
|
|
376
|
+
else Object.defineProperty(base, name, desc);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
function applyMemberDecs(Class, decInfos, metadata) {
|
|
380
|
+
var ret = [];
|
|
381
|
+
var protoInitializers;
|
|
382
|
+
var staticInitializers;
|
|
383
|
+
var existingProtoNonFields = /* @__PURE__ */ new Map();
|
|
384
|
+
var existingStaticNonFields = /* @__PURE__ */ new Map();
|
|
385
|
+
for (var i = 0; i < decInfos.length; i++) {
|
|
386
|
+
var decInfo = decInfos[i];
|
|
387
|
+
if (!Array.isArray(decInfo)) continue;
|
|
388
|
+
var kind = decInfo[1];
|
|
389
|
+
var name = decInfo[2];
|
|
390
|
+
var isPrivate = decInfo.length > 3;
|
|
391
|
+
var isStatic = kind >= 5;
|
|
392
|
+
var base;
|
|
393
|
+
var initializers;
|
|
394
|
+
if (isStatic) {
|
|
395
|
+
base = Class;
|
|
396
|
+
kind = kind - 5;
|
|
397
|
+
staticInitializers = staticInitializers || [];
|
|
398
|
+
initializers = staticInitializers;
|
|
399
|
+
} else {
|
|
400
|
+
base = Class.prototype;
|
|
401
|
+
protoInitializers = protoInitializers || [];
|
|
402
|
+
initializers = protoInitializers;
|
|
403
|
+
}
|
|
404
|
+
if (kind !== 0 && !isPrivate) {
|
|
405
|
+
var existingNonFields = isStatic ? existingStaticNonFields : existingProtoNonFields;
|
|
406
|
+
var existingKind = existingNonFields.get(name) || 0;
|
|
407
|
+
if (existingKind === true || existingKind === 3 && kind !== 4 || existingKind === 4 && kind !== 3) throw new Error("Attempted to decorate a public method/accessor that has the same name as a previously decorated public method/accessor. This is not currently supported by the decorators plugin. Property name was: " + name);
|
|
408
|
+
else if (!existingKind && kind > 2) existingNonFields.set(name, kind);
|
|
409
|
+
else existingNonFields.set(name, true);
|
|
410
|
+
}
|
|
411
|
+
applyMemberDec(ret, base, decInfo, name, kind, isStatic, isPrivate, initializers, metadata);
|
|
412
|
+
}
|
|
413
|
+
pushInitializers(ret, protoInitializers);
|
|
414
|
+
pushInitializers(ret, staticInitializers);
|
|
415
|
+
return ret;
|
|
416
|
+
}
|
|
417
|
+
function pushInitializers(ret, initializers) {
|
|
418
|
+
if (initializers) ret.push(function(instance) {
|
|
419
|
+
for (var i = 0; i < initializers.length; i++) initializers[i].call(instance);
|
|
420
|
+
return instance;
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
function applyClassDecs(targetClass, classDecs, metadata) {
|
|
424
|
+
if (classDecs.length > 0) {
|
|
425
|
+
var initializers = [];
|
|
426
|
+
var newClass = targetClass;
|
|
427
|
+
var name = targetClass.name;
|
|
428
|
+
for (var i = classDecs.length - 1; i >= 0; i--) {
|
|
429
|
+
var decoratorFinishedRef = { v: false };
|
|
430
|
+
try {
|
|
431
|
+
var nextNewClass = classDecs[i](newClass, {
|
|
432
|
+
kind: "class",
|
|
433
|
+
name,
|
|
434
|
+
addInitializer: createAddInitializerMethod(initializers, decoratorFinishedRef),
|
|
435
|
+
metadata
|
|
436
|
+
});
|
|
437
|
+
} finally {
|
|
438
|
+
decoratorFinishedRef.v = true;
|
|
439
|
+
}
|
|
440
|
+
if (nextNewClass !== void 0) {
|
|
441
|
+
assertValidReturnValue(10, nextNewClass);
|
|
442
|
+
newClass = nextNewClass;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
return [defineMetadata(newClass, metadata), function() {
|
|
446
|
+
for (var i$1 = 0; i$1 < initializers.length; i$1++) initializers[i$1].call(newClass);
|
|
447
|
+
}];
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
function defineMetadata(Class, metadata) {
|
|
451
|
+
return Object.defineProperty(Class, Symbol.metadata || Symbol.for("Symbol.metadata"), {
|
|
452
|
+
configurable: true,
|
|
453
|
+
enumerable: true,
|
|
454
|
+
value: metadata
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
return function applyDecs2203R(targetClass, memberDecs, classDecs, parentClass) {
|
|
458
|
+
if (parentClass !== void 0) var parentMetadata = parentClass[Symbol.metadata || Symbol.for("Symbol.metadata")];
|
|
459
|
+
var metadata = Object.create(parentMetadata === void 0 ? null : parentMetadata);
|
|
460
|
+
var e = applyMemberDecs(targetClass, memberDecs, metadata);
|
|
461
|
+
if (!classDecs.length) defineMetadata(targetClass, metadata);
|
|
462
|
+
return {
|
|
463
|
+
e,
|
|
464
|
+
get c() {
|
|
465
|
+
return applyClassDecs(targetClass, classDecs, metadata);
|
|
466
|
+
}
|
|
467
|
+
};
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
function _apply_decs_2203_r(targetClass, memberDecs, classDecs, parentClass) {
|
|
471
|
+
return (_apply_decs_2203_r = applyDecs2203RFactory())(targetClass, memberDecs, classDecs, parentClass);
|
|
472
|
+
}
|
|
473
|
+
var _dec, _initClass;
|
|
474
|
+
let _SchedulerService;
|
|
475
|
+
_dec = Injectable();
|
|
476
|
+
var SchedulerService = class {
|
|
477
|
+
static {
|
|
478
|
+
({c: [_SchedulerService, _initClass]} = _apply_decs_2203_r(this, [], [_dec]));
|
|
479
|
+
}
|
|
480
|
+
logger = inject(Logger, { context: _SchedulerService.name });
|
|
481
|
+
container = inject(Container);
|
|
482
|
+
jobs = /* @__PURE__ */ new Map();
|
|
483
|
+
/**
|
|
484
|
+
* Registers a schedulable service and starts all its cron jobs.
|
|
485
|
+
*
|
|
486
|
+
* The service must be decorated with `@Schedulable()` and contain methods
|
|
487
|
+
* decorated with `@Cron()` to be registered successfully.
|
|
488
|
+
*
|
|
489
|
+
* @param service - The schedulable service class to register
|
|
490
|
+
* @throws {Error} If the service is not decorated with `@Schedulable()`
|
|
491
|
+
*
|
|
492
|
+
* @example
|
|
493
|
+
* ```typescript
|
|
494
|
+
* @Schedulable()
|
|
495
|
+
* class TaskService {
|
|
496
|
+
* @Cron('0 0 * * *')
|
|
497
|
+
* async dailyTask() {
|
|
498
|
+
* // Runs daily at midnight
|
|
499
|
+
* }
|
|
500
|
+
* }
|
|
501
|
+
*
|
|
502
|
+
* schedulerService.register(TaskService)
|
|
503
|
+
* ```
|
|
504
|
+
*
|
|
505
|
+
* @public
|
|
506
|
+
*/ register(service) {
|
|
507
|
+
if (!hasScheduleMetadata(service)) throw new Error(`[Navios-Schedule] Service ${service.name} is not schedulable. Make sure to use @Schedulable decorator.`);
|
|
508
|
+
const metadata = extractScheduleMetadata(service);
|
|
509
|
+
this.logger.debug("Scheduling service", metadata.name);
|
|
510
|
+
this.registerJobs(service, metadata);
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Retrieves a specific cron job instance for a method in a schedulable service.
|
|
514
|
+
*
|
|
515
|
+
* @param service - The schedulable service class
|
|
516
|
+
* @param method - The name of the method decorated with `@Cron()`
|
|
517
|
+
* @returns The CronJob instance if found, undefined otherwise
|
|
518
|
+
*
|
|
519
|
+
* @example
|
|
520
|
+
* ```typescript
|
|
521
|
+
* const job = schedulerService.getJob(TaskService, 'dailyTask')
|
|
522
|
+
* if (job) {
|
|
523
|
+
* console.log('Job is active:', job.isActive)
|
|
524
|
+
* job.start() // Manually start the job
|
|
525
|
+
* job.stop() // Manually stop the job
|
|
526
|
+
* }
|
|
527
|
+
* ```
|
|
528
|
+
*
|
|
529
|
+
* @public
|
|
530
|
+
*/ getJob(service, method) {
|
|
531
|
+
const jobName = `${extractScheduleMetadata(service).name}.${method}()`;
|
|
532
|
+
return this.jobs.get(jobName);
|
|
533
|
+
}
|
|
534
|
+
registerJobs(service, metadata) {
|
|
535
|
+
const jobs = metadata.jobs;
|
|
536
|
+
for (const job of jobs) {
|
|
537
|
+
if (!job.cronTime) {
|
|
538
|
+
this.logger.debug("Skipping job", job.classMethod);
|
|
539
|
+
continue;
|
|
540
|
+
}
|
|
541
|
+
const name = `${metadata.name}.${job.classMethod}()`;
|
|
542
|
+
const self = this;
|
|
543
|
+
const cronJob = CronJob.from({
|
|
544
|
+
cronTime: job.cronTime,
|
|
545
|
+
name,
|
|
546
|
+
async onTick() {
|
|
547
|
+
try {
|
|
548
|
+
self.logger.debug("Executing job", name);
|
|
549
|
+
await (await self.container.get(service))[job.classMethod]();
|
|
550
|
+
} catch (error) {
|
|
551
|
+
self.logger.error("Error executing job", name, error);
|
|
552
|
+
}
|
|
553
|
+
},
|
|
554
|
+
start: !job.disabled
|
|
555
|
+
});
|
|
556
|
+
this.jobs.set(name, cronJob);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* Starts all registered cron jobs that are currently inactive.
|
|
561
|
+
*
|
|
562
|
+
* Only jobs that are not already active will be started. This method
|
|
563
|
+
* is useful for resuming all jobs after calling `stopAll()`.
|
|
564
|
+
*
|
|
565
|
+
* @example
|
|
566
|
+
* ```typescript
|
|
567
|
+
* // Stop all jobs
|
|
568
|
+
* schedulerService.stopAll()
|
|
569
|
+
*
|
|
570
|
+
* // Later, resume all jobs
|
|
571
|
+
* schedulerService.startAll()
|
|
572
|
+
* ```
|
|
573
|
+
*
|
|
574
|
+
* @public
|
|
575
|
+
*/ startAll() {
|
|
576
|
+
for (const job of this.jobs.values()) {
|
|
577
|
+
if (job.isActive) continue;
|
|
578
|
+
job.start();
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
/**
|
|
582
|
+
* Stops all registered cron jobs that are currently active.
|
|
583
|
+
*
|
|
584
|
+
* Only jobs that are currently active will be stopped. This method
|
|
585
|
+
* is useful for pausing all scheduled tasks, for example during
|
|
586
|
+
* application shutdown or maintenance.
|
|
587
|
+
*
|
|
588
|
+
* @example
|
|
589
|
+
* ```typescript
|
|
590
|
+
* // Pause all scheduled jobs
|
|
591
|
+
* schedulerService.stopAll()
|
|
592
|
+
*
|
|
593
|
+
* // Jobs can be resumed later with startAll()
|
|
594
|
+
* schedulerService.startAll()
|
|
595
|
+
* ```
|
|
596
|
+
*
|
|
597
|
+
* @public
|
|
598
|
+
*/ stopAll() {
|
|
599
|
+
for (const job of this.jobs.values()) {
|
|
600
|
+
if (!job.isActive) continue;
|
|
601
|
+
job.stop();
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
static {
|
|
605
|
+
_initClass();
|
|
606
|
+
}
|
|
220
607
|
};
|
|
221
|
-
_init = __decoratorStart();
|
|
222
|
-
_SchedulerService = __decorateElement(_init, 0, "SchedulerService", _SchedulerService_decorators, _SchedulerService);
|
|
223
|
-
__runInitializers(_init, 1, _SchedulerService);
|
|
224
|
-
var SchedulerService = _SchedulerService;
|
|
225
608
|
|
|
226
|
-
|
|
227
|
-
|
|
609
|
+
//#endregion
|
|
610
|
+
export { Cron, CronMetadataKey, Schedulable, Schedule, ScheduleMetadataKey, _SchedulerService as SchedulerService, extractScheduleMetadata, getAllCronMetadata, getCronMetadata, getScheduleMetadata, hasScheduleMetadata };
|
|
228
611
|
//# sourceMappingURL=index.mjs.map
|