@aomex/cron 3.30.7 → 3.30.8-alpha.61a729b.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/dist/index.d.ts +112 -116
- package/dist/index.js +686 -774
- package/dist/index.js.map +1 -1
- package/package.json +11 -11
package/dist/index.js
CHANGED
|
@@ -1,803 +1,715 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
import
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
var TELL_PARENT_INIT = "CRON_PARENT_INIT";
|
|
23
|
-
var TELL_CHILD_RESOLVE = "CRON_CHILD_RESOLVE";
|
|
24
|
-
var TELL_CHILD_REJECT = "CRON_CHILD_REJECT";
|
|
25
|
-
var TELL_PARENT_ENDED = "TELL_PARENT_ENDED";
|
|
26
|
-
|
|
1
|
+
import { I18n, compose, createI18n, middleware, sleep } from "@aomex/common";
|
|
2
|
+
import { Commander, ConsoleMiddleware, scriptName, terminal } from "@aomex/console";
|
|
3
|
+
import { getFileValues, pathToFiles } from "@aomex/internal-file-import";
|
|
4
|
+
import { Caching } from "@aomex/cache";
|
|
5
|
+
import { CacheMemoryAdapter } from "@aomex/cache/memory";
|
|
6
|
+
import * as cronParser from "cron-parser";
|
|
7
|
+
import { bytes, sleep as sleep$1 } from "@aomex/internal-tools";
|
|
8
|
+
import { spawn } from "node:child_process";
|
|
9
|
+
import { randomBytes } from "node:crypto";
|
|
10
|
+
import net from "node:net";
|
|
11
|
+
import formatDuration from "format-duration";
|
|
12
|
+
import pidusage from "pidusage";
|
|
13
|
+
const ENV_CRON = "AOMEX_CRON";
|
|
14
|
+
const ENV_CRON_SCHEDULE_TIME = "AOMEX_CRON_SCHEDULE_TIME";
|
|
15
|
+
const ENV_CRON_EXECUTION_TIME = "AOMEX_CRON_EXECUTION_TIME";
|
|
16
|
+
const ENV_CRON_NEXT_SCHEDULE_TIME = "AOMEX_CRON_NEXT_SCHEDULE_TIME";
|
|
17
|
+
const TELL_CHILD_STOP = "CRON_CHILD_STOP";
|
|
18
|
+
const TELL_PARENT_INIT = "CRON_PARENT_INIT";
|
|
19
|
+
const TELL_CHILD_RESOLVE = "CRON_CHILD_RESOLVE";
|
|
20
|
+
const TELL_CHILD_REJECT = "CRON_CHILD_REJECT";
|
|
21
|
+
const TELL_PARENT_ENDED = "TELL_PARENT_ENDED";
|
|
27
22
|
// src/middleware/cron.middleware.ts
|
|
28
23
|
var CronMiddleware = class extends ConsoleMiddleware {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
24
|
+
options;
|
|
25
|
+
constructor(options) {
|
|
26
|
+
super(async (ctx, next) => {
|
|
27
|
+
if (process.env["AOMEX_CRON"] === "1") {
|
|
28
|
+
let alive = true;
|
|
29
|
+
const { promise: init, resolve } = Promise.withResolvers();
|
|
30
|
+
const onInitOrStop = (message) => {
|
|
31
|
+
switch (message) {
|
|
32
|
+
case TELL_CHILD_RESOLVE:
|
|
33
|
+
alive = true;
|
|
34
|
+
resolve(void 0);
|
|
35
|
+
break;
|
|
36
|
+
case TELL_CHILD_REJECT:
|
|
37
|
+
alive = false;
|
|
38
|
+
resolve(void 0);
|
|
39
|
+
break;
|
|
40
|
+
case TELL_CHILD_STOP: alive = false;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
process.on("message", onInitOrStop);
|
|
44
|
+
process.send(TELL_PARENT_INIT);
|
|
45
|
+
const nextTime = new Date(process.env[ENV_CRON_NEXT_SCHEDULE_TIME]);
|
|
46
|
+
ctx.cron = {
|
|
47
|
+
executionTime: new Date(process.env[ENV_CRON_EXECUTION_TIME]),
|
|
48
|
+
scheduleTime: new Date(process.env[ENV_CRON_SCHEDULE_TIME]),
|
|
49
|
+
nextScheduleTime: nextTime,
|
|
50
|
+
isAlive: () => alive,
|
|
51
|
+
isCurrentEpoch: () => nextTime.getTime() - Date.now() > 0
|
|
52
|
+
};
|
|
53
|
+
try {
|
|
54
|
+
await init;
|
|
55
|
+
await next();
|
|
56
|
+
} finally {
|
|
57
|
+
process.removeListener("message", onInitOrStop);
|
|
58
|
+
process.send(TELL_PARENT_ENDED);
|
|
59
|
+
}
|
|
60
|
+
} else {
|
|
61
|
+
const now = /* @__PURE__ */ new Date();
|
|
62
|
+
ctx.cron = {
|
|
63
|
+
executionTime: now,
|
|
64
|
+
scheduleTime: now,
|
|
65
|
+
nextScheduleTime: now,
|
|
66
|
+
isAlive: () => true,
|
|
67
|
+
isCurrentEpoch: () => true
|
|
68
|
+
};
|
|
69
|
+
return next();
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
this.options = options;
|
|
73
|
+
}
|
|
80
74
|
};
|
|
81
|
-
|
|
82
|
-
|
|
75
|
+
/**
|
|
76
|
+
* 收集定时任务
|
|
77
|
+
*
|
|
78
|
+
* 如果传递字符串,则为cron时间表达式(支持秒)。如:`1-10,20 * * * *`
|
|
79
|
+
*/
|
|
80
|
+
const cron = (options) => {
|
|
81
|
+
return new CronMiddleware(typeof options === "string" ? { time: options } : options);
|
|
83
82
|
};
|
|
84
|
-
|
|
85
|
-
// src/lib/cron.ts
|
|
86
|
-
import { scriptName } from "@aomex/console";
|
|
87
|
-
import { CacheMemoryAdapter, Caching } from "@aomex/cache";
|
|
88
|
-
import * as cronParser from "cron-parser";
|
|
89
|
-
|
|
90
83
|
// src/lib/task.ts
|
|
91
|
-
import { sleep } from "@aomex/internal-tools";
|
|
92
|
-
import { spawn } from "child_process";
|
|
93
|
-
import { randomBytes } from "crypto";
|
|
94
84
|
var Task = class {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
const remoteToken = await cache.get(slot);
|
|
245
|
-
if (remoteToken === token) {
|
|
246
|
-
await cache.delete(slot);
|
|
247
|
-
}
|
|
248
|
-
};
|
|
249
|
-
}
|
|
85
|
+
cron;
|
|
86
|
+
currentTimestamp;
|
|
87
|
+
nextTimestamp;
|
|
88
|
+
child = null;
|
|
89
|
+
concurrentKey;
|
|
90
|
+
filePath;
|
|
91
|
+
execArgv;
|
|
92
|
+
/**
|
|
93
|
+
* 并发锁定时间。非正常退出时要等20分钟才释放,正常情况下任务执行完就会立即释放。
|
|
94
|
+
* 之所以要设置这么长,是为了防止服务器卡顿时,容易造成任务双跑问题
|
|
95
|
+
*/
|
|
96
|
+
concurrentExpireDuration = 12e5;
|
|
97
|
+
constructor(cron, currentTimestamp, nextTimestamp) {
|
|
98
|
+
this.cron = cron;
|
|
99
|
+
this.currentTimestamp = currentTimestamp;
|
|
100
|
+
this.nextTimestamp = nextTimestamp;
|
|
101
|
+
this.concurrentKey = `aomex-cron|v:${cron.argv}|t:${cron.time}|s:1|c:${cron.concurrent}`;
|
|
102
|
+
this.filePath = process.argv[1];
|
|
103
|
+
this.execArgv = [...process.execArgv];
|
|
104
|
+
if (!this.execArgv.includes("--enable-source-maps")) this.execArgv.unshift("--enable-source-maps");
|
|
105
|
+
}
|
|
106
|
+
get pid() {
|
|
107
|
+
return this.child?.pid?.toString();
|
|
108
|
+
}
|
|
109
|
+
async consume() {
|
|
110
|
+
const result = await this.win();
|
|
111
|
+
if (result === "lose") return;
|
|
112
|
+
const pong = this.ping(result.slot, result.token);
|
|
113
|
+
try {
|
|
114
|
+
await this.runChildProcess();
|
|
115
|
+
} finally {
|
|
116
|
+
await pong();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
async runChildProcess() {
|
|
120
|
+
if (this.child) return;
|
|
121
|
+
const { argv } = this.cron;
|
|
122
|
+
const { promise, resolve } = Promise.withResolvers();
|
|
123
|
+
const childProcess = spawn(process.argv0, [
|
|
124
|
+
...this.execArgv,
|
|
125
|
+
this.filePath,
|
|
126
|
+
...argv
|
|
127
|
+
], {
|
|
128
|
+
cwd: process.cwd(),
|
|
129
|
+
env: {
|
|
130
|
+
/**
|
|
131
|
+
* 支持颜色输出。默认值是 `0` 代表不输出颜色。
|
|
132
|
+
* @link https://nodejs.org/api/tty.html#writestreamgetcolordepthenv
|
|
133
|
+
*/
|
|
134
|
+
FORCE_COLOR: "3",
|
|
135
|
+
...process.env,
|
|
136
|
+
[ENV_CRON]: "1",
|
|
137
|
+
[ENV_CRON_SCHEDULE_TIME]: new Date(this.currentTimestamp).toISOString(),
|
|
138
|
+
[ENV_CRON_EXECUTION_TIME]: (/* @__PURE__ */ new Date()).toISOString(),
|
|
139
|
+
[ENV_CRON_NEXT_SCHEDULE_TIME]: new Date(this.nextTimestamp).toISOString()
|
|
140
|
+
},
|
|
141
|
+
stdio: [
|
|
142
|
+
"inherit",
|
|
143
|
+
"inherit",
|
|
144
|
+
"inherit",
|
|
145
|
+
"ipc"
|
|
146
|
+
]
|
|
147
|
+
});
|
|
148
|
+
this.child = childProcess;
|
|
149
|
+
childProcess.on("close", resolve);
|
|
150
|
+
childProcess.on("exit", resolve);
|
|
151
|
+
childProcess.on("error", resolve);
|
|
152
|
+
let timer = null;
|
|
153
|
+
childProcess.on("message", (message) => {
|
|
154
|
+
if (message === "CRON_PARENT_INIT") childProcess.send(this.cron.stopping ? TELL_CHILD_REJECT : TELL_CHILD_RESOLVE);
|
|
155
|
+
if (message === "TELL_PARENT_ENDED") timer = setTimeout(() => {
|
|
156
|
+
try {
|
|
157
|
+
childProcess.kill();
|
|
158
|
+
} finally {
|
|
159
|
+
resolve(void 0);
|
|
160
|
+
timer = null;
|
|
161
|
+
}
|
|
162
|
+
}, 3e4);
|
|
163
|
+
});
|
|
164
|
+
await promise.finally(() => {
|
|
165
|
+
this.child = null;
|
|
166
|
+
if (timer) clearTimeout(timer);
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
getWaitingTimeout() {
|
|
170
|
+
return this.cron.getWaitingTimeout(this.nextTimestamp - this.currentTimestamp);
|
|
171
|
+
}
|
|
172
|
+
async win() {
|
|
173
|
+
const { cache, globalRunningLevel, globalWinnerLevel } = this.cron;
|
|
174
|
+
const waitingTimeout = this.getWaitingTimeout();
|
|
175
|
+
const startTime = Date.now();
|
|
176
|
+
const token = `${process.pid}:${Date.now()}:${randomBytes(16).toString("hex")}`;
|
|
177
|
+
const maxConcurrent = Math.min(this.cron.concurrent, 65535);
|
|
178
|
+
const waitingLevel = Math.max(0, globalRunningLevel - globalWinnerLevel);
|
|
179
|
+
await sleep$1(globalWinnerLevel * 20);
|
|
180
|
+
await sleep$1(waitingLevel * 5);
|
|
181
|
+
while (true) {
|
|
182
|
+
for (let i = 0; i < maxConcurrent; ++i) {
|
|
183
|
+
if (this.cron.stopping) return "lose";
|
|
184
|
+
const slot = `${this.concurrentKey}|slot:${i}`;
|
|
185
|
+
if (await cache.setNX(slot, token, this.concurrentExpireDuration)) return {
|
|
186
|
+
slot,
|
|
187
|
+
token
|
|
188
|
+
};
|
|
189
|
+
await sleep$1(5);
|
|
190
|
+
}
|
|
191
|
+
if (waitingTimeout <= 0) return "lose";
|
|
192
|
+
if (startTime + waitingTimeout <= Date.now()) return "lose";
|
|
193
|
+
await sleep$1(500);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
ping(slot, token) {
|
|
197
|
+
const cache = this.cron.cache;
|
|
198
|
+
const CHECK_INTERVAL = 15e3;
|
|
199
|
+
let isStopped = false;
|
|
200
|
+
const checkTask = async () => {
|
|
201
|
+
let shouldContinue = true;
|
|
202
|
+
if (isStopped) return;
|
|
203
|
+
try {
|
|
204
|
+
const remoteToken = await cache.get(slot);
|
|
205
|
+
if (remoteToken === token) {
|
|
206
|
+
await cache.expire(slot, this.concurrentExpireDuration);
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
if (isStopped) return;
|
|
210
|
+
if (remoteToken === null) {
|
|
211
|
+
if (await cache.setNX(slot, token, this.concurrentExpireDuration)) return;
|
|
212
|
+
}
|
|
213
|
+
try {
|
|
214
|
+
this.child?.send(TELL_CHILD_STOP);
|
|
215
|
+
} catch {}
|
|
216
|
+
await sleep$1(5e3);
|
|
217
|
+
this.child?.kill();
|
|
218
|
+
shouldContinue = false;
|
|
219
|
+
} finally {
|
|
220
|
+
if (shouldContinue && !isStopped) timer = setTimeout(checkTask, CHECK_INTERVAL);
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
let timer = setTimeout(checkTask, CHECK_INTERVAL);
|
|
224
|
+
const mustAfterWaitingTimeout = new Promise((resolve) => {
|
|
225
|
+
sleep$1(this.getWaitingTimeout() + 5e3).then(resolve);
|
|
226
|
+
});
|
|
227
|
+
return async () => {
|
|
228
|
+
isStopped = true;
|
|
229
|
+
clearTimeout(timer);
|
|
230
|
+
await mustAfterWaitingTimeout;
|
|
231
|
+
if (await cache.get(slot) === token) await cache.delete(slot);
|
|
232
|
+
};
|
|
233
|
+
}
|
|
250
234
|
};
|
|
251
|
-
|
|
252
|
-
// src/i18n/index.ts
|
|
253
|
-
import { I18n as I18n3 } from "@aomex/common";
|
|
254
|
-
|
|
255
235
|
// src/i18n/locales/zh-cn.ts
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
236
|
+
const zh = I18n.define({
|
|
237
|
+
eject: "导出定时任务列表",
|
|
238
|
+
start: "启动定时任务",
|
|
239
|
+
stop: "停止定时任务",
|
|
240
|
+
stats: "查看定时任务执行状态",
|
|
241
|
+
not_started: "定时任务未启动,端口:{{port}}",
|
|
242
|
+
invalid_cron_time: "无效的 Cron 表达式:{{time}}",
|
|
243
|
+
invalid_port: "无效的服务端口:{{port}}(有效范围:1-65535)"
|
|
264
244
|
});
|
|
265
|
-
|
|
266
245
|
// src/i18n/locales/en-us.ts
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
246
|
+
const en = I18n.satisfies(zh).define({
|
|
247
|
+
eject: "Eject cron tasks to crontab",
|
|
248
|
+
start: "Start cron tasks",
|
|
249
|
+
stop: "Stop scheduled tasks",
|
|
250
|
+
stats: "Show cron tasks stats",
|
|
251
|
+
not_started: "cron not started with port: {{port}}",
|
|
252
|
+
invalid_cron_time: "Invalid cron expression: {{time}}",
|
|
253
|
+
invalid_port: "Invalid server port: {{port}} (valid range: 1-65535)"
|
|
275
254
|
});
|
|
276
|
-
|
|
277
|
-
// src/i18n/locales/index.ts
|
|
278
|
-
var locales = {
|
|
279
|
-
zh_CN: zh,
|
|
280
|
-
en_US: en
|
|
281
|
-
};
|
|
282
|
-
|
|
283
255
|
// src/i18n/index.ts
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
256
|
+
const i18n = createI18n({
|
|
257
|
+
zh,
|
|
258
|
+
en,
|
|
259
|
+
load: (file) => import(new URL(`./locales/${file}.js`, import.meta.url).href)
|
|
287
260
|
});
|
|
288
|
-
|
|
289
261
|
// src/lib/cron.ts
|
|
290
262
|
var Cron = class {
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
263
|
+
options;
|
|
264
|
+
cronExpression;
|
|
265
|
+
cronExpressionForNext;
|
|
266
|
+
stopping = false;
|
|
267
|
+
handle = null;
|
|
268
|
+
runningLevel = 0;
|
|
269
|
+
time;
|
|
270
|
+
_time;
|
|
271
|
+
_seconds;
|
|
272
|
+
_argv;
|
|
273
|
+
_cache;
|
|
274
|
+
_tasks = /* @__PURE__ */ new Set();
|
|
275
|
+
_globalCrontab;
|
|
276
|
+
constructor(options) {
|
|
277
|
+
this.options = options;
|
|
278
|
+
this._globalCrontab = options.crontab;
|
|
279
|
+
const scheduleTime = "time" in options ? options.time : [
|
|
280
|
+
options.second ?? "0",
|
|
281
|
+
options.minute ?? "*",
|
|
282
|
+
options.hour ?? "*",
|
|
283
|
+
options.dayOfMonth ?? "*",
|
|
284
|
+
options.month ?? "*",
|
|
285
|
+
options.dayOfWeek ?? "*"
|
|
286
|
+
].join(" ");
|
|
287
|
+
if ("time" in options) {
|
|
288
|
+
const fieldCount = scheduleTime.trim().split(/\s+/).filter(Boolean).length;
|
|
289
|
+
if (fieldCount < 5 || fieldCount > 6) throw new Error(i18n.t("invalid_cron_time", { time: scheduleTime }));
|
|
290
|
+
}
|
|
291
|
+
try {
|
|
292
|
+
this.cronExpression = cronParser.CronExpressionParser.parse(scheduleTime);
|
|
293
|
+
this.cronExpressionForNext = cronParser.CronExpressionParser.parse(scheduleTime);
|
|
294
|
+
} catch {
|
|
295
|
+
throw new Error(i18n.t("invalid_cron_time", { time: scheduleTime }));
|
|
296
|
+
}
|
|
297
|
+
this.time = this.cronExpression.stringify(true);
|
|
298
|
+
this.cronExpressionForNext.next();
|
|
299
|
+
}
|
|
300
|
+
get command() {
|
|
301
|
+
return this.options.command;
|
|
302
|
+
}
|
|
303
|
+
get globalWinnerLevel() {
|
|
304
|
+
let level = 0;
|
|
305
|
+
for (const cron of this._globalCrontab) level += cron.getPIDs().length;
|
|
306
|
+
return level;
|
|
307
|
+
}
|
|
308
|
+
get globalRunningLevel() {
|
|
309
|
+
let level = 0;
|
|
310
|
+
for (const cron of this._globalCrontab) level += cron.runningLevel;
|
|
311
|
+
return level;
|
|
312
|
+
}
|
|
313
|
+
get concurrent() {
|
|
314
|
+
let { concurrent = 1 } = this.options;
|
|
315
|
+
if (concurrent === "infinity") concurrent = Infinity;
|
|
316
|
+
return Math.max(1, concurrent);
|
|
317
|
+
}
|
|
318
|
+
getWaitingTimeout(timeDelta = Infinity) {
|
|
319
|
+
return Math.max(0, Math.min(this.options.waitingTimeout ?? 1e4, Math.abs(timeDelta)));
|
|
320
|
+
}
|
|
321
|
+
get cache() {
|
|
322
|
+
return this._cache ??= new Caching(this.options.store ?? new CacheMemoryAdapter());
|
|
323
|
+
}
|
|
324
|
+
get argv() {
|
|
325
|
+
return this._argv ??= [this.options.command, ...this.options.args || []].map(String);
|
|
326
|
+
}
|
|
327
|
+
getPIDs() {
|
|
328
|
+
return [...this._tasks].map((task) => task.pid).filter((pid) => pid !== void 0);
|
|
329
|
+
}
|
|
330
|
+
async start() {
|
|
331
|
+
if (!this.cronExpression.hasNext()) return;
|
|
332
|
+
const currentTime = this.cronExpression.next().getTime();
|
|
333
|
+
const nextTime = this.cronExpressionForNext.next().getTime();
|
|
334
|
+
while (true) {
|
|
335
|
+
/**
|
|
336
|
+
* setTimeout的最大延时值是 `2^31-1`(约为24.85天) ,超过这个值会被立即执行。
|
|
337
|
+
* 拆分延时值可让执行时机更精确。
|
|
338
|
+
*/
|
|
339
|
+
const delta = Math.min(9e5, currentTime - Date.now());
|
|
340
|
+
await this.delay(delta);
|
|
341
|
+
if (this.stopping) return;
|
|
342
|
+
if (currentTime - Date.now() <= 0) {
|
|
343
|
+
++this.runningLevel;
|
|
344
|
+
const task = new Task(this, currentTime, nextTime);
|
|
345
|
+
this._tasks.add(task);
|
|
346
|
+
task.consume().finally(() => {
|
|
347
|
+
this._tasks.delete(task);
|
|
348
|
+
--this.runningLevel;
|
|
349
|
+
});
|
|
350
|
+
break;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
return this.start();
|
|
354
|
+
}
|
|
355
|
+
delay(delta) {
|
|
356
|
+
const { promise, resolve } = Promise.withResolvers();
|
|
357
|
+
const timer = setTimeout(() => {
|
|
358
|
+
this.handle = null;
|
|
359
|
+
resolve(void 0);
|
|
360
|
+
}, delta);
|
|
361
|
+
this.handle = {
|
|
362
|
+
timer,
|
|
363
|
+
resolve
|
|
364
|
+
};
|
|
365
|
+
return promise;
|
|
366
|
+
}
|
|
367
|
+
stop() {
|
|
368
|
+
this.stopping = true;
|
|
369
|
+
if (this.handle) {
|
|
370
|
+
clearTimeout(this.handle.timer);
|
|
371
|
+
this.handle.resolve();
|
|
372
|
+
}
|
|
373
|
+
setTimeout(() => {
|
|
374
|
+
this._tasks.forEach((task) => {
|
|
375
|
+
task.child?.send(TELL_CHILD_STOP);
|
|
376
|
+
});
|
|
377
|
+
}, 1e3);
|
|
378
|
+
}
|
|
379
|
+
toString() {
|
|
380
|
+
return [
|
|
381
|
+
this.time,
|
|
382
|
+
scriptName,
|
|
383
|
+
...this.argv.map((value) => value.includes(" ") ? `"${value}"` : value)
|
|
384
|
+
].join(" ");
|
|
385
|
+
}
|
|
386
|
+
toJSON() {
|
|
387
|
+
return {
|
|
388
|
+
command: this.command,
|
|
389
|
+
time: this.time,
|
|
390
|
+
argv: this.argv,
|
|
391
|
+
concurrent: this.concurrent,
|
|
392
|
+
waitingTimeout: this.getWaitingTimeout()
|
|
393
|
+
};
|
|
394
|
+
}
|
|
420
395
|
};
|
|
421
|
-
|
|
422
396
|
// src/lib/collect-crontab.ts
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
crontab.push(
|
|
435
|
-
new Cron({
|
|
436
|
-
...opts,
|
|
437
|
-
...middleware6["options"],
|
|
438
|
-
command: builder.commands[0],
|
|
439
|
-
crontab
|
|
440
|
-
})
|
|
441
|
-
);
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
return crontab;
|
|
397
|
+
const collectCrontab = async (opts) => {
|
|
398
|
+
const files = await pathToFiles(opts.commanders);
|
|
399
|
+
const commanders = await getFileValues(files, (item) => !!item && item instanceof Commander);
|
|
400
|
+
const crontab = [];
|
|
401
|
+
for (const commander of commanders) for (const builder of commander["builders"]) for (const middleware of builder["middlewareList"]) if (middleware instanceof CronMiddleware) crontab.push(new Cron({
|
|
402
|
+
...opts,
|
|
403
|
+
...middleware["options"],
|
|
404
|
+
command: builder.commands[0],
|
|
405
|
+
crontab
|
|
406
|
+
}));
|
|
407
|
+
return crontab;
|
|
447
408
|
};
|
|
448
|
-
|
|
449
409
|
// src/middleware/start.middleware.ts
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
socket.on("error", (err) => {
|
|
519
|
-
console.log(`[cron:start] ${err instanceof Error ? err.message : String(err)}`);
|
|
520
|
-
});
|
|
521
|
-
});
|
|
522
|
-
const PORT = opts.port || DEFAULT_PORT;
|
|
523
|
-
server.listen(PORT);
|
|
524
|
-
process.once("SIGINT", async () => {
|
|
525
|
-
stopTasks();
|
|
526
|
-
});
|
|
527
|
-
await Promise.all(crontab.map((cron2) => cron2.start()));
|
|
528
|
-
await stopTasks();
|
|
529
|
-
await sleep2(DELAY + 10);
|
|
530
|
-
server.close();
|
|
531
|
-
},
|
|
532
|
-
help: {
|
|
533
|
-
onDocument(doc) {
|
|
534
|
-
doc[commandName] = { summary: i18n.t("start") };
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
});
|
|
410
|
+
const commandName$3 = "cron:start";
|
|
411
|
+
const start = (opts) => {
|
|
412
|
+
return middleware.console({
|
|
413
|
+
fn: async (ctx, next) => {
|
|
414
|
+
if (ctx.input.command !== commandName$3) return next();
|
|
415
|
+
ctx.commandMatched = true;
|
|
416
|
+
if (!Number.isInteger(opts.port) || opts.port < 1 || opts.port > 65535) throw new Error(i18n.t("invalid_port", { port: opts.port }));
|
|
417
|
+
const crontab = await collectCrontab(opts);
|
|
418
|
+
const DELAY = 300;
|
|
419
|
+
if (!crontab.length) return;
|
|
420
|
+
const stopTasks = async (callback) => {
|
|
421
|
+
crontab.forEach((cron) => cron.stop());
|
|
422
|
+
let remain = crontab.length;
|
|
423
|
+
await Promise.all(crontab.map((cron) => {
|
|
424
|
+
const { promise, resolve } = Promise.withResolvers();
|
|
425
|
+
const timer = setInterval(() => {
|
|
426
|
+
if (!cron.runningLevel) {
|
|
427
|
+
clearInterval(timer);
|
|
428
|
+
resolve(void 0);
|
|
429
|
+
callback?.(--remain);
|
|
430
|
+
}
|
|
431
|
+
}, DELAY);
|
|
432
|
+
return promise;
|
|
433
|
+
}));
|
|
434
|
+
};
|
|
435
|
+
const collectRunners = () => {
|
|
436
|
+
return crontab.reduce((carry, cron) => carry.concat(cron.getPIDs().map((pid) => ({
|
|
437
|
+
pid,
|
|
438
|
+
command: cron.argv.join(" "),
|
|
439
|
+
schedule: cron.time
|
|
440
|
+
}))), []);
|
|
441
|
+
};
|
|
442
|
+
const sendStopMessages = async (socket) => {
|
|
443
|
+
socket.write(JSON.stringify({ runners: collectRunners() }) + "\n");
|
|
444
|
+
await stopTasks(() => {
|
|
445
|
+
socket.write(JSON.stringify({ runningPIDs: crontab.flatMap((cron) => cron.getPIDs()) }) + "\n");
|
|
446
|
+
});
|
|
447
|
+
socket.destroy();
|
|
448
|
+
};
|
|
449
|
+
const sendStatsMessage = (socket) => {
|
|
450
|
+
socket.write(JSON.stringify({ runners: collectRunners() }) + "\n");
|
|
451
|
+
};
|
|
452
|
+
const server = net.createServer(async (socket) => {
|
|
453
|
+
socket.on("data", async (data) => {
|
|
454
|
+
switch (data.toString()) {
|
|
455
|
+
case "cron:stop":
|
|
456
|
+
await sendStopMessages(socket);
|
|
457
|
+
break;
|
|
458
|
+
case "cron:stats": sendStatsMessage(socket);
|
|
459
|
+
}
|
|
460
|
+
});
|
|
461
|
+
socket.on("error", (err) => {
|
|
462
|
+
console.log(`[cron:start] ${err instanceof Error ? err.message : String(err)}`);
|
|
463
|
+
});
|
|
464
|
+
});
|
|
465
|
+
server.listen(opts.port);
|
|
466
|
+
process.once("SIGINT", async () => {
|
|
467
|
+
stopTasks();
|
|
468
|
+
});
|
|
469
|
+
await Promise.all(crontab.map((cron) => cron.start()));
|
|
470
|
+
await stopTasks();
|
|
471
|
+
await sleep(310);
|
|
472
|
+
server.close();
|
|
473
|
+
},
|
|
474
|
+
help: { onDocument(doc) {
|
|
475
|
+
doc[commandName$3] = { summary: i18n.t("start") };
|
|
476
|
+
} }
|
|
477
|
+
});
|
|
538
478
|
};
|
|
539
|
-
|
|
540
479
|
// src/middleware/eject.middleware.ts
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
summary: i18n.t("eject")
|
|
555
|
-
};
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
});
|
|
480
|
+
const commandName$2 = "cron:eject";
|
|
481
|
+
const eject = (opts) => {
|
|
482
|
+
return middleware.console({
|
|
483
|
+
fn: async (ctx, next) => {
|
|
484
|
+
if (ctx.input.command !== commandName$2) return next();
|
|
485
|
+
ctx.commandMatched = true;
|
|
486
|
+
const crontab = await collectCrontab(opts);
|
|
487
|
+
console.log(crontab.map((cron) => cron.toString()).join("\n"));
|
|
488
|
+
},
|
|
489
|
+
help: { onDocument(doc) {
|
|
490
|
+
doc[commandName$2] = { summary: i18n.t("eject") };
|
|
491
|
+
} }
|
|
492
|
+
});
|
|
559
493
|
};
|
|
560
|
-
|
|
561
494
|
// src/middleware/stop.middleware.ts
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
item.status = "success";
|
|
675
|
-
}
|
|
676
|
-
});
|
|
677
|
-
}
|
|
678
|
-
}
|
|
679
|
-
});
|
|
680
|
-
await promise;
|
|
681
|
-
await sleep3(300);
|
|
682
|
-
},
|
|
683
|
-
help: {
|
|
684
|
-
onDocument(doc) {
|
|
685
|
-
doc[commandName3] = { summary: i18n.t("stop") };
|
|
686
|
-
}
|
|
687
|
-
}
|
|
688
|
-
});
|
|
495
|
+
const commandName$1 = "cron:stop";
|
|
496
|
+
const stop = (opts) => {
|
|
497
|
+
return middleware.console({
|
|
498
|
+
fn: async (ctx, next) => {
|
|
499
|
+
if (ctx.input.command !== commandName$1) return next();
|
|
500
|
+
ctx.commandMatched = true;
|
|
501
|
+
const { port } = opts;
|
|
502
|
+
const client = net.createConnection({ port }, () => {
|
|
503
|
+
client.write(commandName$1);
|
|
504
|
+
});
|
|
505
|
+
const logSession = terminal.createSession();
|
|
506
|
+
let initialized = false;
|
|
507
|
+
let runners = [];
|
|
508
|
+
let timer;
|
|
509
|
+
(function loop() {
|
|
510
|
+
timer = setTimeout(async () => {
|
|
511
|
+
let stats;
|
|
512
|
+
try {
|
|
513
|
+
const pids = runners.filter(({ status }) => status === "loading").map(({ pid }) => pid);
|
|
514
|
+
stats = pids.length ? await pidusage(pids) : {};
|
|
515
|
+
} catch {
|
|
516
|
+
stats = {};
|
|
517
|
+
}
|
|
518
|
+
runners = runners.map((runner) => {
|
|
519
|
+
if (runner.status === "success") return runner;
|
|
520
|
+
const stat = stats[runner.pid];
|
|
521
|
+
if (!stat) return runner;
|
|
522
|
+
return {
|
|
523
|
+
...runner,
|
|
524
|
+
cpu: stat.cpu,
|
|
525
|
+
memory: stat.memory,
|
|
526
|
+
elapsed: stat.elapsed
|
|
527
|
+
};
|
|
528
|
+
}).sort((a, b) => b.elapsed - a.elapsed);
|
|
529
|
+
if (runners.length) logSession.update(terminal.generateTable([[
|
|
530
|
+
"",
|
|
531
|
+
"PID",
|
|
532
|
+
"COMMAND",
|
|
533
|
+
"SCHEDULE",
|
|
534
|
+
"CPU",
|
|
535
|
+
"MEMORY",
|
|
536
|
+
"TIME"
|
|
537
|
+
], ...runners.map(({ pid, command, schedule, elapsed, cpu, memory, status }) => {
|
|
538
|
+
return [
|
|
539
|
+
`%${status}%`,
|
|
540
|
+
pid,
|
|
541
|
+
command.padEnd(24, " "),
|
|
542
|
+
schedule,
|
|
543
|
+
`${cpu.toFixed(2)}%`,
|
|
544
|
+
bytes(memory),
|
|
545
|
+
formatDuration(elapsed, { leading: true })
|
|
546
|
+
];
|
|
547
|
+
})], {
|
|
548
|
+
columnDefault: { paddingRight: 4 },
|
|
549
|
+
columns: {
|
|
550
|
+
0: {
|
|
551
|
+
paddingLeft: 0,
|
|
552
|
+
paddingRight: 0
|
|
553
|
+
},
|
|
554
|
+
2: {
|
|
555
|
+
paddingRight: 1,
|
|
556
|
+
width: 24,
|
|
557
|
+
wrapWord: true
|
|
558
|
+
}
|
|
559
|
+
},
|
|
560
|
+
drawVerticalLine: () => false,
|
|
561
|
+
drawHorizontalLine: () => false
|
|
562
|
+
}).replace(/^\s+/, " "));
|
|
563
|
+
if (initialized && runners.every((item) => item.status === "success")) {
|
|
564
|
+
logSession.commit();
|
|
565
|
+
client.destroy();
|
|
566
|
+
} else loop();
|
|
567
|
+
}, 100);
|
|
568
|
+
})();
|
|
569
|
+
process.once("SIGINT", () => {
|
|
570
|
+
clearTimeout(timer);
|
|
571
|
+
client.destroy();
|
|
572
|
+
});
|
|
573
|
+
const { resolve, reject, promise } = Promise.withResolvers();
|
|
574
|
+
client.on("close", resolve);
|
|
575
|
+
client.on("error", (err) => {
|
|
576
|
+
if (err.code === "ECONNREFUSED") {
|
|
577
|
+
terminal.printWarning(i18n.t("not_started", { port }));
|
|
578
|
+
resolve(void 0);
|
|
579
|
+
} else reject(/* @__PURE__ */ new Error(`[cron:stop] ${err instanceof Error ? err.message : String(err)}`));
|
|
580
|
+
});
|
|
581
|
+
client.on("data", async (data) => {
|
|
582
|
+
for (const stringifyData of data.toString().split("\n").filter(Boolean)) {
|
|
583
|
+
const jsonData = JSON.parse(stringifyData);
|
|
584
|
+
if ("runners" in jsonData) {
|
|
585
|
+
initialized = true;
|
|
586
|
+
runners = jsonData.runners.map((runner) => {
|
|
587
|
+
return {
|
|
588
|
+
...runner,
|
|
589
|
+
cpu: 0,
|
|
590
|
+
memory: 0,
|
|
591
|
+
elapsed: 0,
|
|
592
|
+
status: "loading"
|
|
593
|
+
};
|
|
594
|
+
});
|
|
595
|
+
} else if ("runningPIDs" in jsonData) runners.forEach((item) => {
|
|
596
|
+
if (!jsonData.runningPIDs.includes(item.pid)) item.status = "success";
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
});
|
|
600
|
+
await promise;
|
|
601
|
+
await sleep$1(300);
|
|
602
|
+
},
|
|
603
|
+
help: { onDocument(doc) {
|
|
604
|
+
doc[commandName$1] = { summary: i18n.t("stop") };
|
|
605
|
+
} }
|
|
606
|
+
});
|
|
689
607
|
};
|
|
690
|
-
|
|
691
608
|
// src/middleware/stats.middleware.ts
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
help: {
|
|
775
|
-
onDocument(doc) {
|
|
776
|
-
doc[commandName4] = { summary: i18n.t("stats") };
|
|
777
|
-
}
|
|
778
|
-
}
|
|
779
|
-
});
|
|
609
|
+
const commandName = "cron:stats";
|
|
610
|
+
const stats = (opts) => {
|
|
611
|
+
return middleware.console({
|
|
612
|
+
fn: async (ctx, next) => {
|
|
613
|
+
if (ctx.input.command !== commandName) return next();
|
|
614
|
+
ctx.commandMatched = true;
|
|
615
|
+
const { port } = opts;
|
|
616
|
+
const client = net.createConnection({ port }, () => {
|
|
617
|
+
client.write(commandName);
|
|
618
|
+
const timer = setInterval(() => {
|
|
619
|
+
client.write(commandName);
|
|
620
|
+
}, 600);
|
|
621
|
+
client.on("error", () => {
|
|
622
|
+
clearInterval(timer);
|
|
623
|
+
});
|
|
624
|
+
client.on("close", () => {
|
|
625
|
+
clearInterval(timer);
|
|
626
|
+
});
|
|
627
|
+
});
|
|
628
|
+
const logSession = terminal.createSession();
|
|
629
|
+
const { resolve, reject, promise } = Promise.withResolvers();
|
|
630
|
+
client.on("close", resolve);
|
|
631
|
+
client.on("error", (err) => {
|
|
632
|
+
if (err.code === "ECONNREFUSED") {
|
|
633
|
+
terminal.printWarning(i18n.t("not_started", { port }));
|
|
634
|
+
resolve(void 0);
|
|
635
|
+
} else reject(err);
|
|
636
|
+
});
|
|
637
|
+
client.on("data", async (data) => {
|
|
638
|
+
for (const stringifyData of data.toString().split("\n").filter(Boolean)) {
|
|
639
|
+
const jsonData = JSON.parse(stringifyData);
|
|
640
|
+
if (!("runners" in jsonData)) return;
|
|
641
|
+
let stats;
|
|
642
|
+
try {
|
|
643
|
+
stats = await pidusage(jsonData.runners.map((item) => item.pid));
|
|
644
|
+
} catch {
|
|
645
|
+
stats = {};
|
|
646
|
+
}
|
|
647
|
+
logSession.update(terminal.generateTable([[
|
|
648
|
+
"PID",
|
|
649
|
+
"COMMAND",
|
|
650
|
+
"SCHEDULE",
|
|
651
|
+
"CPU",
|
|
652
|
+
"MEMORY",
|
|
653
|
+
"TIME"
|
|
654
|
+
], ...jsonData.runners.map((runner) => {
|
|
655
|
+
const stat = stats[runner.pid] || {
|
|
656
|
+
cpu: 0,
|
|
657
|
+
memory: 0,
|
|
658
|
+
elapsed: 0
|
|
659
|
+
};
|
|
660
|
+
return {
|
|
661
|
+
...runner,
|
|
662
|
+
...stat
|
|
663
|
+
};
|
|
664
|
+
}).sort((a, b) => b.elapsed - a.elapsed).map(({ pid, command, schedule, elapsed, cpu, memory }) => {
|
|
665
|
+
return [
|
|
666
|
+
pid,
|
|
667
|
+
command.padEnd(24, " "),
|
|
668
|
+
schedule,
|
|
669
|
+
`${cpu.toFixed(2)}%`,
|
|
670
|
+
bytes(memory),
|
|
671
|
+
formatDuration(elapsed, { leading: true })
|
|
672
|
+
];
|
|
673
|
+
})], {
|
|
674
|
+
columnDefault: { paddingRight: 4 },
|
|
675
|
+
columns: { 1: {
|
|
676
|
+
paddingRight: 1,
|
|
677
|
+
width: 24,
|
|
678
|
+
wrapWord: true
|
|
679
|
+
} },
|
|
680
|
+
drawVerticalLine: () => false,
|
|
681
|
+
drawHorizontalLine: () => false
|
|
682
|
+
}));
|
|
683
|
+
}
|
|
684
|
+
});
|
|
685
|
+
await promise;
|
|
686
|
+
},
|
|
687
|
+
help: { onDocument(doc) {
|
|
688
|
+
doc[commandName] = { summary: i18n.t("stats") };
|
|
689
|
+
} }
|
|
690
|
+
});
|
|
780
691
|
};
|
|
781
|
-
|
|
782
692
|
// src/middleware/crons.middleware.ts
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
}
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
693
|
+
const crons = (opts) => {
|
|
694
|
+
const middlewareList = [
|
|
695
|
+
start(opts),
|
|
696
|
+
eject(opts),
|
|
697
|
+
stop(opts),
|
|
698
|
+
stats(opts)
|
|
699
|
+
];
|
|
700
|
+
const fn = compose(middlewareList);
|
|
701
|
+
return middleware.console({
|
|
702
|
+
fn,
|
|
703
|
+
help: {
|
|
704
|
+
async onDocument(_, { children }) {
|
|
705
|
+
await children(middlewareList);
|
|
706
|
+
},
|
|
707
|
+
async postDocument(_, { children }) {
|
|
708
|
+
await children(middlewareList);
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
});
|
|
802
712
|
};
|
|
713
|
+
export { CronMiddleware, cron, crons };
|
|
714
|
+
|
|
803
715
|
//# sourceMappingURL=index.js.map
|