@whyour/qinglong 2.22.0 → 2.23.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 (150) hide show
  1. package/back/protos/cron.proto +4 -0
  2. package/docs/development/node-cron-migration.md +49 -0
  3. package/docs/development/task-startup.md +78 -0
  4. package/docs/releases/2.22.0.md +8 -0
  5. package/docs/releases/2.23.0.md +33 -0
  6. package/docs/script-history.md +53 -0
  7. package/package.json +9 -8
  8. package/scripts/benchmark-script-history.cjs +39 -0
  9. package/scripts/publish-static.sh +34 -0
  10. package/scripts/sync-static.sh +20 -0
  11. package/scripts/wait-for-mirror.sh +214 -0
  12. package/shell/api.sh +56 -70
  13. package/shell/node_path_cache.sh +10 -2
  14. package/shell/otask.sh +7 -9
  15. package/shell/preload/sitecustomize.js +6 -2
  16. package/shell/preload/sitecustomize.py +4 -2
  17. package/shell/share.sh +1 -1
  18. package/shell/task-timeout.sh +63 -0
  19. package/shell/task.sh +8 -6
  20. package/shell/update.sh +40 -95
  21. package/shell/upgrade.cjs +8 -0
  22. package/static/build/api/config.js +2 -6
  23. package/static/build/api/env.js +15 -14
  24. package/static/build/api/open.js +2 -1
  25. package/static/build/api/script.js +155 -9
  26. package/static/build/api/subscription.js +3 -6
  27. package/static/build/api/system.js +35 -3
  28. package/static/build/config/grpcCerts.js +89 -40
  29. package/static/build/config/subscription.js +19 -11
  30. package/static/build/config/util.js +6 -4
  31. package/static/build/loaders/express.js +2 -0
  32. package/static/build/loaders/initData.js +20 -30
  33. package/static/build/loaders/initTask.js +11 -1
  34. package/static/build/middlewares/uniqueConstraintError.js +15 -0
  35. package/static/build/protos/cron.js +63 -2
  36. package/static/build/schedule/addCron.js +45 -63
  37. package/static/build/schedule/client.js +19 -1
  38. package/static/build/schedule/delCron.js +1 -1
  39. package/static/build/schedule/setConcurrency.js +27 -0
  40. package/static/build/services/config.js +2 -3
  41. package/static/build/services/cron.js +36 -10
  42. package/static/build/services/env.js +1 -1
  43. package/static/build/services/grpc.js +2 -1
  44. package/static/build/services/log.js +1 -1
  45. package/static/build/services/open.js +106 -31
  46. package/static/build/services/schedule.js +20 -10
  47. package/static/build/services/script.js +1 -1
  48. package/static/build/services/scriptHistory.js +30 -0
  49. package/static/build/services/sshKey.js +39 -14
  50. package/static/build/services/subscription.js +104 -53
  51. package/static/build/services/system.js +67 -32
  52. package/static/build/services/user.js +15 -10
  53. package/static/build/shared/cronCommand.js +17 -0
  54. package/static/build/shared/cronSchedule.js +101 -0
  55. package/static/build/shared/cronScheduler.js +73 -0
  56. package/static/build/shared/i18n.js +3 -2
  57. package/static/build/shared/logPath.js +15 -0
  58. package/static/build/shared/loginThrottle.js +45 -0
  59. package/static/build/shared/pLimit.js +19 -6
  60. package/static/build/shared/runCron.js +13 -4
  61. package/static/build/shared/scriptHistory.js +445 -0
  62. package/static/build/shared/shellQuote.js +11 -0
  63. package/static/build/shared/subscriptionMutationLock.js +34 -0
  64. package/static/build/shared/subscriptionPath.js +41 -0
  65. package/static/build/shared/subscriptionStorage.js +98 -0
  66. package/static/build/shared/utils.js +2 -1
  67. package/static/build/token.js +3 -14
  68. package/static/build/validation/schedule.js +3 -18
  69. package/static/dist/292.264757f0.async.js +1 -0
  70. package/static/dist/3512.95e41255.async.js +1 -0
  71. package/static/dist/index.html +2 -2
  72. package/static/dist/preload_helper.01fcd534.js +1 -0
  73. package/static/dist/src__pages__dependence__modal.0fa8b421.async.js +1 -0
  74. package/static/dist/src__pages__diff__index.b94e5dc5.async.js +1 -0
  75. package/static/dist/src__pages__script__editModal.afabf073.async.js +1 -0
  76. package/static/dist/src__pages__script__editNameModal.789ad87b.async.js +1 -0
  77. package/static/dist/src__pages__script__historyDrawer.8c58d958.async.js +1 -0
  78. package/static/dist/src__pages__script__historyDrawer.a449193a.chunk.css +1 -0
  79. package/static/dist/src__pages__script__index.ab360776.chunk.css +1 -0
  80. package/static/dist/src__pages__script__index.fc575945.async.js +1 -0
  81. package/static/dist/src__pages__script__saveModal.8fca1a22.async.js +1 -0
  82. package/static/dist/src__pages__script__saveWithHistory.99ece830.async.js +1 -0
  83. package/static/dist/src__pages__subscription__modal.30868ba8.async.js +1 -0
  84. package/static/dist/{umi.fc019050.js → umi.3a24a9fa.js} +1 -1
  85. package/test/back/advisory-input-security.test.cjs +416 -0
  86. package/test/back/code-scanning-security.test.cjs +189 -0
  87. package/test/back/cron-macros.test.cjs +51 -0
  88. package/test/back/cron-schedule-compatibility.test.cjs +69 -0
  89. package/test/back/cron-scheduler-routing.test.cjs +59 -0
  90. package/test/back/draft-path-security.test.cjs +235 -0
  91. package/test/back/env-import-security.test.cjs +91 -0
  92. package/test/back/execution-lifecycle.test.cjs +1 -1
  93. package/test/back/file-access-scope.test.cjs +180 -0
  94. package/test/back/grpc-mtls-security.test.cjs +136 -0
  95. package/test/back/http-security.test.cjs +105 -5
  96. package/test/back/legacy-cron-recovery.test.cjs +76 -0
  97. package/test/back/linux-mirror-result.test.cjs +176 -0
  98. package/test/back/listen-security.test.cjs +1 -0
  99. package/test/back/mirror-status.test.cjs +257 -0
  100. package/test/back/node-cron-migration.test.cjs +65 -0
  101. package/test/back/node-path-cache.test.cjs +29 -0
  102. package/test/back/node-path-lock.test.cjs +6 -2
  103. package/test/back/preload-lazy-notify.test.cjs +168 -0
  104. package/test/back/queued-cron-invalidation.test.cjs +98 -0
  105. package/test/back/restart-instance-recovery.test.cjs +116 -0
  106. package/test/back/scheduler-bulk-recovery.test.cjs +40 -0
  107. package/test/back/scheduler-concurrency.test.cjs +411 -0
  108. package/test/back/scheduler-reconciliation.test.cjs +58 -4
  109. package/test/back/script-file-access.test.cjs +210 -0
  110. package/test/back/script-history-api.test.cjs +551 -0
  111. package/test/back/script-history.test.cjs +383 -0
  112. package/test/back/shell-api-parsing.test.cjs +4 -4
  113. package/test/back/shell-translation-output.test.cjs +16 -0
  114. package/test/back/shell-upgrade.test.cjs +312 -0
  115. package/test/back/shellApiToken.test.cjs +151 -0
  116. package/test/back/ssh-config-home.test.cjs +113 -0
  117. package/test/back/static-mirror-trigger.test.cjs +441 -0
  118. package/test/back/static-mirror.test.cjs +179 -0
  119. package/test/back/stop-race.test.cjs +5 -1
  120. package/test/back/subscription-mutation-lock.test.cjs +83 -0
  121. package/test/back/subscription-review-regression.test.cjs +607 -0
  122. package/test/back/subscription-storage-regression.test.cjs +553 -0
  123. package/test/back/system-dependence-cache.test.cjs +1 -0
  124. package/test/back/system-export-history.test.cjs +119 -0
  125. package/test/back/systemTokenRenewal.test.cjs +497 -0
  126. package/test/back/task-shell-lifecycle.test.cjs +194 -0
  127. package/test/back/unique-constraint-error.test.cjs +72 -0
  128. package/test/back/upgrade-guards.test.cjs +285 -0
  129. package/test/back/upgrade-lifecycle.test.cjs +288 -0
  130. package/test/back/upgrade-transaction.test.cjs +133 -0
  131. package/test/back/user-security.test.cjs +102 -1
  132. package/test/back/wpush-grpc.test.cjs +1 -0
  133. package/test/fixtures/legacy-cron.json +1371 -0
  134. package/test/front/diff-file-routing.test.cjs +126 -0
  135. package/test/front/script-history-save.test.cjs +121 -0
  136. package/test/helpers/upgrade-fixture.cjs +30 -0
  137. package/version.yaml +14 -19
  138. package/static/dist/2096.6d98fd12.async.js +0 -1
  139. package/static/dist/4859.97d6653c.async.js +0 -1
  140. package/static/dist/6707.801b886b.async.js +0 -1
  141. package/static/dist/preload_helper.cf7de3fa.js +0 -1
  142. package/static/dist/src__pages__dependence__modal.a29c1a0f.async.js +0 -1
  143. package/static/dist/src__pages__diff__index.1714b941.async.js +0 -1
  144. package/static/dist/src__pages__script__editModal.22acdf7c.async.js +0 -1
  145. package/static/dist/src__pages__script__editNameModal.1a521cc0.async.js +0 -1
  146. package/static/dist/src__pages__script__index.60b6f827.chunk.css +0 -1
  147. package/static/dist/src__pages__script__index.a18b2692.async.js +0 -1
  148. package/static/dist/src__pages__script__saveModal.8965b74c.async.js +0 -1
  149. package/static/dist/src__pages__subscription__modal.38991e1e.async.js +0 -1
  150. /package/static/dist/{4859.1b962b29.chunk.css → 292.1b962b29.chunk.css} +0 -0
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getInvalidCronSchedules = exports.isValidCronSchedule = exports.parseCronSchedule = void 0;
7
+ const cron_parser_1 = __importDefault(require("cron-parser"));
8
+ const node_cron_1 = require("node-cron");
9
+ const schedule_1 = require("../interface/schedule");
10
+ const aliases = {
11
+ '@yearly': '0 0 0 1 1 *',
12
+ '@annually': '0 0 0 1 1 *',
13
+ '@monthly': '0 0 0 1 * *',
14
+ '@weekly': '0 0 0 * * 0',
15
+ '@daily': '0 0 0 * * *',
16
+ '@midnight': '0 0 0 * * *',
17
+ '@hourly': '0 0 * * * *',
18
+ '@minutely': '0 * * * * *',
19
+ };
20
+ // Validation and registration repeatedly see identical rules in large snapshots.
21
+ // Cache only immutable syntax, never dates or running tasks, and bound user input.
22
+ const scheduleCache = new Map();
23
+ // Canonicalize legacy shorthand, aliases, names and numeric-start steps before
24
+ // node-cron sees them. Extend legacy aliases explicitly; reject H and bare /N.
25
+ function parseCronSchedule(schedule) {
26
+ if (typeof schedule !== 'string' || !schedule.trim()) {
27
+ throw new Error('Invalid cron schedule');
28
+ }
29
+ let source = schedule.trim();
30
+ const cacheKey = source;
31
+ const cached = scheduleCache.get(cacheKey);
32
+ if (cached)
33
+ return cached;
34
+ if (source.startsWith('@')) {
35
+ if (!Object.hasOwn(aliases, source))
36
+ throw new Error('Invalid cron alias');
37
+ source = aliases[source];
38
+ }
39
+ let parts = source.split(/\s+/);
40
+ if (parts.length > 6 ||
41
+ parts.some((part) => /(^|,)\//.test(part) || /H(?:\(|\/|$)/i.test(part))) {
42
+ throw new Error('Unsupported cron syntax');
43
+ }
44
+ parts = [
45
+ ...['0', '*', '*', '*', '*', '*'].slice(0, 6 - parts.length),
46
+ ...parts,
47
+ ];
48
+ if (parts.some((part, index) => index !== 3 && index !== 5 && part.includes('?'))) {
49
+ throw new Error('Question mark is only valid in day fields');
50
+ }
51
+ const expression = cron_parser_1.default.parse(parts.join(' '));
52
+ if (!expression.hasNext())
53
+ throw new Error('Cron schedule has no next execution');
54
+ const normalized = expression.stringify(true).replace(/\?/g, '*').split(' ');
55
+ const dayCount = expression.fields.dayOfMonth.values.length;
56
+ const weekCount = expression.fields.dayOfWeek.values.length;
57
+ const patterns = dayCount < 31 && weekCount < 8
58
+ ? [
59
+ [...normalized.slice(0, 5), '*'].join(' '),
60
+ [...normalized.slice(0, 3), '*', ...normalized.slice(4)].join(' '),
61
+ ]
62
+ : [normalized.join(' ')];
63
+ if (!patterns.every((pattern) => (0, node_cron_1.validate)(pattern))) {
64
+ throw new Error('Unsupported cron schedule');
65
+ }
66
+ const parsed = {
67
+ patterns,
68
+ dayCount,
69
+ nthDay: expression.fields.dayOfWeek.nthDay,
70
+ };
71
+ Object.freeze(patterns);
72
+ Object.freeze(parsed);
73
+ if (scheduleCache.size >= 512)
74
+ scheduleCache.delete(scheduleCache.keys().next().value);
75
+ scheduleCache.set(cacheKey, parsed);
76
+ return parsed;
77
+ }
78
+ exports.parseCronSchedule = parseCronSchedule;
79
+ function isValidCronSchedule(schedule) {
80
+ try {
81
+ parseCronSchedule(schedule);
82
+ return true;
83
+ }
84
+ catch (_a) {
85
+ return false;
86
+ }
87
+ }
88
+ exports.isValidCronSchedule = isValidCronSchedule;
89
+ function getInvalidCronSchedules(cron) {
90
+ var _a, _b;
91
+ if (((_a = cron.schedule) === null || _a === void 0 ? void 0 : _a.startsWith(schedule_1.ScheduleType.ONCE)) ||
92
+ ((_b = cron.schedule) === null || _b === void 0 ? void 0 : _b.startsWith(schedule_1.ScheduleType.BOOT))) {
93
+ return [];
94
+ }
95
+ return [
96
+ cron.schedule,
97
+ ...(cron.extra_schedules || []).map((x) => x.schedule),
98
+ ].filter((schedule) => !isValidCronSchedule(schedule));
99
+ }
100
+ exports.getInvalidCronSchedules = getInvalidCronSchedules;
101
+ //# sourceMappingURL=cronSchedule.js.map
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createCronJob = void 0;
4
+ const node_cron_1 = require("node-cron");
5
+ const cronSchedule_1 = require("./cronSchedule");
6
+ // node-cron owns timers and calendar calculation; Qinglong owns execution
7
+ // concurrency and process lifetime. Missed in-process slots join that same queue.
8
+ function createCronJob(schedule, callback, options) {
9
+ const parsed = (0, cronSchedule_1.parseCronSchedule)(schedule);
10
+ const tasks = [];
11
+ let cancelled = false;
12
+ let started = false;
13
+ const accepts = (index, date) => {
14
+ if (tasks.length === 1)
15
+ return true;
16
+ // Preserve cron-parser 4's day/weekday OR semantics, including its
17
+ // month-dependent full-day range and the global nth-week constraint.
18
+ if (parsed.nthDay && Math.ceil(date.getDate() / 7) !== parsed.nthDay)
19
+ return false;
20
+ if (index === 1)
21
+ return !tasks[0].match(date);
22
+ const monthDays = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
23
+ return parsed.dayCount < monthDays[date.getMonth()] || tasks[1].match(date);
24
+ };
25
+ const dispatch = async (index, date, missed) => {
26
+ if (cancelled || !started || !accepts(index, date))
27
+ return;
28
+ if (missed) {
29
+ options.logger.warn('[schedule][补执行迟到任务] 任务: %s, 计划时间: %s', options.name, date.toISOString());
30
+ }
31
+ try {
32
+ await callback(date);
33
+ }
34
+ catch (error) {
35
+ options.logger.error('[schedule][定时回调失败] 任务: %s, 错误: %s', options.name, error instanceof Error ? error.message : String(error));
36
+ }
37
+ };
38
+ try {
39
+ for (const [index, pattern] of parsed.patterns.entries()) {
40
+ const task = (0, node_cron_1.createTask)(pattern, (context) => dispatch(index, context.date, false), {
41
+ noOverlap: false,
42
+ missedExecutionTolerance: 5000,
43
+ });
44
+ tasks.push(task);
45
+ task.on('execution:missed', (context) => dispatch(index, context.date, true));
46
+ // Fail before an existing schedule is cancelled, including impossible dates.
47
+ task.getNextRuns(1);
48
+ }
49
+ }
50
+ catch (error) {
51
+ tasks.forEach((task) => task.destroy());
52
+ throw error;
53
+ }
54
+ const job = {
55
+ start() {
56
+ if (started || cancelled)
57
+ return;
58
+ started = true;
59
+ tasks.forEach((task) => task.start());
60
+ },
61
+ cancel() {
62
+ if (cancelled)
63
+ return;
64
+ cancelled = true;
65
+ tasks.forEach((task) => task.destroy());
66
+ },
67
+ };
68
+ if (options.start !== false)
69
+ job.start();
70
+ return job;
71
+ }
72
+ exports.createCronJob = createCronJob;
73
+ //# sourceMappingURL=cronScheduler.js.map
@@ -5,6 +5,7 @@ const store_1 = require("./store");
5
5
  const messages = {
6
6
  zh: {},
7
7
  en: {
8
+ '资源已存在,请检查重复的名称或值': 'Resource already exists; check for duplicate names or values',
8
9
  '暂无权限': 'Access denied',
9
10
  '参数错误': 'Invalid parameter',
10
11
  '参数不正确': 'Invalid parameter',
@@ -132,8 +133,8 @@ const messages = {
132
133
  '调度器注册失败,任务创建已回滚': 'Scheduler registration failed, task creation rolled back',
133
134
  '调度器注册失败,任务更新已回滚': 'Scheduler registration failed, task update rolled back',
134
135
  '调度器注册失败,任务启用已回滚': 'Scheduler registration failed, task enable rolled back',
135
- '任务ID %s: 无效的 cron 表达式 "%s"(不支持裸 /N 步长和 ? 字符)': 'Task ID %s: invalid cron expression "%s" (bare /N steps and ? character not supported)',
136
- '任务ID %s (extra_schedule): 无效的 cron 表达式 "%s"(不支持裸 /N 步长和 ? 字符)': 'Task ID %s (extra_schedule): invalid cron expression "%s" (bare /N steps and ? character not supported)',
136
+ '任务ID %s: 无效的 cron 表达式 "%s"': 'Task ID %s: invalid cron expression "%s"',
137
+ '任务ID %s (extra_schedule): 无效的 cron 表达式 "%s"': 'Task ID %s (extra_schedule): invalid cron expression "%s"',
137
138
  },
138
139
  };
139
140
  let currentLang = 'zh';
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resolveLogPath = void 0;
4
+ const fileAccess_1 = require("./fileAccess");
5
+ /** Check stored paths too: old database rows are not trusted input. */
6
+ function resolveLogPath(root, value) {
7
+ const target = (0, fileAccess_1.resolveFileAccess)(root, [value]);
8
+ if (!target)
9
+ throw Object.assign(new Error('Log path is outside the log directory'), {
10
+ status: 400,
11
+ });
12
+ return target;
13
+ }
14
+ exports.resolveLogPath = resolveLogPath;
15
+ //# sourceMappingURL=logPath.js.map
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LoginThrottle = void 0;
4
+ const RETRY_TTL = 15 * 60 * 1000;
5
+ const MAX_SOURCES = 10000;
6
+ const MAX_RETRY_SECONDS = 5 * 60;
7
+ /** Source-specific, bounded backoff; persisted account history is not a lock. */
8
+ class LoginThrottle {
9
+ constructor() {
10
+ this.sources = new Map();
11
+ }
12
+ get(source, now) {
13
+ const state = this.sources.get(source);
14
+ if (state && now - state.lastFailure >= RETRY_TTL) {
15
+ this.sources.delete(source);
16
+ return undefined;
17
+ }
18
+ return state;
19
+ }
20
+ retryAfter(source, now) {
21
+ const state = this.get(source, now);
22
+ if (!state || state.failures < 3)
23
+ return 0;
24
+ const seconds = Math.min(3 ** state.failures, MAX_RETRY_SECONDS);
25
+ return Math.max(0, Math.ceil((seconds * 1000 - (now - state.lastFailure)) / 1000));
26
+ }
27
+ fail(source, now) {
28
+ var _a;
29
+ const failures = Math.min((((_a = this.get(source, now)) === null || _a === void 0 ? void 0 : _a.failures) || 0) + 1, 6);
30
+ this.sources.delete(source);
31
+ if (this.sources.size >= MAX_SOURCES) {
32
+ this.sources.delete(this.sources.keys().next().value);
33
+ }
34
+ this.sources.set(source, { failures, lastFailure: now });
35
+ return failures;
36
+ }
37
+ reset(source) {
38
+ this.sources.delete(source);
39
+ }
40
+ clear() {
41
+ this.sources.clear();
42
+ }
43
+ }
44
+ exports.LoginThrottle = LoginThrottle;
45
+ //# sourceMappingURL=loginThrottle.js.map
@@ -20,7 +20,9 @@ class TaskLimit {
20
20
  const creds = tlsConfig
21
21
  ? grpc_js_1.credentials.createSsl(Buffer.from(tlsConfig.caCert), Buffer.from(tlsConfig.clientKey), Buffer.from(tlsConfig.clientCert))
22
22
  : grpc_js_1.credentials.createInsecure();
23
- this._client = new api_1.ApiClient(`localhost:${config_1.default.grpcPort}`, creds, { 'grpc.enable_http_proxy': 0 });
23
+ this._client = new api_1.ApiClient(`localhost:${config_1.default.grpcPort}`, creds, {
24
+ 'grpc.enable_http_proxy': 0,
25
+ });
24
26
  }
25
27
  return this._client;
26
28
  }
@@ -84,12 +86,17 @@ class TaskLimit {
84
86
  this.queuedDependencyIds.delete(dependency.id);
85
87
  }
86
88
  }
