@ruiapp/rapid-core 0.8.11 → 0.8.12

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
@@ -8676,29 +8676,22 @@ var pluginModels$1 = [CronJob];
8676
8676
 
8677
8677
  const code$1 = "runCronJob";
8678
8678
  async function handler$1(plugin, ctx, options) {
8679
- const { server, routerContext } = ctx;
8680
- const { response } = routerContext;
8679
+ const { server, routerContext: routeContext } = ctx;
8680
+ const { response } = routeContext;
8681
8681
  const input = ctx.input;
8682
- if (options.code) {
8682
+ if (options?.code) {
8683
8683
  input.code = options.code;
8684
8684
  }
8685
8685
  if (!input.code) {
8686
8686
  throw new Error(`Cron job code is required.`);
8687
8687
  }
8688
8688
  const cronJobService = server.getService("cronJobService");
8689
- const job = cronJobService.getJobConfigurationByCode(input.code);
8690
- if (!job) {
8689
+ const jobConfig = cronJobService.getJobConfigurationByCode(input.code);
8690
+ if (!jobConfig) {
8691
8691
  throw new Error(`Cron job with code '${input.code}' was not found.`);
8692
8692
  }
8693
- let jobExecutionContext = {
8694
- logger: server.getLogger(),
8695
- routerContext,
8696
- next: null,
8697
- server,
8698
- applicationConfig: null,
8699
- input: input.input,
8700
- };
8701
- await cronJobService.executeJob(jobExecutionContext, job);
8693
+ // running job in background.
8694
+ cronJobService.executeJob(jobConfig, input.input);
8702
8695
  response.json({});
8703
8696
  }
8704
8697
 
@@ -8766,7 +8759,7 @@ class CronJobService {
8766
8759
  ...(job.jobOptions || {}),
8767
8760
  cronTime: job.cronTime,
8768
8761
  onTick: async () => {
8769
- await this.tryExecuteJob(job);
8762
+ await this.executeJob(job);
8770
8763
  },
8771
8764
  });
8772
8765
  }
@@ -8839,7 +8832,12 @@ class CronJobService {
8839
8832
  });
8840
8833
  }
8841
8834
  }
8842
- async tryExecuteJob(job) {
8835
+ /**
8836
+ * 执行指定任务
8837
+ * @param job
8838
+ * @param input
8839
+ */
8840
+ async executeJob(job, input) {
8843
8841
  const server = this.#server;
8844
8842
  const logger = server.getLogger();
8845
8843
  const jobCode = job.code;
@@ -8850,13 +8848,20 @@ class CronJobService {
8850
8848
  next: null,
8851
8849
  server,
8852
8850
  applicationConfig: null,
8853
- input: null,
8851
+ input,
8854
8852
  };
8855
8853
  let result;
8856
8854
  let lastErrorMessage;
8857
8855
  let lastErrorStack;
8858
8856
  try {
8859
- await this.executeJob(handlerContext, job);
8857
+ validateLicense(server);
8858
+ if (job.actionHandlerCode) {
8859
+ const actionHandler = server.getActionHandlerByCode(job.code);
8860
+ await actionHandler(handlerContext, job.handleOptions);
8861
+ }
8862
+ else {
8863
+ await job.handler(handlerContext, job.handleOptions);
8864
+ }
8860
8865
  result = "success";
8861
8866
  logger.info(`Completed cron job '${jobCode}'...`);
8862
8867
  }
@@ -8879,41 +8884,31 @@ class CronJobService {
8879
8884
  }
8880
8885
  }
8881
8886
  }
