@velajs/vela 0.9.0 → 1.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/CHANGELOG.md +33 -0
- package/dist/application.d.ts +12 -0
- package/dist/application.js +22 -0
- package/dist/constants.d.ts +29 -26
- package/dist/constants.js +26 -29
- package/dist/http/middleware-consumer.d.ts +11 -10
- package/dist/http/middleware-consumer.js +10 -11
- package/dist/http/route.manager.js +1 -6
- package/dist/index.d.ts +4 -4
- package/dist/index.js +2 -2
- package/dist/openapi/decorators.d.ts +15 -1
- package/dist/openapi/decorators.js +24 -0
- package/dist/openapi/document.js +99 -18
- package/dist/openapi/index.d.ts +3 -2
- package/dist/openapi/index.js +2 -1
- package/dist/openapi/scalar-ui.d.ts +1 -0
- package/dist/openapi/scalar-ui.js +18 -0
- package/dist/openapi/types.d.ts +18 -0
- package/dist/pipeline/component.manager.d.ts +1 -0
- package/dist/pipeline/component.manager.js +10 -34
- package/dist/registry/metadata.registry.d.ts +2 -2
- package/dist/registry/metadata.registry.js +13 -6
- package/dist/schedule/cron-matcher.d.ts +2 -0
- package/dist/schedule/cron-matcher.js +62 -0
- package/dist/schedule/index.d.ts +4 -3
- package/dist/schedule/index.js +2 -2
- package/dist/schedule/schedule.module.d.ts +2 -6
- package/dist/schedule/schedule.module.js +4 -42
- package/dist/schedule/schedule.tokens.d.ts +0 -3
- package/dist/schedule/schedule.tokens.js +0 -2
- package/dist/schedule/schedule.types.d.ts +0 -3
- package/dist/schedule-node/index.d.ts +2 -0
- package/dist/schedule-node/index.js +2 -0
- package/dist/schedule-node/schedule-node.module.d.ts +4 -0
- package/dist/schedule-node/schedule-node.module.js +18 -0
- package/dist/{schedule → schedule-node}/schedule.executor.d.ts +2 -5
- package/dist/schedule-node/schedule.executor.js +91 -0
- package/dist/services/logger.d.ts +9 -8
- package/dist/services/logger.js +14 -15
- package/package.json +5 -1
- package/dist/schedule/schedule.executor.js +0 -169
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
function _ts_decorate(decorators, target, key, desc) {
|
|
2
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
-
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;
|
|
5
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
-
}
|
|
7
|
-
function _ts_metadata(k, v) {
|
|
8
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
-
}
|
|
10
|
-
import { Injectable } from "../container/index.js";
|
|
11
|
-
import { ScheduleRegistry } from "./schedule.registry.js";
|
|
12
|
-
export class ScheduleExecutor {
|
|
13
|
-
registry;
|
|
14
|
-
intervalTimers = [];
|
|
15
|
-
cronTimers = [];
|
|
16
|
-
cronMatcherCache = new Map();
|
|
17
|
-
lastCronMinute = new Map();
|
|
18
|
-
running = true;
|
|
19
|
-
constructor(registry){
|
|
20
|
-
this.registry = registry;
|
|
21
|
-
}
|
|
22
|
-
onApplicationBootstrap() {
|
|
23
|
-
const intervalJobs = this.registry.getIntervalJobs();
|
|
24
|
-
const cronJobs = this.registry.getCronJobs();
|
|
25
|
-
for (const job of intervalJobs){
|
|
26
|
-
this.scheduleInterval(job.instance, job.methodName, job.ms);
|
|
27
|
-
}
|
|
28
|
-
for(let i = 0; i < cronJobs.length; i++){
|
|
29
|
-
const job = cronJobs[i];
|
|
30
|
-
this.scheduleCron(job.instance, job.methodName, job.expression, i);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
scheduleInterval(instance, methodName, ms) {
|
|
34
|
-
if (!this.running) return;
|
|
35
|
-
const timer = setInterval(()=>{
|
|
36
|
-
void this.invoke(instance, methodName);
|
|
37
|
-
}, ms);
|
|
38
|
-
this.intervalTimers.push(timer);
|
|
39
|
-
}
|
|
40
|
-
scheduleCron(instance, methodName, expression, index) {
|
|
41
|
-
if (!this.running) return;
|
|
42
|
-
const matcher = this.getCronMatcher(expression);
|
|
43
|
-
if (!matcher) {
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
const jobKey = `${index}:${methodName}:${expression}`;
|
|
47
|
-
const timer = setInterval(()=>{
|
|
48
|
-
const now = new Date();
|
|
49
|
-
const minuteKey = Math.floor(now.getTime() / 60_000);
|
|
50
|
-
if (this.lastCronMinute.get(jobKey) === minuteKey) {
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
if (!matcher(now)) {
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
this.lastCronMinute.set(jobKey, minuteKey);
|
|
57
|
-
void this.invoke(instance, methodName);
|
|
58
|
-
}, 1000);
|
|
59
|
-
this.cronTimers.push(timer);
|
|
60
|
-
}
|
|
61
|
-
async invoke(instance, methodName) {
|
|
62
|
-
try {
|
|
63
|
-
const method = instance[methodName];
|
|
64
|
-
if (typeof method === 'function') {
|
|
65
|
-
await method.call(instance);
|
|
66
|
-
}
|
|
67
|
-
} catch {
|
|
68
|
-
// Swallow errors to keep the scheduler alive
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
getCronMatcher(expression) {
|
|
72
|
-
if (this.cronMatcherCache.has(expression)) {
|
|
73
|
-
return this.cronMatcherCache.get(expression) ?? null;
|
|
74
|
-
}
|
|
75
|
-
const fields = expression.trim().split(/\s+/);
|
|
76
|
-
if (fields.length !== 5) {
|
|
77
|
-
this.cronMatcherCache.set(expression, null);
|
|
78
|
-
return null;
|
|
79
|
-
}
|
|
80
|
-
const [minuteField, hourField, dayField, monthField, weekdayField] = fields;
|
|
81
|
-
const minuteMatcher = this.parseField(minuteField, 0, 59);
|
|
82
|
-
const hourMatcher = this.parseField(hourField, 0, 23);
|
|
83
|
-
const dayMatcher = this.parseField(dayField, 1, 31);
|
|
84
|
-
const monthMatcher = this.parseField(monthField, 1, 12);
|
|
85
|
-
const weekdayMatcher = this.parseField(weekdayField, 0, 7);
|
|
86
|
-
if (!minuteMatcher || !hourMatcher || !dayMatcher || !monthMatcher || !weekdayMatcher) {
|
|
87
|
-
this.cronMatcherCache.set(expression, null);
|
|
88
|
-
return null;
|
|
89
|
-
}
|
|
90
|
-
const matcher = (date)=>{
|
|
91
|
-
const weekday = date.getDay();
|
|
92
|
-
return minuteMatcher(date.getMinutes()) && hourMatcher(date.getHours()) && dayMatcher(date.getDate()) && monthMatcher(date.getMonth() + 1) && weekdayMatcher(weekday);
|
|
93
|
-
};
|
|
94
|
-
this.cronMatcherCache.set(expression, matcher);
|
|
95
|
-
return matcher;
|
|
96
|
-
}
|
|
97
|
-
parseField(field, min, max) {
|
|
98
|
-
const segments = field.split(',');
|
|
99
|
-
const predicates = [];
|
|
100
|
-
for (const rawSegment of segments){
|
|
101
|
-
const segment = rawSegment.trim();
|
|
102
|
-
if (!segment) return null;
|
|
103
|
-
const stepParts = segment.split('/');
|
|
104
|
-
if (stepParts.length > 2) return null;
|
|
105
|
-
const baseSegment = stepParts[0];
|
|
106
|
-
const step = stepParts.length === 2 ? Number(stepParts[1]) : 1;
|
|
107
|
-
if (!Number.isInteger(step) || step <= 0) return null;
|
|
108
|
-
const range = this.parseRange(baseSegment, min, max);
|
|
109
|
-
if (!range) return null;
|
|
110
|
-
predicates.push((value)=>{
|
|
111
|
-
if (value < range.start || value > range.end) return false;
|
|
112
|
-
return (value - range.start) % step === 0;
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
return (value)=>predicates.some((predicate)=>predicate(value));
|
|
116
|
-
}
|
|
117
|
-
parseRange(segment, min, max) {
|
|
118
|
-
if (segment === '*') {
|
|
119
|
-
return {
|
|
120
|
-
start: min,
|
|
121
|
-
end: max
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
const bounds = segment.split('-');
|
|
125
|
-
if (bounds.length === 1) {
|
|
126
|
-
const value = this.parseCronNumber(bounds[0], min, max);
|
|
127
|
-
if (value === null) return null;
|
|
128
|
-
return {
|
|
129
|
-
start: value,
|
|
130
|
-
end: value
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
if (bounds.length !== 2) return null;
|
|
134
|
-
const start = this.parseCronNumber(bounds[0], min, max);
|
|
135
|
-
const end = this.parseCronNumber(bounds[1], min, max);
|
|
136
|
-
if (start === null || end === null || start > end) return null;
|
|
137
|
-
return {
|
|
138
|
-
start,
|
|
139
|
-
end
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
parseCronNumber(raw, min, max) {
|
|
143
|
-
const value = Number(raw);
|
|
144
|
-
if (!Number.isInteger(value)) return null;
|
|
145
|
-
// Cron allows both 0 and 7 for Sunday.
|
|
146
|
-
const normalized = max === 7 && value === 7 ? 0 : value;
|
|
147
|
-
if (normalized < min || normalized > max) return null;
|
|
148
|
-
return normalized;
|
|
149
|
-
}
|
|
150
|
-
onModuleDestroy() {
|
|
151
|
-
this.running = false;
|
|
152
|
-
for (const timer of this.intervalTimers){
|
|
153
|
-
clearInterval(timer);
|
|
154
|
-
}
|
|
155
|
-
for (const timer of this.cronTimers){
|
|
156
|
-
clearInterval(timer);
|
|
157
|
-
}
|
|
158
|
-
this.intervalTimers = [];
|
|
159
|
-
this.cronTimers = [];
|
|
160
|
-
this.lastCronMinute.clear();
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
ScheduleExecutor = _ts_decorate([
|
|
164
|
-
Injectable(),
|
|
165
|
-
_ts_metadata("design:type", Function),
|
|
166
|
-
_ts_metadata("design:paramtypes", [
|
|
167
|
-
typeof ScheduleRegistry === "undefined" ? Object : ScheduleRegistry
|
|
168
|
-
])
|
|
169
|
-
], ScheduleExecutor);
|