87
- removeQueuedCron(id) {
89
+ removeQueuedCron(id, completed) {
88
90
  if (this.queuedCrons.has(id)) {
89
91
  const runs = this.queuedCrons.get(id);
90
92
  if (runs && runs.length > 0) {
91
- runs.pop();
92
- this.queuedCrons.set(id, runs);
93
+ const remaining = completed
94
+ ? runs.filter((run) => run !== completed)
95
+ : runs.slice(0, -1);
96
+ if (remaining.length)
97
+ this.queuedCrons.set(id, remaining);
98
+ else
99
+ this.queuedCrons.delete(id);
93
100
  }
94
101
  }
95
102
  }
@@ -109,9 +116,15 @@ class TaskLimit {
109
116
  this.manualCronoLimit.concurrency = doc.info.cronConcurrency;
110
117
  }
111
118
  }
112
- async runWithCronLimit(cron, fn, options) {
119
+ async runWithCronLimit(cron, fn, options, isCurrent = () => true) {
120
+ var _a;
121
+ if (!isCurrent())
122
+ return;
113
123
  fn.cron = cron;
114
- let runs = this.queuedCrons.get(cron.id);
124
+ fn.isCurrent = isCurrent;
125
+ // Invalidated snapshots must not consume the new revision's repeat budget.
126
+ const runs = (_a = this.queuedCrons
127
+ .get(cron.id)) === null || _a === void 0 ? void 0 : _a.filter((run) => { var _a; return ((_a = run.isCurrent) === null || _a === void 0 ? void 0 : _a.call(run)) !== false; });
115
128
  const result = (runs === null || runs === void 0 ? void 0 : runs.length) ? [...runs, fn] : [fn];
116
129
  const repeatTimes = this.repeatCronNotifyMap.get(cron.id) || 0;
117
130
  if ((result === null || result === void 0 ? void 0 : result.length) > 5) {
@@ -12,14 +12,19 @@ const util_1 = require("../config/util");
12
12
  const runningInstance_1 = require("../data/runningInstance");
13
13
  const dayjs_1 = __importDefault(require("dayjs"));
14
14
  const childProcess_1 = require("./childProcess");
15
- function runCron(cmd, cron) {
16
- return pLimit_1.default.runWithCronLimit(cron, async () => {
15
+ function runCron(cmd, cron, isCurrent = () => true) {
16
+ const execute = async () => {
17
17
  try {
18
+ if (!isCurrent())
19
+ return;
18
20
  // Check if the cron is already running and stop it (only if multiple instances are not allowed)
19
21
  try {
20
22
  const existingCron = await cron_1.CrontabModel.findOne({
21
23
  where: { id: Number(cron.id) },
22
24
  });
25
+ // A mutation may commit while this queued run is reading the database.
26
+ if (!isCurrent() || !existingCron || existingCron.isDisabled)
27
+ return;
23
28
  // Default to single instance mode (0) for backward compatibility
24
29
  const allowSingleInstances = (existingCron === null || existingCron === void 0 ? void 0 : existingCron.allow_multiple_instances) === 0;
25
30
  if (allowSingleInstances &&
@@ -45,6 +50,9 @@ function runCron(cmd, cron) {
45
50
  logger_1.default.error(`[schedule][检查已运行任务失败] 任务ID: ${cron.id}, 错误: ${error}`);
46
51
  throw error;
47
52
  }
53
+ // In particular, invalidation can happen while stopping a previous run.
54
+ if (!isCurrent())
55
+ return;
48
56
  logger_1.default.info(`[schedule][开始执行任务] 参数 ${JSON.stringify(Object.assign(Object.assign({}, cron), { command: cmd }))}`);
49
57
  const cp = (0, cross_spawn_1.spawn)(cmd, { shell: '/bin/bash' });
50
58
  const { completed } = (0, childProcess_1.observeChildProcess)(cp, {
@@ -63,9 +71,10 @@ function runCron(cmd, cron) {
63
71
  logger_1.default.error('[schedule][创建任务失败] 命令: %s, 错误: %s', cmd, (0, childProcess_1.asError)(error).message);
64
72
  }
65
73
  finally {
66
- pLimit_1.default.removeQueuedCron(cron.id);
74
+ pLimit_1.default.removeQueuedCron(cron.id, execute);
67
75
  }
68
- });
76
+ };
77
+ return pLimit_1.default.runWithCronLimit(cron, execute, undefined, isCurrent);
69
78
  }
70
79
  exports.runCron = runCron;
71
80
  //# sourceMappingURL=runCron.js.map