@whyour/qinglong 0.11.2 → 0.12.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 (174) hide show
  1. package/README-en.md +65 -58
  2. package/README.md +62 -55
  3. package/package.json +2 -2
  4. package/sample/config.sample.sh +9 -0
  5. package/sample/notify.js +260 -0
  6. package/sample/notify.py +177 -3
  7. package/shell/otask.sh +8 -6
  8. package/shell/share.sh +20 -11
  9. package/shell/task.sh +21 -5
  10. package/static/build/api/cron.js +1 -2
  11. package/static/build/api/log.js +1 -1
  12. package/static/build/api/script.js +4 -3
  13. package/static/build/config/serverEnv.js +8 -0
  14. package/static/build/config/util.js +13 -7
  15. package/static/build/data/notify.js +11 -1
  16. package/static/build/loaders/server.js +0 -4
  17. package/static/build/protos/cron.js +14 -2
  18. package/static/build/schedule/addCron.js +5 -5
  19. package/static/build/services/cron.js +20 -13
  20. package/static/build/services/dependence.js +1 -1
  21. package/static/build/services/notify.js +57 -5
  22. package/static/build/services/schedule.js +23 -11
  23. package/static/build/services/script.js +1 -1
  24. package/static/build/services/subscription.js +5 -1
  25. package/static/build/services/system.js +3 -1
  26. package/static/build/shared/pLimit.js +38 -18
  27. package/static/build/shared/runCron.js +5 -6
  28. package/static/dist/{7878.91f5096f.async.js → 1988.5291d10f.async.js} +1 -1
  29. package/static/dist/2260.6c4d0bd0.async.js +1 -0
  30. package/static/dist/2393.c35a1174.async.js +1 -0
  31. package/static/dist/{2618.d4ab1f05.async.js → 2618.7a71aedf.async.js} +1 -1
  32. package/static/dist/{2715.f6d4a9ed.async.js → 2715.292538bc.async.js} +1 -1
  33. package/static/dist/{29.dd4d8489.async.js → 29.32a92099.async.js} +1 -1
  34. package/static/dist/3435.2670d41f.async.js +1 -0
  35. package/static/dist/380.33bc8989.async.js +1 -0
  36. package/static/dist/3800.f0507af4.async.js +1 -0
  37. package/static/dist/{4124.27cef0bd.async.js → 4124.b86154ed.async.js} +1 -1
  38. package/static/dist/4163.1781a549.async.js +1 -0
  39. package/static/dist/419.e21ea0bd.async.js +1 -0
  40. package/static/dist/4378.59be202f.async.js +1 -0
  41. package/static/dist/4424.a840b858.async.js +1 -0
  42. package/static/dist/4674.1b3db685.async.js +1 -0
  43. package/static/dist/{4925.9e8a41a2.async.js → 4925.9b770864.async.js} +1 -1
  44. package/static/dist/5310.4cd7a0c1.async.js +1 -0
  45. package/static/dist/{5484.8b81227f.async.js → 5484.68655765.async.js} +1 -1
  46. package/static/dist/5619.db04f386.async.js +1 -0
  47. package/static/dist/5830.9be47cbb.async.js +1 -0
  48. package/static/dist/6644.1f11b8fa.async.js +1 -0
  49. package/static/dist/694.0baa21db.async.js +1 -0
  50. package/static/dist/6986.6a4ae181.async.js +1 -0
  51. package/static/dist/{7382.26e7f5a4.async.js → 7346.7096e3b3.async.js} +1 -1
  52. package/static/dist/{7362.9fde9d80.async.js → 7362.e740709d.async.js} +1 -1
  53. package/static/dist/7393.341d9643.async.js +1 -0
  54. package/static/dist/{7708.dc75b315.async.js → 7708.27674f3a.async.js} +1 -1
  55. package/static/dist/7742.51617038.async.js +1 -0
  56. package/static/dist/7787.2fcf966f.async.js +1 -0
  57. package/static/dist/8008.cf4ff203.async.js +1 -0
  58. package/static/dist/8171.e2986b87.async.js +1 -0
  59. package/static/dist/8297.0c80e09a.async.js +1 -0
  60. package/static/dist/833.5add31bf.async.js +1 -0
  61. package/static/dist/{8432.055570e6.async.js → 8432.d8d0ade6.async.js} +1 -1
  62. package/static/dist/858.1a385b25.async.js +1 -0
  63. package/static/dist/8934.55adca8f.async.js +1 -0
  64. package/static/dist/{9065.8be8d60f.async.js → 9065.a1341834.async.js} +1 -1
  65. package/static/dist/9206.25353766.async.js +1 -0
  66. package/static/dist/{9504.248f6493.async.js → 9504.22415050.async.js} +1 -1
  67. package/static/dist/9525.b2007159.async.js +1 -0
  68. package/static/dist/9673.ac17e45d.async.js +1 -0
  69. package/static/dist/index.html +1 -1
  70. package/static/dist/layouts__index.308c5e5e.chunk.css +1 -0
  71. package/static/dist/layouts__index.931ce4c3.async.js +1 -0
  72. package/static/dist/{src__pages__404.e6b13931.async.js → src__pages__404.e1ca45fd.async.js} +1 -1
  73. package/static/dist/src__pages__config__index.9d83dfb7.async.js +1 -0
  74. package/static/dist/src__pages__crontab__detail.b91d6518.async.js +1 -0
  75. package/static/dist/src__pages__crontab__index.04176975.async.js +1 -0
  76. package/static/dist/src__pages__crontab__index.b622c4d6.chunk.css +1 -0
  77. package/static/dist/src__pages__crontab__logModal.91357c7c.async.js +1 -0
  78. package/static/dist/src__pages__crontab__modal.3fa8f075.async.js +1 -0
  79. package/static/dist/{src__pages__crontab__viewCreateModal.c46df55e.async.js → src__pages__crontab__viewCreateModal.a29322e7.async.js} +1 -1
  80. package/static/dist/{src__pages__crontab__viewManageModal.67aa809d.async.js → src__pages__crontab__viewManageModal.c026d403.async.js} +1 -1
  81. package/static/dist/{src__pages__dependence__index.1908bb39.async.js → src__pages__dependence__index.21153c24.async.js} +1 -1
  82. package/static/dist/src__pages__dependence__logModal.5aada63c.async.js +1 -0
  83. package/static/dist/src__pages__dependence__modal.96353c88.async.js +1 -0
  84. package/static/dist/src__pages__diff__index.e644b15f.async.js +1 -0
  85. package/static/dist/src__pages__env__editNameModal.25c21e59.async.js +1 -0
  86. package/static/dist/src__pages__env__index.f545d035.async.js +1 -0
  87. package/static/dist/src__pages__env__modal.4fd6abc2.async.js +1 -0
  88. package/static/dist/src__pages__error__index.f3ca5563.async.js +1 -0
  89. package/static/dist/src__pages__initialization__index.727e0f70.async.js +1 -0
  90. package/static/dist/{src__pages__log__index.4010410c.async.js → src__pages__log__index.ba2c1721.async.js} +1 -1
  91. package/static/dist/src__pages__login__index.e4257741.async.js +1 -0
  92. package/static/dist/src__pages__script__editModal.8d7bac2a.async.js +1 -0
  93. package/static/dist/src__pages__script__editNameModal.ca76e055.async.js +1 -0
  94. package/static/dist/src__pages__script__index.da31e2c2.async.js +1 -0
  95. package/static/dist/src__pages__script__renameModal.c166915b.async.js +1 -0
  96. package/static/dist/src__pages__script__saveModal.7ee07566.async.js +1 -0
  97. package/static/dist/src__pages__script__setting.020a7629.async.js +1 -0
  98. package/static/dist/src__pages__setting__about.40dce429.async.js +1 -0
  99. package/static/dist/src__pages__setting__appModal.efa5bb29.async.js +1 -0
  100. package/static/dist/src__pages__setting__checkUpdate.42020a45.async.js +1 -0
  101. package/static/dist/src__pages__setting__index.540c33a4.async.js +1 -0
  102. package/static/dist/src__pages__setting__loginLog.fe7238b5.async.js +1 -0
  103. package/static/dist/src__pages__setting__notification.b99a4c64.async.js +1 -0
  104. package/static/dist/src__pages__setting__other.d20be1c1.async.js +1 -0
  105. package/static/dist/src__pages__setting__progress.65e2e878.async.js +1 -0
  106. package/static/dist/src__pages__setting__security.d14993c7.async.js +1 -0
  107. package/static/dist/src__pages__setting__systemLog.751cc94d.async.js +1 -0
  108. package/static/dist/src__pages__subscription__index.0f0a7c0a.async.js +1 -0
  109. package/static/dist/src__pages__subscription__logModal.fce8a9b4.async.js +1 -0
  110. package/static/dist/src__pages__subscription__modal.61e787ce.async.js +1 -0
  111. package/static/dist/umi.0397b91d.js +1 -0
  112. package/version.yaml +17 -10
  113. package/static/dist/1124.0d983222.async.js +0 -1
  114. package/static/dist/2260.6b2b0020.async.js +0 -1
  115. package/static/dist/2393.4404a4fc.async.js +0 -1
  116. package/static/dist/2536.282cde37.async.js +0 -1
  117. package/static/dist/3271.e5502cf4.async.js +0 -1
  118. package/static/dist/3435.4a54080b.async.js +0 -1
  119. package/static/dist/380.1bd1f124.async.js +0 -1
  120. package/static/dist/3800.fca63cfc.async.js +0 -1
  121. package/static/dist/4163.2fc47c9d.async.js +0 -1
  122. package/static/dist/419.f84c4548.async.js +0 -1
  123. package/static/dist/4424.73f63db5.async.js +0 -1
  124. package/static/dist/4674.0196afcf.async.js +0 -1
  125. package/static/dist/5310.213fe20f.async.js +0 -1
  126. package/static/dist/6433.d9698b6c.async.js +0 -1
  127. package/static/dist/6644.fe01612c.async.js +0 -1
  128. package/static/dist/6805.5446eb5a.async.js +0 -1
  129. package/static/dist/6986.57a091e1.async.js +0 -1
  130. package/static/dist/7393.1f1df786.async.js +0 -1
  131. package/static/dist/7742.863081f5.async.js +0 -1
  132. package/static/dist/8008.882bf214.async.js +0 -1
  133. package/static/dist/8037.dd53e9c8.async.js +0 -1
  134. package/static/dist/8297.c44afb18.async.js +0 -1
  135. package/static/dist/833.ca85f497.async.js +0 -1
  136. package/static/dist/858.1899883e.async.js +0 -1
  137. package/static/dist/9673.5576fcd4.async.js +0 -1
  138. package/static/dist/9692.e04f774e.async.js +0 -1
  139. package/static/dist/layouts__index.0909c66d.chunk.css +0 -1
  140. package/static/dist/layouts__index.ed595716.async.js +0 -1
  141. package/static/dist/src__pages__config__index.f41a22d4.async.js +0 -1
  142. package/static/dist/src__pages__crontab__detail.b4fc2995.async.js +0 -1
  143. package/static/dist/src__pages__crontab__index.6c7b7b2c.chunk.css +0 -1
  144. package/static/dist/src__pages__crontab__index.776dba58.async.js +0 -1
  145. package/static/dist/src__pages__crontab__logModal.0fb6d62d.async.js +0 -1
  146. package/static/dist/src__pages__crontab__modal.0b01490c.async.js +0 -1
  147. package/static/dist/src__pages__dependence__logModal.8ecc9316.async.js +0 -1
  148. package/static/dist/src__pages__dependence__modal.9c12e6bb.async.js +0 -1
  149. package/static/dist/src__pages__diff__index.e23d088b.async.js +0 -1
  150. package/static/dist/src__pages__env__editNameModal.6f1edde6.async.js +0 -1
  151. package/static/dist/src__pages__env__index.3b08292e.async.js +0 -1
  152. package/static/dist/src__pages__env__modal.ca6133e6.async.js +0 -1
  153. package/static/dist/src__pages__error__index.0b4f4a22.async.js +0 -1
  154. package/static/dist/src__pages__initialization__index.51ebefcb.async.js +0 -1
  155. package/static/dist/src__pages__login__index.8667d8d2.async.js +0 -1
  156. package/static/dist/src__pages__script__editModal.b020a209.async.js +0 -1
  157. package/static/dist/src__pages__script__editNameModal.9220eb1a.async.js +0 -1
  158. package/static/dist/src__pages__script__index.6184a17e.async.js +0 -1
  159. package/static/dist/src__pages__script__renameModal.d6192fed.async.js +0 -1
  160. package/static/dist/src__pages__script__saveModal.eac5e9e7.async.js +0 -1
  161. package/static/dist/src__pages__script__setting.b958297b.async.js +0 -1
  162. package/static/dist/src__pages__setting__about.992010a5.async.js +0 -1
  163. package/static/dist/src__pages__setting__appModal.7563e7b0.async.js +0 -1
  164. package/static/dist/src__pages__setting__checkUpdate.9901a906.async.js +0 -1
  165. package/static/dist/src__pages__setting__index.2aa66013.async.js +0 -1
  166. package/static/dist/src__pages__setting__loginLog.2f2b0a78.async.js +0 -1
  167. package/static/dist/src__pages__setting__notification.e8ce9bdc.async.js +0 -1
  168. package/static/dist/src__pages__setting__other.c6067975.async.js +0 -1
  169. package/static/dist/src__pages__setting__progress.f58bffae.async.js +0 -1
  170. package/static/dist/src__pages__setting__security.cae5c68d.async.js +0 -1
  171. package/static/dist/src__pages__subscription__index.3266194c.async.js +0 -1
  172. package/static/dist/src__pages__subscription__logModal.a023b07e.async.js +0 -1
  173. package/static/dist/src__pages__subscription__modal.49a1a657.async.js +0 -1
  174. package/static/dist/umi.ff82d609.js +0 -1
