@ruiapp/rapid-core 0.8.11 → 0.8.13
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 +50 -53
- package/dist/plugins/cronJob/services/CronJobService.d.ts +2 -3
- package/dist/server.d.ts +1 -1
- package/package.json +1 -1
- package/src/plugins/cronJob/actionHandlers/runCronJob.ts +7 -14
- package/src/plugins/cronJob/services/CronJobService.ts +39 -39
- package/src/server.ts +4 -2
package/dist/index.js
CHANGED
|
@@ -4658,12 +4658,14 @@ class RapidServer {
|
|
|
4658
4658
|
throw err;
|
|
4659
4659
|
}
|
|
4660
4660
|
}
|
|
4661
|
-
async tryQueryDatabaseObject(sql, params, client) {
|
|
4661
|
+
async tryQueryDatabaseObject(sql, params, client, silent) {
|
|
4662
4662
|
try {
|
|
4663
4663
|
return await this.queryDatabaseObject(sql, params, client);
|
|
4664
4664
|
}
|
|
4665
4665
|
catch (err) {
|
|
4666
|
-
|
|
4666
|
+
if (!silent) {
|
|
4667
|
+
this.#logger.error("Failed to query database object.", { errorMessage: err.message, sql, params });
|
|
4668
|
+
}
|
|
4667
4669
|
}
|
|
4668
4670
|
return [];
|
|
4669
4671
|
}
|
|
@@ -8676,29 +8678,22 @@ var pluginModels$1 = [CronJob];
|
|
|
8676
8678
|
|
|
8677
8679
|
const code$1 = "runCronJob";
|
|
8678
8680
|
async function handler$1(plugin, ctx, options) {
|
|
8679
|
-
const { server, routerContext } = ctx;
|
|
8680
|
-
const { response } =
|
|
8681
|
+
const { server, routerContext: routeContext } = ctx;
|
|
8682
|
+
const { response } = routeContext;
|
|
8681
8683
|
const input = ctx.input;
|
|
8682
|
-
if (options
|
|
8684
|
+
if (options?.code) {
|
|
8683
8685
|
input.code = options.code;
|
|
8684
8686
|
}
|
|
8685
8687
|
if (!input.code) {
|
|
8686
8688
|
throw new Error(`Cron job code is required.`);
|
|
8687
8689
|
}
|
|
8688
8690
|
const cronJobService = server.getService("cronJobService");
|
|
8689
|
-
const
|
|
8690
|
-
if (!
|
|
8691
|
+
const jobConfig = cronJobService.getJobConfigurationByCode(input.code);
|
|
8692
|
+
if (!jobConfig) {
|
|
8691
8693
|
throw new Error(`Cron job with code '${input.code}' was not found.`);
|
|
8692
8694
|
}
|
|
8693
|
-
|
|
8694
|
-
|
|
8695
|
-
routerContext,
|
|
8696
|
-
next: null,
|
|
8697
|
-
server,
|
|
8698
|
-
applicationConfig: null,
|
|
8699
|
-
input: input.input,
|
|
8700
|
-
};
|
|
8701
|
-
await cronJobService.executeJob(jobExecutionContext, job);
|
|
8695
|
+
// running job in background.
|
|
8696
|
+
cronJobService.executeJob(jobConfig, input.input);
|
|
8702
8697
|
response.json({});
|
|
8703
8698
|
}
|
|
8704
8699
|
|
|
@@ -8766,7 +8761,7 @@ class CronJobService {
|
|
|
8766
8761
|
...(job.jobOptions || {}),
|
|
8767
8762
|
cronTime: job.cronTime,
|
|
8768
8763
|
onTick: async () => {
|
|
8769
|
-
await this.
|
|
8764
|
+
await this.executeJob(job);
|
|
8770
8765
|
},
|
|
8771
8766
|
});
|
|
8772
8767
|
}
|
|
@@ -8839,7 +8834,12 @@ class CronJobService {
|
|
|
8839
8834
|
});
|
|
8840
8835
|
}
|
|
8841
8836
|
}
|
|
8842
|
-
|
|
8837
|
+
/**
|
|
8838
|
+
* 执行指定任务
|
|
8839
|
+
* @param job
|
|
8840
|
+
* @param input
|
|
8841
|
+
*/
|
|
8842
|
+
async executeJob(job, input) {
|
|
8843
8843
|
const server = this.#server;
|
|
8844
8844
|
const logger = server.getLogger();
|
|
8845
8845
|
const jobCode = job.code;
|
|
@@ -8850,13 +8850,20 @@ class CronJobService {
|
|
|
8850
8850
|
next: null,
|
|
8851
8851
|
server,
|
|
8852
8852
|
applicationConfig: null,
|
|
8853
|
-
input
|
|
8853
|
+
input,
|
|
8854
8854
|
};
|
|
8855
8855
|
let result;
|
|
8856
8856
|
let lastErrorMessage;
|
|
8857
8857
|
let lastErrorStack;
|
|
8858
8858
|
try {
|
|
8859
|
-
|
|
8859
|
+
validateLicense(server);
|
|
8860
|
+
if (job.actionHandlerCode) {
|
|
8861
|
+
const actionHandler = server.getActionHandlerByCode(job.code);
|
|
8862
|
+
await actionHandler(handlerContext, job.handleOptions);
|
|
8863
|
+
}
|
|
8864
|
+
else {
|
|
8865
|
+
await job.handler(handlerContext, job.handleOptions);
|
|
8866
|
+
}
|
|
8860
8867
|
result = "success";
|
|
8861
8868
|
logger.info(`Completed cron job '${jobCode}'...`);
|
|
8862
8869
|
}
|
|
@@ -8879,41 +8886,31 @@ class CronJobService {
|
|
|
8879
8886
|
}
|
|
8880
8887
|
}
|
|
8881
8888
|
}
|
|
8882
|
-
|
|
8883
|
-
|
|
8884
|
-
|
|
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
|
-
},
|
|
8889
|
+
try {
|
|
8890
|
+
const cronJobManager = server.getEntityManager("sys_cron_job");
|
|
8891
|
+
const cronJobInDb = await cronJobManager.findEntity({
|
|
8892
|
+
filters: [{ operator: "eq", field: "code", value: jobCode }],
|
|
8901
8893
|
});
|
|
8894
|
+
if (cronJobInDb) {
|
|
8895
|
+
let nextRunningTime;
|
|
8896
|
+
const namedJobInstance = lodash.find(this.#namedJobInstances, { code: jobCode });
|
|
8897
|
+
if (namedJobInstance && namedJobInstance.instance) {
|
|
8898
|
+
nextRunningTime = formatDateTimeWithTimezone(namedJobInstance.instance.nextDate().toISO());
|
|
8899
|
+
}
|
|
8900
|
+
await cronJobManager.updateEntityById({
|
|
8901
|
+
id: cronJobInDb.id,
|
|
8902
|
+
entityToSave: {
|
|
8903
|
+
nextRunningTime,
|
|
8904
|
+
lastRunningResult: result,
|
|
8905
|
+
lastRunningTime: getNowStringWithTimezone(),
|
|
8906
|
+
lastErrorMessage,
|
|
8907
|
+
lastErrorStack,
|
|
8908
|
+
},
|
|
8909
|
+
});
|
|
8910
|
+
}
|
|
8902
8911
|
}
|
|
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);
|
|
8912
|
+
catch (ex) {
|
|
8913
|
+
logger.error("Failed to saving cron job running result. job code: %s, error: %s", jobCode, ex.message);
|
|
8917
8914
|
}
|
|
8918
8915
|
}
|
|
8919
8916
|
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(
|
|
23
|
+
executeJob(job: CronJobConfiguration, input?: any): Promise<void>;
|
|
25
24
|
updateJobConfig(routeContext: RouteContext, options: UpdateJobConfigOptions): Promise<void>;
|
|
26
25
|
}
|
package/dist/server.d.ts
CHANGED
|
@@ -53,7 +53,7 @@ export declare class RapidServer implements IRpdServer {
|
|
|
53
53
|
registerFacilityFactory(factory: FacilityFactory): void;
|
|
54
54
|
getFacility<TFacility = any, TOptions = any>(name: string, options?: TOptions, nullIfUnknownFacility?: boolean): Promise<TFacility>;
|
|
55
55
|
queryDatabaseObject(sql: string, params?: unknown[] | Record<string, unknown>, client?: IDatabaseClient): Promise<any[]>;
|
|
56
|
-
tryQueryDatabaseObject(sql: string, params?: unknown[] | Record<string, unknown>, client?: IDatabaseClient): Promise<any[]>;
|
|
56
|
+
tryQueryDatabaseObject(sql: string, params?: unknown[] | Record<string, unknown>, client?: IDatabaseClient, silent?: boolean): Promise<any[]>;
|
|
57
57
|
get middlewares(): any[];
|
|
58
58
|
handleRequest(request: Request, next: Next): Promise<Response>;
|
|
59
59
|
beforeRunRouteActions(handlerContext: ActionHandlerContext): Promise<void>;
|
package/package.json
CHANGED
|
@@ -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 } =
|
|
9
|
+
const { server, routerContext: routeContext } = ctx;
|
|
10
|
+
const { response } = routeContext;
|
|
11
11
|
|
|
12
12
|
const input: RunCronJobInput = ctx.input;
|
|
13
13
|
|
|
14
|
-
if (options
|
|
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
|
|
24
|
-
if (!
|
|
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
|
-
|
|
29
|
-
|
|
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.
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
163
|
-
|
|
164
|
-
|
|
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
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
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
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
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
|
|
package/src/server.ts
CHANGED
|
@@ -419,11 +419,13 @@ export class RapidServer implements IRpdServer {
|
|
|
419
419
|
}
|
|
420
420
|
}
|
|
421
421
|
|
|
422
|
-
async tryQueryDatabaseObject(sql: string, params?: unknown[] | Record<string, unknown>, client?: IDatabaseClient): Promise<any[]> {
|
|
422
|
+
async tryQueryDatabaseObject(sql: string, params?: unknown[] | Record<string, unknown>, client?: IDatabaseClient, silent?: boolean): Promise<any[]> {
|
|
423
423
|
try {
|
|
424
424
|
return await this.queryDatabaseObject(sql, params, client);
|
|
425
425
|
} catch (err) {
|
|
426
|
-
|
|
426
|
+
if (!silent) {
|
|
427
|
+
this.#logger.error("Failed to query database object.", { errorMessage: err.message, sql, params });
|
|
428
|
+
}
|
|
427
429
|
}
|
|
428
430
|
|
|
429
431
|
return [];
|