8882
- const cronJobManager = server.getEntityManager("sys_cron_job");
8883
- const cronJobInDb = await cronJobManager.findEntity({
8884
- filters: [{ operator: "eq", field: "code", value: jobCode }],
8885
- });
8886
- if (cronJobInDb) {
8887
- let nextRunningTime;
8888
- const namedJobInstance = lodash.find(this.#namedJobInstances, { code: jobCode });
8889
- if (namedJobInstance && namedJobInstance.instance) {
8890
- nextRunningTime = formatDateTimeWithTimezone(namedJobInstance.instance.nextDate().toISO());
8891
- }
8892
- await cronJobManager.updateEntityById({
8893
- id: cronJobInDb.id,
8894
- entityToSave: {
8895
- nextRunningTime,
8896
- lastRunningResult: result,
8897
- lastRunningTime: getNowStringWithTimezone(),
8898
- lastErrorMessage,
8899
- lastErrorStack,
8900
- },
8887
+ try {
8888
+ const cronJobManager = server.getEntityManager("sys_cron_job");
8889
+ const cronJobInDb = await cronJobManager.findEntity({
8890
+ filters: [{ operator: "eq", field: "code", value: jobCode }],
8901
8891
  });
8892
+ if (cronJobInDb) {
8893
+ let nextRunningTime;
8894
+ const namedJobInstance = lodash.find(this.#namedJobInstances, { code: jobCode });
8895
+ if (namedJobInstance && namedJobInstance.instance) {
8896
+ nextRunningTime = formatDateTimeWithTimezone(namedJobInstance.instance.nextDate().toISO());
8897
+ }
8898
+ await cronJobManager.updateEntityById({
8899
+ id: cronJobInDb.id,
8900
+ entityToSave: {
8901
+ nextRunningTime,
8902
+ lastRunningResult: result,
8903
+ lastRunningTime: getNowStringWithTimezone(),
8904
+ lastErrorMessage,
8905
+ lastErrorStack,
8906
+ },
8907
+ });
8908
+ }
8902
8909
  }
8903
- }
8904
- /**
8905
- * 执行指定任务
8906
- * @param job
8907
- */
8908
- async executeJob(handlerContext, job) {
8909
- const server = this.#server;
8910
- validateLicense(server);
8911
- if (job.actionHandlerCode) {
8912
- const actionHandler = server.getActionHandlerByCode(job.code);
8913
- await actionHandler(handlerContext, job.handleOptions);
8914
- }
8915
- else {
8916
- await job.handler(handlerContext, job.handleOptions);
8910
+ catch (ex) {
8911
+ logger.error("Failed to saving cron job running result. job code: %s, error: %s", jobCode, ex.message);
8917
8912
  }
8918
8913
  }
8919
8914
  async updateJobConfig(routeContext, options) {
@@ -2,7 +2,6 @@ import { IRpdServer } from "../../../core/server";
2
2
  import { RouteContext } from "../../../core/routeContext";
3
3
  import { UpdateJobConfigOptions } from "../CronJobPluginTypes";
4
4
  import { CronJobConfiguration } from "../../../types/cron-job-types";
5
- import { ActionHandlerContext } from "../../../core/actionHandler";
6
5
  export default class CronJobService {
7
6
  #private;
8
7
  constructor(server: IRpdServer);
@@ -16,11 +15,11 @@ export default class CronJobService {
16
15
  * 重新加载定时任务
17
16
  */
18
17
  reloadJobs(): Promise<void>;
19
- tryExecuteJob(job: CronJobConfiguration): Promise<void>;
20
18
  /**
21
19
  * 执行指定任务
22
20
  * @param job
21
+ * @param input
23
22
  */
24
- executeJob(handlerContext: ActionHandlerContext, job: CronJobConfiguration): Promise<void>;
23
+ executeJob(job: CronJobConfiguration, input?: any): Promise<void>;
25
24
  updateJobConfig(routeContext: RouteContext, options: UpdateJobConfigOptions): Promise<void>;
26
25
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruiapp/rapid-core",
3
- "version": "0.8.11",
3
+ "version": "0.8.12",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -6,12 +6,12 @@ import CronJobService from "../services/CronJobService";
6
6
  export const code = "runCronJob";
7
7
 
8
8
  export async function handler(plugin: CronJobPlugin, ctx: ActionHandlerContext, options: RunCronJobActionHandlerOptions) {
9
- const { server, routerContext } = ctx;
10
- const { response } = routerContext;
9
+ const { server, routerContext: routeContext } = ctx;
10
+ const { response } = routeContext;
11
11
 
12
12
  const input: RunCronJobInput = ctx.input;
13
13
 
14
- if (options.code) {
14
+ if (options?.code) {
15
15
  input.code = options.code;
16
16
  }
17
17
 
@@ -20,20 +20,13 @@ export async function handler(plugin: CronJobPlugin, ctx: ActionHandlerContext,
20
20
  }
21
21
 
22
22
  const cronJobService = server.getService<CronJobService>("cronJobService");
23
- const job = cronJobService.getJobConfigurationByCode(input.code);
24
- if (!job) {
23
+ const jobConfig = cronJobService.getJobConfigurationByCode(input.code);
24
+ if (!jobConfig) {
25
25
  throw new Error(`Cron job with code '${input.code}' was not found.`);
26
26
  }
27
27
 
28
- let jobExecutionContext: ActionHandlerContext = {
29
- logger: server.getLogger(),
30
- routerContext,
31
- next: null,
32
- server,
33
- applicationConfig: null,
34
- input: input.input,
35
- };
36
- await cronJobService.executeJob(jobExecutionContext, job);
28
+ // running job in background.
29
+ cronJobService.executeJob(jobConfig, input.input);
37
30
 
38
31
  response.json({});
39
32
  }
@@ -30,7 +30,7 @@ export default class CronJobService {
30
30
  ...(job.jobOptions || {}),
31
31
  cronTime: job.cronTime,
32
32
  onTick: async () => {
33
- await this.tryExecuteJob(job);
33
+ await this.executeJob(job);
34
34
  },
35
35
  });
36
36
  }
@@ -117,7 +117,12 @@ export default class CronJobService {
117
117
  }
118
118
  }
119
119
 
120
- async tryExecuteJob(job: CronJobConfiguration) {
120
+ /**
121
+ * 执行指定任务
122
+ * @param job
123
+ * @param input
124
+ */
125
+ async executeJob(job: CronJobConfiguration, input?: any) {
121
126
  const server = this.#server;
122
127
  const logger = server.getLogger();
123
128
 
@@ -130,14 +135,21 @@ export default class CronJobService {
130
135
  next: null,
131
136
  server,
132
137
  applicationConfig: null,
133
- input: null,
138
+ input,
134
139
  };
135
140
 
136
141
  let result: JobRunningResult;
137
142
  let lastErrorMessage: string | null;
138
143
  let lastErrorStack: string | null;
139
144
  try {
140
- await this.executeJob(handlerContext, job);
145
+ validateLicense(server);
146
+
147
+ if (job.actionHandlerCode) {
148
+ const actionHandler = server.getActionHandlerByCode(job.code);
149
+ await actionHandler(handlerContext, job.handleOptions);
150
+ } else {
151
+ await job.handler(handlerContext, job.handleOptions);
152
+ }
141
153
  result = "success";
142
154
  logger.info(`Completed cron job '${jobCode}'...`);
143
155
  } catch (ex: any) {
@@ -159,44 +171,32 @@ export default class CronJobService {
159
171
  }
160
172
  }
161
173
 
162
- const cronJobManager = server.getEntityManager<SysCronJob>("sys_cron_job");
163
- const cronJobInDb = await cronJobManager.findEntity({
164
- filters: [{ operator: "eq", field: "code", value: jobCode }],
165
- });
166
-
167
- if (cronJobInDb) {
168
- let nextRunningTime: string | null;
169
- const namedJobInstance = find(this.#namedJobInstances, { code: jobCode });
170
- if (namedJobInstance && namedJobInstance.instance) {
171
- nextRunningTime = formatDateTimeWithTimezone(namedJobInstance.instance.nextDate().toISO());
172
- }
173
-
174
- await cronJobManager.updateEntityById({
175
- id: cronJobInDb.id,
176
- entityToSave: {
177
- nextRunningTime,
178
- lastRunningResult: result,
179
- lastRunningTime: getNowStringWithTimezone(),
180
- lastErrorMessage,
181
- lastErrorStack,
182
- } as Partial<SysCronJob>,
174
+ try {
175
+ const cronJobManager = server.getEntityManager<SysCronJob>("sys_cron_job");
176
+ const cronJobInDb = await cronJobManager.findEntity({
177
+ filters: [{ operator: "eq", field: "code", value: jobCode }],
183
178
  });
184
- }
185
- }
186
179
 
187
- /**
188
- * 执行指定任务
189
- * @param job
190
- */
191
- async executeJob(handlerContext: ActionHandlerContext, job: CronJobConfiguration) {
192
- const server = this.#server;
193
- validateLicense(server);
180
+ if (cronJobInDb) {
181
+ let nextRunningTime: string | null;
182
+ const namedJobInstance = find(this.#namedJobInstances, { code: jobCode });
183
+ if (namedJobInstance && namedJobInstance.instance) {
184
+ nextRunningTime = formatDateTimeWithTimezone(namedJobInstance.instance.nextDate().toISO());
185
+ }
194
186
 
195
- if (job.actionHandlerCode) {
196
- const actionHandler = server.getActionHandlerByCode(job.code);
197
- await actionHandler(handlerContext, job.handleOptions);
198
- } else {
199
- await job.handler(handlerContext, job.handleOptions);
187
+ await cronJobManager.updateEntityById({
188
+ id: cronJobInDb.id,
189
+ entityToSave: {
190
+ nextRunningTime,
191
+ lastRunningResult: result,
192
+ lastRunningTime: getNowStringWithTimezone(),
193
+ lastErrorMessage,
194
+ lastErrorStack,
195
+ } as Partial<SysCronJob>,
196
+ });
197
+ }
198
+ } catch (ex: any) {
199
+ logger.error("Failed to saving cron job running result. job code: %s, error: %s", jobCode, ex.message);
200
200
  }
201
201
  }
202
202