@@ -31,6 +31,8 @@ const client_1 = __importDefault(require("../schedule/client"));
31
31
  const pLimit_1 = __importDefault(require("../shared/pLimit"));
32
32
  const cross_spawn_1 = require("cross-spawn");
33
33
  const dayjs_1 = __importDefault(require("dayjs"));
34
+ const pickBy_1 = __importDefault(require("lodash/pickBy"));
35
+ const omit_1 = __importDefault(require("lodash/omit"));
34
36
  let CronService = class CronService {
35
37
  constructor(logger) {
36
38
  this.logger = logger;
@@ -47,7 +49,7 @@ let CronService = class CronService {
47
49
  tab.saved = false;
48
50
  const doc = await this.insert(tab);
49
51
  await client_1.default.addCron([
50
- { id: String(doc.id), schedule: doc.schedule, command: this.makeCommand(doc), extraSchedules: doc.extra_schedules || [] },
52
+ { name: doc.name || '', id: String(doc.id), schedule: doc.schedule, command: this.makeCommand(doc), extraSchedules: doc.extra_schedules || [] },
51
53
  ]);
52
54
  await this.set_crontab();
53
55
  return doc;
@@ -66,6 +68,7 @@ let CronService = class CronService {
66
68
  await client_1.default.delCron([String(newDoc.id)]);
67
69
  await client_1.default.addCron([
68
70
  {
71
+ name: doc.name || '',
69
72
  id: String(newDoc.id),
70
73
  schedule: newDoc.schedule,
71
74
  command: this.makeCommand(newDoc),
@@ -80,7 +83,7 @@ let CronService = class CronService {
80
83
  return await this.getDb({ id: payload.id });
81
84
  }
82
85
  async status({ ids, status, pid, log_path, last_running_time = 0, last_execution_time = 0, }) {
83
- const options = {
86
+ let options = {
84
87
  status,
85
88
  pid,
86
89
  log_path,
@@ -89,7 +92,13 @@ let CronService = class CronService {
89
92
  if (last_running_time > 0) {
90
93
  options.last_running_time = last_running_time;
91
94
  }
92
- return await cron_1.CrontabModel.update(Object.assign({}, options), { where: { id: ids } });
95
+ for (const id of ids) {
96
+ const cron = await this.getDb({ id });
97
+ if (status === cron_1.CrontabStatus.idle && log_path !== cron.log_path) {
98
+ options = (0, omit_1.default)(options, ['status', 'log_path', 'pid']);
99
+ }
100
+ await cron_1.CrontabModel.update(Object.assign({}, (0, pickBy_1.default)(options, (v) => v === 0 || !!v)), { where: { id } });
101
+ }
93
102
  }
94
103
  async remove(ids) {
95
104
  await cron_1.CrontabModel.destroy({ where: { id: ids } });
@@ -338,14 +347,16 @@ let CronService = class CronService {
338
347
  await cron_1.CrontabModel.update({ status: cron_1.CrontabStatus.idle, pid: undefined }, { where: { id: ids } });
339
348
  }
340
349
  async runSingle(cronId) {
341
- return pLimit_1.default.runWithCpuLimit(() => {
350
+ return pLimit_1.default.runWithCronLimit(() => {
342
351
  return new Promise(async (resolve) => {
343
352
  var _a;
344
353
  const cron = await this.getDb({ id: cronId });
354
+ const params = { name: cron.name, command: cron.command, schedule: cron.schedule, extraSchedules: cron.extra_schedules };
345
355
  if (cron.status !== cron_1.CrontabStatus.queued) {
346
- resolve();
356
+ resolve(params);
347
357
  return;
348
358
  }
359
+ this.logger.info(`[panel][开始执行任务] 参数 ${JSON.stringify(params)}`);
349
360
  let { id, command, log_path } = cron;
350
361
  const uniqPath = await (0, util_1.getUniqPath)(command, `${id}`);
351
362
  const logTime = (0, dayjs_1.default)().format('YYYY-MM-DD-HH-mm-ss-SSS');
@@ -355,9 +366,6 @@ let CronService = class CronService {
355
366
  }
356
367
  const logPath = `${uniqPath}/${logTime}.log`;
357
368
  const absolutePath = path_1.default.resolve(config_1.default.logPath, `${logPath}`);
358
- this.logger.silly('Running job');
359
- this.logger.silly('ID: ' + id);
360
- this.logger.silly('Original command: ' + command);
361
369
  const cp = (0, cross_spawn_1.spawn)(`real_log_path=${logPath} no_delay=true ${this.makeCommand(cron)}`, { shell: '/bin/bash' });
362
370
  await cron_1.CrontabModel.update({ status: cron_1.CrontabStatus.running, pid: cp.pid, log_path: logPath }, { where: { id } });
363
371
  cp.stderr.on('data', (data) => {
@@ -366,12 +374,9 @@ let CronService = class CronService {
366
374
  cp.on('error', (err) => {
367
375
  fs_1.default.appendFileSync(`${absolutePath}`, `${JSON.stringify(err)}`);
368
376
  });
369
- cp.on('exit', async (code, signal) => {
370
- this.logger.info(`[panel][任务退出] 任务 ${command} 进程id: ${cp.pid}, 退出码 ${code}`);
371
- });
372
- cp.on('close', async (code) => {
377
+ cp.on('exit', async (code) => {
373
378
  await cron_1.CrontabModel.update({ status: cron_1.CrontabStatus.idle, pid: undefined }, { where: { id } });
374
- resolve();
379
+ resolve(Object.assign(Object.assign({}, params), { pid: cp.pid, code }));
375
380
  });
376
381
  });
377
382
  });
@@ -385,6 +390,7 @@ let CronService = class CronService {
385
390
  await cron_1.CrontabModel.update({ isDisabled: 0 }, { where: { id: ids } });
386
391
  const docs = await cron_1.CrontabModel.findAll({ where: { id: ids } });
387
392
  const crons = docs.map((doc) => ({
393
+ name: doc.name || '',
388
394
  id: String(doc.id),
389
395
  schedule: doc.schedule,
390
396
  command: this.makeCommand(doc),
@@ -502,6 +508,7 @@ let CronService = class CronService {
502
508
  const sixCron = tabs.data
503
509
  .filter((x) => x.isDisabled !== 1)
504
510
  .map((doc) => ({
511
+ name: doc.name || '',
505
512
  id: String(doc.id),
506
513
  schedule: doc.schedule,
507
514
  command: this.makeCommand(doc),
@@ -217,7 +217,7 @@ let DependenceService = class DependenceService {
217
217
  });
218
218
  this.updateLog(depIds, JSON.stringify(err));
219
219
  });
220
- cp.on('close', async (code) => {
220
+ cp.on('exit', async (code) => {
221
221
  const endTime = (0, dayjs_1.default)();
222
222
  const isSucceed = code === 0;
223
223
  const resultText = isSucceed ? '成功' : '失败';
@@ -26,14 +26,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- const typedi_1 = require("typedi");
30
- const winston_1 = __importDefault(require("winston"));
31
- const user_1 = __importDefault(require("./user"));
32
- const got_1 = __importDefault(require("got"));
33
- const nodemailer_1 = __importDefault(require("nodemailer"));
34
29
  const crypto_1 = __importDefault(require("crypto"));
30
+ const got_1 = __importDefault(require("got"));
35
31
  const hpagent_1 = require("hpagent");
32
+ const nodemailer_1 = __importDefault(require("nodemailer"));
33
+ const typedi_1 = require("typedi");
34
+ const winston_1 = __importDefault(require("winston"));
36
35
  const util_1 = require("../config/util");
36
+ const user_1 = __importDefault(require("./user"));
37
37
  let NotificationService = class NotificationService {
38
38
  constructor(logger) {
39
39
  this.logger = logger;
@@ -55,6 +55,7 @@ let NotificationService = class NotificationService {
55
55
  ['pushMe', this.pushMe],
56
56
  ['webhook', this.webhook],
57
57
  ['lark', this.lark],
58
+ ['chronocat', this.chronocat],
58
59
  ]);
59
60
  this.title = '';
60
61
  this.content = '';
@@ -508,6 +509,57 @@ let NotificationService = class NotificationService {
508
509
  throw new Error(error.response ? error.response.body : error);
509
510
  }
510
511
  }
512
+ async chronocat() {
513
+ var _a, _b;
514
+ const { chronocatURL, chronocatQQ, chronocatToekn } = this.params;
515
+ try {
516
+ const user_ids = (_a = chronocatQQ
517
+ .match(/user_id=(\d+)/g)) === null || _a === void 0 ? void 0 : _a.map((match) => match.split('=')[1]);
518
+ const group_ids = (_b = chronocatQQ
519
+ .match(/group_id=(\d+)/g)) === null || _b === void 0 ? void 0 : _b.map((match) => match.split('=')[1]);
520
+ const url = `${chronocatURL}/api/message/send`;
521
+ const headers = {
522
+ 'Content-Type': 'application/json',
523
+ Authorization: `Bearer ${chronocatToekn}`,
524
+ };
525
+ for (const [chat_type, ids] of [
526
+ [1, user_ids],
527
+ [2, group_ids],
528
+ ]) {
529
+ if (!ids) {
530
+ continue;
531
+ }
532
+ let _ids = ids;
533
+ for (const chat_id of _ids) {
534
+ const data = {
535
+ peer: {
536
+ chatType: chat_type,
537
+ peerUin: chat_id,
538
+ },
539
+ elements: [
540
+ {
541
+ elementType: 1,
542
+ textElement: {
543
+ content: `${this.title}\n\n${this.content}`,
544
+ },
545
+ },
546
+ ],
547
+ };
548
+ const res = await got_1.default.post(url, Object.assign(Object.assign({}, this.gotOption), { json: data, headers }));
549
+ if (res.body === 'success') {
550
+ return true;
551
+ }
552
+ else {
553
+ throw new Error(res.body);
554
+ }
555
+ }
556
+ }
557
+ return false;
558
+ }
559
+ catch (error) {
560
+ throw new Error(error.response ? error.response.body : error);
561
+ }
562
+ }
511
563
  async webhook() {
512
564
  const { webhookUrl, webhookBody, webhookHeaders, webhookMethod, webhookContentType, } = this.params;
513
565
  const { formatBody, formatUrl } = this.formatNotifyContent(webhookUrl, webhookBody);
@@ -29,10 +29,11 @@ let ScheduleService = class ScheduleService {
29
29
  this.intervalSchedule = new toad_scheduler_1.ToadScheduler();
30
30
  this.maxBuffer = 200 * 1024 * 1024;
31
31
  }
32
- async runTask(command, callbacks = {}, completionTime = 'end') {
33
- return pLimit_1.default.runWithCpuLimit(() => {
32
+ async runTask(command, callbacks = {}, params, completionTime = 'end') {
33
+ return pLimit_1.default.runWithCronLimit(() => {
34
34
  return new Promise(async (resolve, reject) => {
35
35
  var _a, _b, _c;
36
+ this.logger.info(`[panel][开始执行任务] 参数 ${JSON.stringify(Object.assign(Object.assign({}, params), { command }))}`);
36
37
  try {
37
38
  const startTime = (0, dayjs_1.default)();
38
39
  await ((_a = callbacks.onBefore) === null || _a === void 0 ? void 0 : _a.call(callbacks, startTime));
@@ -53,14 +54,11 @@ let ScheduleService = class ScheduleService {
53
54
  this.logger.error('[panel][创建任务失败] 命令: %s, 错误信息: %j', command, err);
54
55
  await ((_a = callbacks.onError) === null || _a === void 0 ? void 0 : _a.call(callbacks, JSON.stringify(err)));
55
56
  });
56
- cp.on('exit', async (code, signal) => {
57
- this.logger.info(`[panel][任务退出] ${command} 进程id: ${cp.pid}, 退出码 ${code}`);
58
- });
59
- cp.on('close', async (code) => {
57
+ cp.on('exit', async (code) => {
60
58
  var _a;
61
59
  const endTime = (0, dayjs_1.default)();
62
60
  await ((_a = callbacks.onEnd) === null || _a === void 0 ? void 0 : _a.call(callbacks, cp, endTime, endTime.diff(startTime, 'seconds')));
63
- resolve(null);
61
+ resolve(Object.assign(Object.assign({}, params), { pid: cp.pid, code }));
64
62
  });
65
63
  }
66
64
  catch (error) {
@@ -74,10 +72,18 @@ let ScheduleService = class ScheduleService {
74
72
  const _id = this.formatId(id);
75
73
  this.logger.info('[panel][创建cron任务], 任务ID: %s, cron: %s, 任务名: %s, 执行命令: %s', _id, schedule, name, command);
76
74
  this.scheduleStacks.set(_id, node_schedule_1.default.scheduleJob(_id, schedule, async () => {
77
- this.runTask(command, callbacks);
75
+ this.runTask(command, callbacks, {
76
+ name,
77
+ schedule,
78
+ command,
79
+ });
78
80
  }));
79
81
  if (runImmediately) {
80
- this.runTask(command, callbacks);
82
+ this.runTask(command, callbacks, {
83
+ name,
84
+ schedule,
85
+ command,
86
+ });
81
87
  }
82
88
  }
83
89
  async cancelCronTask({ id = 0, name }) {
@@ -93,14 +99,20 @@ let ScheduleService = class ScheduleService {
93
99
  const _id = this.formatId(id);
94
100
  this.logger.info('[panel][创建interval任务], 任务ID: %s, 任务名: %s, 执行命令: %s', _id, name, command);
95
101
  const task = new toad_scheduler_1.Task(name, () => {
96
- this.runTask(command, callbacks);
102
+ this.runTask(command, callbacks, {
103
+ name,
104
+ command,
105
+ });
97
106
  }, (err) => {
98
107
  this.logger.error('[执行任务失败] 命令: %s, 错误信息: %j', command, err);
99
108
  });
100
109
  const job = new toad_scheduler_1.LongIntervalJob(Object.assign({ runImmediately: false }, schedule), task, _id);
101
110
  this.intervalSchedule.addIntervalJob(job);
102
111
  if (runImmediately) {
103
- this.runTask(command, callbacks);
112
+ this.runTask(command, callbacks, {
113
+ name,
114
+ command,
115
+ });
104
116
  }
105
117
  }
106
118
  async cancelIntervalTask({ id = 0, name }) {
@@ -57,7 +57,7 @@ let ScriptService = class ScriptService {
57
57
  async runScript(filePath) {
58
58
  const relativePath = path_1.default.relative(config_1.default.scriptPath, filePath);
59
59
  const command = `${const_1.TASK_COMMAND} -l ${relativePath} now`;
60
- const pid = await this.scheduleService.runTask(command, this.taskCallbacks(filePath), 'start');
60
+ const pid = await this.scheduleService.runTask(command, this.taskCallbacks(filePath), { command }, 'start');
61
61
  return { code: 200, data: pid };
62
62
  }
63
63
  async stopScript(filePath, pid) {
@@ -266,7 +266,11 @@ let SubscriptionService = class SubscriptionService {
266
266
  return;
267
267
  }
268
268
  const command = (0, subscription_2.formatCommand)(subscription);
269
- this.scheduleService.runTask(command, this.taskCallbacks(subscription));
269
+ this.scheduleService.runTask(command, this.taskCallbacks(subscription), {
270
+ name: subscription.name,
271
+ schedule: subscription.schedule,
272
+ command
273
+ });
270
274
  }
271
275
  async disabled(ids) {
272
276
  await subscription_1.SubscriptionModel.update({ is_disabled: 1 }, { where: { id: ids } });
@@ -189,7 +189,9 @@ let SystemService = class SystemService {
189
189
  if (!command.startsWith(const_1.TASK_COMMAND)) {
190
190
  command = `${const_1.TASK_COMMAND} ${command}`;
191
191
  }
192
- this.scheduleService.runTask(`real_log_path=${logPath} real_time=true ${command}`, callback);
192
+ this.scheduleService.runTask(`real_log_path=${logPath} real_time=true ${command}`, callback, {
193
+ command,
194
+ });
193
195
  }
194
196
  async stop({ command, pid }) {
195
197
  if (!pid && !command) {
@@ -3,27 +3,48 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const p_limit_1 = __importDefault(require("p-limit"));
6
+ const p_queue_cjs_1 = __importDefault(require("p-queue-cjs"));
7
7
  const os_1 = __importDefault(require("os"));
8
8
  const auth_1 = require("../data/auth");
9
9
  const logger_1 = __importDefault(require("../loaders/logger"));
10
10
  class TaskLimit {
11
- get cpuLimitActiveCount() {
12
- return this.cpuLimit.activeCount;
11
+ get cronLimitActiveCount() {
12
+ return this.cronLimit.pending;
13
13
  }
14
- get cpuLimitPendingCount() {
15
- return this.cpuLimit.pendingCount;
14
+ get cronLimitPendingCount() {
15
+ return this.cronLimit.size;
16
16
  }
17
17
  constructor() {
18
- this.oneLimit = (0, p_limit_1.default)(1);
19
- this.updateLogLimit = (0, p_limit_1.default)(1);
20
- this.cpuLimit = (0, p_limit_1.default)(Math.max(os_1.default.cpus().length, 4));
18
+ this.oneLimit = new p_queue_cjs_1.default({ concurrency: 1 });
19
+ this.updateLogLimit = new p_queue_cjs_1.default({ concurrency: 1 });
20
+ this.cronLimit = new p_queue_cjs_1.default({ concurrency: Math.max(os_1.default.cpus().length, 4) });
21
21
  this.setCustomLimit();
22
+ this.handleEvents();
23
+ }
24
+ handleEvents() {
25
+ this.cronLimit.on('add', () => {
26
+ logger_1.default.info(`[schedule][任务加入队列] 运行中任务数: ${this.cronLimitActiveCount}, 等待中任务数: ${this.cronLimitPendingCount}`);
27
+ });
28
+ this.cronLimit.on('active', () => {
29
+ logger_1.default.info(`[schedule][开始处理任务] 运行中任务数: ${this.cronLimitActiveCount + 1}, 等待中任务数: ${this.cronLimitPendingCount}`);
30
+ });
31
+ this.cronLimit.on('completed', (param) => {
32
+ logger_1.default.info(`[schedule][任务处理成功] 参数 ${JSON.stringify(param)}`);
33
+ });
34
+ this.cronLimit.on('error', error => {
35
+ logger_1.default.error(`[schedule][任务处理错误] 参数 ${JSON.stringify(error)}`);
36
+ });
37
+ this.cronLimit.on('next', () => {
38
+ logger_1.default.info(`[schedule][任务处理结束] 运行中任务数: ${this.cronLimitActiveCount}, 等待中任务数: ${this.cronLimitPendingCount}`);
39
+ });
40
+ this.cronLimit.on('idle', () => {
41
+ logger_1.default.info(`[schedule][任务队列] 空闲中...`);
42
+ });
22
43
  }
23
44
  async setCustomLimit(limit) {
24
- var _a, _b;
45
+ var _a;
25
46
  if (limit) {
26
- this.cpuLimit = (0, p_limit_1.default)(limit);
47
+ this.cronLimit.concurrency = limit;
27
48
  return;
28
49
  }
29
50
  await auth_1.AuthModel.sync();
@@ -31,18 +52,17 @@ class TaskLimit {
31
52
  where: { type: auth_1.AuthDataType.systemConfig },
32
53
  });
33
54
  if ((_a = doc === null || doc === void 0 ? void 0 : doc.info) === null || _a === void 0 ? void 0 : _a.cronConcurrency) {
34
- this.cpuLimit = (0, p_limit_1.default)((_b = doc === null || doc === void 0 ? void 0 : doc.info) === null || _b === void 0 ? void 0 : _b.cronConcurrency);
55
+ this.cronLimit.concurrency = doc.info.cronConcurrency;
35
56
  }
36
57
  }
37
- runWithCpuLimit(fn) {
38
- logger_1.default.info(`[schedule][任务加入队列] 运行中任务数: ${this.cpuLimitActiveCount}, 等待中任务数: ${this.cpuLimitPendingCount}`);
39
- return this.cpuLimit(fn);
58
+ async runWithCronLimit(fn, options) {
59
+ return this.cronLimit.add(fn, options);
40
60
  }
41
- runOneByOne(fn) {
42
- return this.oneLimit(fn);
61
+ runOneByOne(fn, options) {
62
+ return this.oneLimit.add(fn, options);
43
63
  }
44
- updateDepLog(fn) {
45
- return this.updateLogLimit(fn);
64
+ updateDepLog(fn, options) {
65
+ return this.updateLogLimit.add(fn, options);
46
66
  }
47
67
  }
48
68
  exports.default = new TaskLimit();
@@ -7,10 +7,10 @@ exports.runCron = void 0;
7
7
  const cross_spawn_1 = require("cross-spawn");
8
8
  const pLimit_1 = __importDefault(require("./pLimit"));
9
9
  const logger_1 = __importDefault(require("../loaders/logger"));
10
- function runCron(cmd) {
11
- return pLimit_1.default.runWithCpuLimit(() => {
10
+ function runCron(cmd, options) {
11
+ return pLimit_1.default.runWithCronLimit(() => {
12
12
  return new Promise(async (resolve) => {
13
- logger_1.default.info(`[schedule][开始执行任务] 运行命令: ${cmd}`);
13
+ logger_1.default.info(`[schedule][开始执行任务] 参数 ${JSON.stringify(Object.assign(Object.assign({}, options), { command: cmd }))}`);
14
14
  const cp = (0, cross_spawn_1.spawn)(cmd, { shell: '/bin/bash' });
15
15
  cp.stderr.on('data', (data) => {
16
16
  logger_1.default.info('[schedule][执行任务失败] 命令: %s, 错误信息: %j', cmd, data.toString());
@@ -18,9 +18,8 @@ function runCron(cmd) {
18
18
  cp.on('error', (err) => {
19
19
  logger_1.default.error('[schedule][创建任务失败] 命令: %s, 错误信息: %j', cmd, err);
20
20
  });
21
- cp.on('close', async (code) => {
22
- logger_1.default.info(`[schedule][任务退出] ${cmd} 进程id: ${cp.pid} 退出, 退出码 ${code}`);
23
- resolve();
21
+ cp.on('exit', async (code) => {
22
+ resolve(Object.assign(Object.assign({}, options), { command: cmd, pid: cp.pid, code }));
24
23
  });
25
24
  });
26
25
  });