@ruiapp/rapid-core 0.8.3 → 0.8.5

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.js CHANGED
@@ -19,24 +19,6 @@ var xstate = require('xstate');
19
19
 
20
20
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
21
21
 
22
- function _interopNamespace(e) {
23
- if (e && e.__esModule) return e;
24
- var n = Object.create(null);
25
- if (e) {
26
- Object.keys(e).forEach(function (k) {
27
- if (k !== 'default') {
28
- var d = Object.getOwnPropertyDescriptor(e, k);
29
- Object.defineProperty(n, k, d.get ? d : {
30
- enumerable: true,
31
- get: function () { return e[k]; }
32
- });
33
- }
34
- });
35
- }
36
- n["default"] = e;
37
- return Object.freeze(n);
38
- }
39
-
40
22
  var Router__default = /*#__PURE__*/_interopDefaultLegacy(Router);
41
23
  var qs__default = /*#__PURE__*/_interopDefaultLegacy(qs);
42
24
  var dayjs__default = /*#__PURE__*/_interopDefaultLegacy(dayjs);
@@ -45,7 +27,6 @@ var bcrypt__default = /*#__PURE__*/_interopDefaultLegacy(bcrypt);
45
27
  var path__default = /*#__PURE__*/_interopDefaultLegacy(path);
46
28
  var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
47
29
  var nodemailer__default = /*#__PURE__*/_interopDefaultLegacy(nodemailer);
48
- var cron__namespace = /*#__PURE__*/_interopNamespace(cron);
49
30
 
