@ruiapp/rapid-core 0.8.4 → 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,47 +8500,37 @@ var runCronJob = {
8518
8500
 
8519
8501
  var pluginRoutes$1 = [runCronJob];
8520
8502
 
8521
- class CronJobPlugin {
8503
+ class CronJobService {
8522
8504
  #server;
8523
- constructor() { }
8524
- get code() {
8525
- return "cronJob";
8526
- }
8527
- get description() {
8528
- return "";
8529
- }
8530
- get extendingAbilities() {
8531
- return [];
8532
- }
8533
- get configurableTargets() {
8534
- return [];
8535
- }
8536
- get configurations() {
8537
- return [];
8538
- }
8539
- async initPlugin(server) {
8505
+ #jobInstances;
8506
+ constructor(server) {
8540
8507
  this.#server = server;
8541
8508
  }
8542
- async registerMiddlewares(server) { }
8543
- async registerActionHandlers(server) {
8544
- for (const actionHandler of pluginActionHandlers$1) {
8545
- server.registerActionHandler(this, actionHandler);
8546
- }
8547
- }
8548
- async registerEventHandlers(server) { }
8549
- async registerMessageHandlers(server) { }
8550
- async registerTaskProcessors(server) { }
8551
- async onLoadingApplication(server, applicationConfig) { }
8552
- async configureModels(server, applicationConfig) { }
8553
- async configureModelProperties(server, applicationConfig) { }
8554
- async configureRoutes(server, applicationConfig) {
8555
- server.appendApplicationConfig({ routes: pluginRoutes$1 });
8509
+ /**
8510
+ * 根据编号获取定时任务配置信息
8511
+ * @param code
8512
+ * @returns
8513
+ */
8514
+ getJobConfigurationByCode(code) {
8515
+ return lodash.find(this.#server.listCronJobs(), (job) => job.code === code);
8556
8516
  }
8557
- async onApplicationLoaded(server, applicationConfig) { }
8558
- async onApplicationReady(server, applicationConfig) {
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 = [];
8559
8528
  const cronJobs = server.listCronJobs();
8560
8529
  for (const job of cronJobs) {
8561
- const jobInstance = cron__namespace.CronJob.from({
8530
+ if (job.disabled) {
8531
+ continue;
8532
+ }
8533
+ const jobInstance = cron.CronJob.from({
8562
8534
  ...(job.jobOptions || {}),
8563
8535
  cronTime: job.cronTime,
8564
8536
  onTick: async () => {
@@ -8566,23 +8538,29 @@ class CronJobPlugin {
8566
8538
  },
8567
8539
  });
8568
8540
  jobInstance.start();
8541
+ this.#jobInstances.push({
8542
+ code: job.code,
8543
+ instance: jobInstance,
8544
+ });
8569
8545
  }
8570
8546
  }
8571
- getJobConfigurationByCode(code) {
8572
- return lodash.find(this.#server.listCronJobs(), (job) => job.code === code);
8573
- }
8574
8547
  async tryExecuteJob(server, job) {
8575
8548
  const logger = server.getLogger();
8576
8549
  logger.info(`Executing cron job '${job.code}'...`);
8577
8550
  try {
8578
- await this.executeJob(server, job);
8551
+ await this.executeJob(job);
8579
8552
  logger.info(`Completed cron job '${job.code}'...`);
8580
8553
  }
8581
8554
  catch (ex) {
8582
8555
  logger.error('Cron job "%s" execution error: %s', job.code, ex.message, { cronJobCode: job.code });
8583
8556
  }
8584
8557
  }
8585
- async executeJob(server, job) {
8558
+ /**
8559
+ * 执行指定任务
8560
+ * @param job
8561
+ */
8562
+ async executeJob(job) {
8563
+ const server = this.#server;
8586
8564
  const logger = server.getLogger();
8587
8565
  validateLicense(server);
8588
8566
  let handlerContext = {
@@ -8603,6 +8581,53 @@ class CronJobPlugin {
8603
8581
  }
8604
8582
  }
8605
8583
 
8584
+ class CronJobPlugin {
8585
+ #server;
8586
+ #cronJobService;
8587
+ constructor() { }
8588
+ get code() {
8589
+ return "cronJob";
8590
+ }
8591
+ get description() {
8592
+ return "";
8593
+ }
8594
+ get extendingAbilities() {
8595
+ return [];
8596
+ }
8597
+ get configurableTargets() {
8598
+ return [];
8599
+ }
8600
+ get configurations() {
8601
+ return [];
8602
+ }
8603
+ async initPlugin(server) {
8604
+ this.#server = server;
8605
+ }
8606
+ async registerMiddlewares(server) { }
8607
+ async registerActionHandlers(server) {
8608
+ for (const actionHandler of pluginActionHandlers$1) {
8609
+ server.registerActionHandler(this, actionHandler);
8610
+ }
8611
+ }
8612
+ async registerEventHandlers(server) { }
8613
+ async registerMessageHandlers(server) { }
8614
+ async registerTaskProcessors(server) { }
8615
+ async onLoadingApplication(server, applicationConfig) { }
8616
+ async configureModels(server, applicationConfig) { }
8617
+ async configureModelProperties(server, applicationConfig) { }
8618
+ async configureServices(server, applicationConfig) {
8619
+ this.#cronJobService = new CronJobService(server);
8620
+ server.registerService("cronJobService", this.#cronJobService);
8621
+ }
8622
+ async configureRoutes(server, applicationConfig) {
8623
+ server.appendApplicationConfig({ routes: pluginRoutes$1 });
8624
+ }
8625
+ async onApplicationLoaded(server, applicationConfig) { }
8626
+ async onApplicationReady(server, applicationConfig) {
8627
+ this.#cronJobService.reloadJobs();
8628
+ }
8629
+ }
8630
+
8606
8631
  async function getStateMachineNextSnapshot(options) {
8607
8632
  const { machineConfig, currentState, event } = options;
8608
8633
  machineConfig.initial = currentState;
@@ -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,11 +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
- tryExecuteJob(server: IRpdServer, job: CronJobConfiguration): Promise<void>;
26
- executeJob(server: IRpdServer, job: CronJobConfiguration): Promise<void>;
27
24
  }
28
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.4",
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,54 +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
- await this.tryExecuteJob(server, job);
81
- },
82
- });
83
- jobInstance.start();
84
- }
85
- }
86
-
87
- getJobConfigurationByCode(code: string) {
88
- return find(this.#server.listCronJobs(), (job) => job.code === code);
89
- }
90
-
91
- async tryExecuteJob(server: IRpdServer, job: CronJobConfiguration) {
92
- const logger = server.getLogger();
93
- logger.info(`Executing cron job '${job.code}'...`);
94
-
95
- try {
96
- await this.executeJob(server, job);
97
- logger.info(`Completed cron job '${job.code}'...`);
98
- } catch (ex: any) {
99
- logger.error('Cron job "%s" execution error: %s', job.code, ex.message, { cronJobCode: job.code });
100
- }
101
- }
102
-
103
- async executeJob(server: IRpdServer, job: CronJobConfiguration) {
104
- const logger = server.getLogger();
105
- validateLicense(server);
106
-
107
- let handlerContext: ActionHandlerContext = {
108
- logger,
109
- routerContext: RouteContext.newSystemOperationContext(server),
110
- next: null,
111
- server,
112
- applicationConfig: null,
113
- input: null,
114
- };
115
-
116
- if (job.actionHandlerCode) {
117
- const actionHandler = server.getActionHandlerByCode(job.code);
118
- await actionHandler(handlerContext, job.handleOptions);
119
- } else {
120
- await job.handler(handlerContext, job.handleOptions);
121
- }
75
+ this.#cronJobService.reloadJobs();
122
76
  }
123
77
  }
124
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
  */