50
31
  function fixBigIntJSONSerialize() {
51
32
  BigInt.prototype.toJSON = function () {
@@ -8486,11 +8467,12 @@ async function handler$1(plugin, ctx, options) {
8486
8467
  if (!input.code) {
8487
8468
  throw new Error(`Cron job code is required.`);
8488
8469
  }
8489
- const job = plugin.getJobConfigurationByCode(input.code);
8470
+ const cronJobService = server.getService("cronJobService");
8471
+ const job = cronJobService.getJobConfigurationByCode(input.code);
8490
8472
  if (!job) {
8491
8473
  throw new Error(`Cron job with code '${input.code}' was not found.`);
8492
8474
  }
8493
- await plugin.executeJob(server, job);
8475
+ await cronJobService.executeJob(job);
8494
8476
  response.json({});
8495
8477
  }
8496
8478
 
@@ -8518,8 +8500,90 @@ var runCronJob = {
8518
8500
 
8519
8501
  var pluginRoutes$1 = [runCronJob];
8520
8502
 
8503
+ class CronJobService {
8504
+ #server;
8505
+ #jobInstances;
8506
+ constructor(server) {
8507
+ this.#server = server;
8508
+ }
8509
+ /**
8510
+ * 根据编号获取定时任务配置信息
8511
+ * @param code
8512
+ * @returns
8513
+ */
8514
+ getJobConfigurationByCode(code) {
8515
+ return lodash.find(this.#server.listCronJobs(), (job) => job.code === code);
8516
+ }
8517
+ /**
8518
+ * 重新加载定时任务
8519
+ */
8520
+ reloadJobs() {
8521
+ const server = this.#server;
8522
+ if (this.#jobInstances) {
8523
+ for (const job of this.#jobInstances) {
8524
+ job.instance.stop();
8525
+ }
8526
+ }
8527
+ this.#jobInstances = [];
8528
+ const cronJobs = server.listCronJobs();
8529
+ for (const job of cronJobs) {
8530
+ if (job.disabled) {
8531
+ continue;
8532
+ }
8533
+ const jobInstance = cron.CronJob.from({
8534
+ ...(job.jobOptions || {}),
8535
+ cronTime: job.cronTime,
8536
+ onTick: async () => {
8537
+ await this.tryExecuteJob(server, job);
8538
+ },
8539
+ });
8540
+ jobInstance.start();
8541
+ this.#jobInstances.push({
8542
+ code: job.code,
8543
+ instance: jobInstance,
8544
+ });
8545
+ }
8546
+ }
8547
+ async tryExecuteJob(server, job) {
8548
+ const logger = server.getLogger();
8549
+ logger.info(`Executing cron job '${job.code}'...`);
8550
+ try {
8551
+ await this.executeJob(job);
8552
+ logger.info(`Completed cron job '${job.code}'...`);
8553
+ }
8554
+ catch (ex) {
8555
+ logger.error('Cron job "%s" execution error: %s', job.code, ex.message, { cronJobCode: job.code });
8556
+ }
8557
+ }
8558
+ /**
8559
+ * 执行指定任务
8560
+ * @param job
8561
+ */
8562
+ async executeJob(job) {
8563
+ const server = this.#server;
8564
+ const logger = server.getLogger();
8565
+ validateLicense(server);
8566
+ let handlerContext = {
8567
+ logger,
8568
+ routerContext: RouteContext.newSystemOperationContext(server),
8569
+ next: null,
8570
+ server,
8571
+ applicationConfig: null,
8572
+ input: null,
8573
+ };
8574
+ if (job.actionHandlerCode) {
8575
+ const actionHandler = server.getActionHandlerByCode(job.code);
8576
+ await actionHandler(handlerContext, job.handleOptions);
8577
+ }
8578
+ else {
8579
+ await job.handler(handlerContext, job.handleOptions);
8580
+ }
8581
+ }
8582
+ }
8583
+
8521
8584
  class CronJobPlugin {
8522
8585
  #server;
8586
+ #cronJobService;
8523
8587
  constructor() { }
8524
8588
  get code() {
8525
8589
  return "cronJob";
@@ -8551,50 +8615,16 @@ class CronJobPlugin {
8551
8615
  async onLoadingApplication(server, applicationConfig) { }
8552
8616
  async configureModels(server, applicationConfig) { }
8553
8617
  async configureModelProperties(server, applicationConfig) { }
8618
+ async configureServices(server, applicationConfig) {
8619
+ this.#cronJobService = new CronJobService(server);
8620
+ server.registerService("cronJobService", this.#cronJobService);
8621
+ }
8554
8622
  async configureRoutes(server, applicationConfig) {
8555
8623
  server.appendApplicationConfig({ routes: pluginRoutes$1 });
8556
8624
  }
8557
8625
  async onApplicationLoaded(server, applicationConfig) { }
8558
8626
  async onApplicationReady(server, applicationConfig) {
8559
- const cronJobs = server.listCronJobs();
8560
- for (const job of cronJobs) {
8561
- const jobInstance = cron__namespace.CronJob.from({
8562
- ...(job.jobOptions || {}),
8563
- cronTime: job.cronTime,
8564
- onTick: async () => {
8565
- server.getLogger().info(`Executing cron job '${job.code}'...`);
8566
- await this.executeJob(server, job);
8567
- },
8568
- });
8569
- jobInstance.start();
8570
- }
8571
- }
8572
- getJobConfigurationByCode(code) {
8573
- return lodash.find(this.#server.listCronJobs(), (job) => job.code === code);
8574
- }
8575
- async executeJob(server, job) {
8576
- const logger = server.getLogger();
8577
- try {
8578
- validateLicense(server);
8579
- let handlerContext = {
8580
- logger,
8581
- routerContext: RouteContext.newSystemOperationContext(server),
8582
- next: null,
8583
- server,
8584
- applicationConfig: null,
8585
- input: null,
8586
- };
8587
- if (job.actionHandlerCode) {
8588
- const actionHandler = server.getActionHandlerByCode(job.code);
8589
- await actionHandler(handlerContext, job.handleOptions);
8590
- }
8591
- else {
8592
- await job.handler(handlerContext, job.handleOptions);
8593
- }
8594
- }
8595
- catch (ex) {
8596
- logger.error('Cron job "%s" execution error: %s', job.code, ex.message, { cronJobCode: job.code });
8597
- }
8627
+ this.#cronJobService.reloadJobs();
8598
8628
  }
8599
8629
  }
8600
8630
 
@@ -1,5 +1,4 @@
1
1
  import type { RpdApplicationConfig } from "../../types";
2
- import { CronJobConfiguration } from "../../types/cron-job-types";
3
2
  import { IRpdServer, RapidPlugin, RpdConfigurationItemOptions, RpdServerPluginConfigurableTargetOptions, RpdServerPluginExtendingAbilities } from "../../core/server";
4
3
  declare class CronJobPlugin implements RapidPlugin {
5
4
  #private;
@@ -18,10 +17,9 @@ declare class CronJobPlugin implements RapidPlugin {
18
17
  onLoadingApplication(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
19
18
  configureModels(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
20
19
  configureModelProperties(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
20
+ configureServices(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
21
21
  configureRoutes(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
22
22
  onApplicationLoaded(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
23
23
  onApplicationReady(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any>;
24
- getJobConfigurationByCode(code: string): CronJobConfiguration;
25
- executeJob(server: IRpdServer, job: CronJobConfiguration): Promise<void>;
26
24
  }
27
25
  export default CronJobPlugin;
@@ -1,6 +1,11 @@
1
+ import { CronJob } from "cron";
1
2
  export type RunCronJobActionHandlerOptions = {
2
3
  code?: string;
3
4
  };
4
5
  export type RunCronJobInput = {
5
6
  code?: string;
6
7
  };
8
+ export type NamedCronJobInstance = {
9
+ code: string;
10
+ instance: CronJob;
11
+ };
@@ -0,0 +1,22 @@
1
+ import { IRpdServer } from "../../../core/server";
2
+ import { CronJobConfiguration } from "../../../types/cron-job-types";
3
+ export default class CronJobService {
4
+ #private;
5
+ constructor(server: IRpdServer);
6
+ /**
7
+ * 根据编号获取定时任务配置信息
8
+ * @param code
9
+ * @returns
10
+ */
11
+ getJobConfigurationByCode(code: string): CronJobConfiguration;
12
+ /**
13
+ * 重新加载定时任务
14
+ */
15
+ reloadJobs(): void;
16
+ tryExecuteJob(server: IRpdServer, job: CronJobConfiguration): Promise<void>;
17
+ /**
18
+ * 执行指定任务
19
+ * @param job
20
+ */
21
+ executeJob(job: CronJobConfiguration): Promise<void>;
22
+ }
@@ -8,6 +8,10 @@ export interface CronJobConfiguration {
8
8
  * 定时任务描述
9
9
  */
10
10
  description?: string;
11
+ /**
12
+ * 是否禁用
13
+ */
14
+ disabled?: boolean;
11
15
  /**
12
16
  * crontab 表达式
13
17
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruiapp/rapid-core",
3
- "version": "0.8.3",
3
+ "version": "0.8.5",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,8 +1,6 @@
1
- import * as cron from "cron";
2
1
  import type { RpdApplicationConfig } from "~/types";
3
2
  import pluginActionHandlers from "./actionHandlers";
4
3
  import pluginRoutes from "./routes";
5
- import { CronJobConfiguration } from "~/types/cron-job-types";
6
4
  import {
7
5
  IRpdServer,
8
6
  RapidPlugin,
@@ -10,13 +8,11 @@ import {
10
8
  RpdServerPluginConfigurableTargetOptions,
11
9
  RpdServerPluginExtendingAbilities,
12
10
  } from "~/core/server";
13
- import { ActionHandlerContext } from "~/core/actionHandler";
14
- import { find } from "lodash";
15
- import { validateLicense } from "~/helpers/licenseHelper";
16
- import { RouteContext } from "~/core/routeContext";
11
+ import CronJobService from "./services/CronJobService";
17
12
 
18
13
  class CronJobPlugin implements RapidPlugin {
19
14
  #server: IRpdServer;
15
+ #cronJobService!: CronJobService;
20
16
 
21
17
  constructor() {}
22
18
 
@@ -64,6 +60,11 @@ class CronJobPlugin implements RapidPlugin {
64
60
 
65
61
  async configureModelProperties(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {}
66
62
 
63
+ async configureServices(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
64
+ this.#cronJobService = new CronJobService(server);
65
+ server.registerService("cronJobService", this.#cronJobService);
66
+ }
67
+
67
68
  async configureRoutes(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
68
69
  server.appendApplicationConfig({ routes: pluginRoutes });
69
70
  }
@@ -71,47 +72,7 @@ class CronJobPlugin implements RapidPlugin {
71
72
  async onApplicationLoaded(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {}
72
73
 
73
74
  async onApplicationReady(server: IRpdServer, applicationConfig: RpdApplicationConfig): Promise<any> {
74
- const cronJobs = server.listCronJobs();
75
- for (const job of cronJobs) {
76
- const jobInstance = cron.CronJob.from({
77
- ...(job.jobOptions || {}),
78
- cronTime: job.cronTime,
79
- onTick: async () => {
80
- server.getLogger().info(`Executing cron job '${job.code}'...`);
81
- await this.executeJob(server, job);
82
- },
83
- });
84
- jobInstance.start();
85
- }
86
- }
87
-
88
- getJobConfigurationByCode(code: string) {
89
- return find(this.#server.listCronJobs(), (job) => job.code === code);
90
- }
91
-
92
- async executeJob(server: IRpdServer, job: CronJobConfiguration) {
93
- const logger = server.getLogger();
94
- try {
95
- validateLicense(server);
96
-
97
- let handlerContext: ActionHandlerContext = {
98
- logger,
99
- routerContext: RouteContext.newSystemOperationContext(server),
100
- next: null,
101
- server,
102
- applicationConfig: null,
103
- input: null,
104
- };
105
-
106
- if (job.actionHandlerCode) {
107
- const actionHandler = server.getActionHandlerByCode(job.code);
108
- await actionHandler(handlerContext, job.handleOptions);
109
- } else {
110
- await job.handler(handlerContext, job.handleOptions);
111
- }
112
- } catch (ex) {
113
- logger.error('Cron job "%s" execution error: %s', job.code, ex.message, { cronJobCode: job.code });
114
- }
75
+ this.#cronJobService.reloadJobs();
115
76
  }
116
77
  }
117
78
 
@@ -1,3 +1,5 @@
1
+ import { CronJob } from "cron";
2
+
1
3
  export type RunCronJobActionHandlerOptions = {
2
4
  code?: string;
3
5
  };
@@ -5,3 +7,8 @@ export type RunCronJobActionHandlerOptions = {
5
7
  export type RunCronJobInput = {
6
8
  code?: string;
7
9
  };
10
+
11
+ export type NamedCronJobInstance = {
12
+ code: string;
13
+ instance: CronJob;
14
+ };
@@ -1,6 +1,7 @@
1
1
  import { ActionHandlerContext } from "~/core/actionHandler";
2
2
  import { RunCronJobActionHandlerOptions, RunCronJobInput } from "../CronJobPluginTypes";
3
3
  import type CronJobPlugin from "../CronJobPlugin";
4
+ import CronJobService from "../services/CronJobService";
4
5
 
5
6
  export const code = "runCronJob";
6
7
 
@@ -18,12 +19,13 @@ export async function handler(plugin: CronJobPlugin, ctx: ActionHandlerContext,
18
19
  throw new Error(`Cron job code is required.`);
19
20
  }
20
21
 
21
- const job = plugin.getJobConfigurationByCode(input.code);
22
+ const cronJobService = server.getService<CronJobService>("cronJobService");
23
+ const job = cronJobService.getJobConfigurationByCode(input.code);
22
24
  if (!job) {
23
25
  throw new Error(`Cron job with code '${input.code}' was not found.`);
24
26
  }
25
27
 
26
- await plugin.executeJob(server, job);
28
+ await cronJobService.executeJob(job);
27
29
 
28
30
  response.json({});
29
31
  }
@@ -0,0 +1,100 @@
1
+ import { CronJob } from "cron";
2
+ import { IRpdServer } from "~/core/server";
3
+ import { find } from "lodash";
4
+ import { RouteContext } from "~/core/routeContext";
5
+ import { validateLicense } from "~/helpers/licenseHelper";
6
+ import { NamedCronJobInstance } from "../CronJobPluginTypes";
7
+ import { CronJobConfiguration } from "~/types/cron-job-types";
8
+ import { ActionHandlerContext } from "~/core/actionHandler";
9
+
10
+ export default class CronJobService {
11
+ #server: IRpdServer;
12
+ #jobInstances: NamedCronJobInstance[];
13
+
14
+ constructor(server: IRpdServer) {
15
+ this.#server = server;
16
+ }
17
+
18
+ /**
19
+ * 根据编号获取定时任务配置信息
20
+ * @param code
21
+ * @returns
22
+ */
23
+ getJobConfigurationByCode(code: string) {
24
+ return find(this.#server.listCronJobs(), (job) => job.code === code);
25
+ }
26
+
27
+ /**
28
+ * 重新加载定时任务
29
+ */
30
+ reloadJobs() {
31
+ const server = this.#server;
32
+
33
+ if (this.#jobInstances) {
34
+ for (const job of this.#jobInstances) {
35
+ job.instance.stop();
36
+ }
37
+ }
38
+
39
+ this.#jobInstances = [];
40
+
41
+ const cronJobs = server.listCronJobs();
42
+ for (const job of cronJobs) {
43
+ if (job.disabled) {
44
+ continue;
45
+ }
46
+
47
+ const jobInstance = CronJob.from({
48
+ ...(job.jobOptions || {}),
49
+ cronTime: job.cronTime,
50
+ onTick: async () => {
51
+ await this.tryExecuteJob(server, job);
52
+ },
53
+ });
54
+ jobInstance.start();
55
+
56
+ this.#jobInstances.push({
57
+ code: job.code,
58
+ instance: jobInstance,
59
+ });
60
+ }
61
+ }
62
+
63
+ async tryExecuteJob(server: IRpdServer, job: CronJobConfiguration) {
64
+ const logger = server.getLogger();
65
+ logger.info(`Executing cron job '${job.code}'...`);
66
+
67
+ try {
68
+ await this.executeJob(job);
69
+ logger.info(`Completed cron job '${job.code}'...`);
70
+ } catch (ex: any) {
71
+ logger.error('Cron job "%s" execution error: %s', job.code, ex.message, { cronJobCode: job.code });
72
+ }
73
+ }
74
+
75
+ /**
76
+ * 执行指定任务
77
+ * @param job
78
+ */
79
+ async executeJob(job: CronJobConfiguration) {
80
+ const server = this.#server;
81
+ const logger = server.getLogger();
82
+ validateLicense(server);
83
+
84
+ let handlerContext: ActionHandlerContext = {
85
+ logger,
86
+ routerContext: RouteContext.newSystemOperationContext(server),
87
+ next: null,
88
+ server,
89
+ applicationConfig: null,
90
+ input: null,
91
+ };
92
+
93
+ if (job.actionHandlerCode) {
94
+ const actionHandler = server.getActionHandlerByCode(job.code);
95
+ await actionHandler(handlerContext, job.handleOptions);
96
+ } else {
97
+ await job.handler(handlerContext, job.handleOptions);
98
+ }
99
+ }
100
+ }
@@ -11,6 +11,11 @@ export interface CronJobConfiguration {
11
11
  */
12
12
  description?: string;
13
13
 
14
+ /**
15
+ * 是否禁用
16
+ */
17
+ disabled?: boolean;
18
+
14
19
  /**
15
20
  * crontab 表达式
16
21
  */