@ruiapp/rapid-core 0.11.5 → 0.11.7
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/CHANGELOG.md +11 -11
- package/dist/core/pluginManager.d.ts +2 -0
- package/dist/core/server.d.ts +3 -0
- package/dist/dataAccess/entityManager.d.ts +3 -3
- package/dist/index.js +118 -41
- package/dist/server.d.ts +1 -0
- package/dist/types.d.ts +2 -0
- package/package.json +1 -1
- package/rollup.config.js +16 -16
- package/src/bootstrapApplicationConfig.ts +904 -904
- package/src/core/ExpressionInterpreter.ts +45 -45
- package/src/core/actionHandler.ts +24 -24
- package/src/core/actionHandlers/ifActionHandler.ts +27 -27
- package/src/core/eventManager.ts +20 -20
- package/src/core/facility.ts +7 -7
- package/src/core/http/formDataParser.ts +87 -87
- package/src/core/http-types.ts +4 -4
- package/src/core/pluginManager.ts +202 -193
- package/src/core/providers/runtimeProvider.ts +5 -5
- package/src/core/request.ts +96 -96
- package/src/core/response.ts +84 -84
- package/src/core/routeContext.ts +127 -127
- package/src/core/routes/healthz.ts +20 -20
- package/src/core/routes/index.ts +3 -3
- package/src/core/routesBuilder.ts +129 -122
- package/src/core/server.ts +161 -158
- package/src/dataAccess/columnTypeMapper.ts +22 -22
- package/src/dataAccess/dataAccessTypes.ts +165 -165
- package/src/dataAccess/dataAccessor.ts +135 -135
- package/src/dataAccess/entityManager.ts +2031 -1986
- package/src/dataAccess/entityMapper.ts +111 -111
- package/src/dataAccess/entityValidator.ts +33 -33
- package/src/dataAccess/propertyMapper.ts +28 -28
- package/src/deno-std/assert/assert.ts +9 -9
- package/src/deno-std/assert/assertion_error.ts +7 -7
- package/src/deno-std/datetime/to_imf.ts +32 -32
- package/src/deno-std/encoding/base64.ts +141 -141
- package/src/deno-std/http/cookie.ts +372 -372
- package/src/facilities/cache/CacheFacilityTypes.ts +29 -29
- package/src/facilities/cache/CacheFactory.ts +31 -31
- package/src/facilities/cache/MemoryCache.ts +58 -58
- package/src/facilities/cache/MemoryCacheProvider.ts +15 -15
- package/src/facilities/log/LogFacility.ts +35 -35
- package/src/helpers/dbTransactionHelper.ts +42 -42
- package/src/helpers/entityHelper.ts +89 -89
- package/src/helpers/filterHelper.ts +148 -148
- package/src/helpers/inputHelper.ts +11 -11
- package/src/helpers/licenseHelper.ts +29 -29
- package/src/helpers/metaHelper.ts +111 -111
- package/src/helpers/runCollectionEntityActionHandler.ts +58 -58
- package/src/index.ts +85 -85
- package/src/plugins/auth/AuthPlugin.ts +107 -107
- package/src/plugins/auth/AuthPluginTypes.ts +11 -11
- package/src/plugins/auth/actionHandlers/changePassword.ts +61 -61
- package/src/plugins/auth/actionHandlers/createSession.ts +68 -68
- package/src/plugins/auth/actionHandlers/deleteSession.ts +18 -18
- package/src/plugins/auth/actionHandlers/getMyProfile.ts +28 -28
- package/src/plugins/auth/actionHandlers/index.ts +8 -8
- package/src/plugins/auth/actionHandlers/resetPassword.ts +45 -45
- package/src/plugins/auth/models/AccessToken.ts +56 -56
- package/src/plugins/auth/models/index.ts +3 -3
- package/src/plugins/auth/routes/changePassword.ts +15 -15
- package/src/plugins/auth/routes/getMyProfile.ts +15 -15
- package/src/plugins/auth/routes/index.ts +7 -7
- package/src/plugins/auth/routes/resetPassword.ts +15 -15
- package/src/plugins/auth/routes/signin.ts +15 -15
- package/src/plugins/auth/routes/signout.ts +15 -15
- package/src/plugins/auth/services/AuthService.ts +66 -66
- package/src/plugins/cronJob/CronJobPlugin.ts +104 -104
- package/src/plugins/cronJob/CronJobPluginTypes.ts +44 -44
- package/src/plugins/cronJob/actionHandlers/index.ts +4 -4
- package/src/plugins/cronJob/actionHandlers/runCronJob.ts +32 -32
- package/src/plugins/cronJob/entityWatchers/cronJobEntityWatchers.ts +24 -24
- package/src/plugins/cronJob/entityWatchers/index.ts +4 -4
- package/src/plugins/cronJob/models/CronJob.ts +129 -129
- package/src/plugins/cronJob/models/index.ts +3 -3
- package/src/plugins/cronJob/routes/index.ts +3 -3
- package/src/plugins/cronJob/routes/runCronJob.ts +15 -15
- package/src/plugins/cronJob/services/CronJobService.ts +255 -255
- package/src/plugins/dataManage/DataManagePlugin.ts +165 -165
- package/src/plugins/dataManage/actionHandlers/addEntityRelations.ts +15 -15
- package/src/plugins/dataManage/actionHandlers/countCollectionEntities.ts +17 -17
- package/src/plugins/dataManage/actionHandlers/createCollectionEntitiesBatch.ts +81 -81
- package/src/plugins/dataManage/actionHandlers/createCollectionEntity.ts +20 -20
- package/src/plugins/dataManage/actionHandlers/deleteCollectionEntities.ts +47 -45
- package/src/plugins/dataManage/actionHandlers/deleteCollectionEntityById.ts +20 -20
- package/src/plugins/dataManage/actionHandlers/findCollectionEntities.ts +27 -27
- package/src/plugins/dataManage/actionHandlers/findCollectionEntityById.ts +30 -30
- package/src/plugins/dataManage/actionHandlers/queryDatabase.ts +22 -22
- package/src/plugins/dataManage/actionHandlers/removeEntityRelations.ts +15 -15
- package/src/plugins/dataManage/actionHandlers/saveEntity.ts +46 -46
- package/src/plugins/dataManage/actionHandlers/updateCollectionEntityById.ts +38 -38
- package/src/plugins/entityAccessControl/EntityAccessControlPlugin.ts +146 -146
- package/src/plugins/fileManage/FileManagePlugin.ts +52 -52
- package/src/plugins/fileManage/actionHandlers/downloadDocument.ts +65 -65
- package/src/plugins/fileManage/actionHandlers/downloadFile.ts +44 -44
- package/src/plugins/fileManage/actionHandlers/uploadFile.ts +33 -33
- package/src/plugins/fileManage/routes/downloadDocument.ts +15 -15
- package/src/plugins/fileManage/routes/downloadFile.ts +15 -15
- package/src/plugins/fileManage/routes/index.ts +5 -5
- package/src/plugins/fileManage/routes/uploadFile.ts +15 -15
- package/src/plugins/license/LicensePlugin.ts +79 -79
- package/src/plugins/license/LicensePluginTypes.ts +95 -95
- package/src/plugins/license/LicenseService.ts +141 -141
- package/src/plugins/license/actionHandlers/getLicense.ts +18 -18
- package/src/plugins/license/actionHandlers/index.ts +5 -5
- package/src/plugins/license/actionHandlers/updateLicense.ts +24 -24
- package/src/plugins/license/helpers/certHelper.ts +21 -21
- package/src/plugins/license/helpers/cryptoHelper.ts +47 -47
- package/src/plugins/license/models/index.ts +1 -1
- package/src/plugins/license/routes/getLicense.ts +15 -15
- package/src/plugins/license/routes/index.ts +4 -4
- package/src/plugins/license/routes/updateLicense.ts +15 -15
- package/src/plugins/mail/MailPlugin.ts +74 -74
- package/src/plugins/mail/MailPluginTypes.ts +27 -27
- package/src/plugins/mail/MailService.ts +38 -38
- package/src/plugins/mail/actionHandlers/index.ts +3 -3
- package/src/plugins/mail/models/index.ts +1 -1
- package/src/plugins/mail/routes/index.ts +1 -1
- package/src/plugins/metaManage/MetaManagePlugin.ts +198 -198
- package/src/plugins/metaManage/actionHandlers/getMetaModelDetail.ts +10 -10
- package/src/plugins/metaManage/actionHandlers/listMetaModels.ts +10 -10
- package/src/plugins/metaManage/actionHandlers/listMetaRoutes.ts +10 -10
- package/src/plugins/metaManage/services/MetaService.ts +376 -376
- package/src/plugins/notification/NotificationPlugin.ts +68 -68
- package/src/plugins/notification/NotificationPluginTypes.ts +13 -13
- package/src/plugins/notification/NotificationService.ts +25 -25
- package/src/plugins/notification/actionHandlers/index.ts +3 -3
- package/src/plugins/notification/models/Notification.ts +60 -60
- package/src/plugins/notification/models/index.ts +3 -3
- package/src/plugins/notification/routes/index.ts +1 -1
- package/src/plugins/routeManage/RouteManagePlugin.ts +64 -64
- package/src/plugins/routeManage/actionHandlers/httpProxy.ts +13 -13
- package/src/plugins/routeManage/actionHandlers/mock.ts +28 -28
- package/src/plugins/sequence/SequencePlugin.ts +146 -146
- package/src/plugins/sequence/SequencePluginTypes.ts +69 -69
- package/src/plugins/sequence/SequenceService.ts +92 -92
- package/src/plugins/sequence/actionHandlers/generateSn.ts +32 -32
- package/src/plugins/sequence/actionHandlers/index.ts +4 -4
- package/src/plugins/sequence/models/SequenceAutoIncrementRecord.ts +49 -49
- package/src/plugins/sequence/models/SequenceRule.ts +42 -42
- package/src/plugins/sequence/models/index.ts +4 -4
- package/src/plugins/sequence/routes/generateSn.ts +15 -15
- package/src/plugins/sequence/routes/index.ts +3 -3
- package/src/plugins/sequence/segment-utility.ts +11 -11
- package/src/plugins/sequence/segments/autoIncrement.ts +90 -90
- package/src/plugins/sequence/segments/dayOfMonth.ts +19 -19
- package/src/plugins/sequence/segments/index.ts +9 -9
- package/src/plugins/sequence/segments/literal.ts +16 -16
- package/src/plugins/sequence/segments/month.ts +19 -19
- package/src/plugins/sequence/segments/parameter.ts +20 -20
- package/src/plugins/sequence/segments/year.ts +19 -19
- package/src/plugins/serverOperation/ServerOperationPlugin.ts +93 -93
- package/src/plugins/serverOperation/ServerOperationPluginTypes.ts +25 -25
- package/src/plugins/serverOperation/actionHandlers/index.ts +4 -4
- package/src/plugins/serverOperation/actionHandlers/runServerOperation.ts +20 -20
- package/src/plugins/setting/SettingPlugin.ts +68 -68
- package/src/plugins/setting/SettingPluginTypes.ts +37 -37
- package/src/plugins/setting/SettingService.ts +222 -222
- package/src/plugins/setting/actionHandlers/getSystemSettingValues.ts +30 -30
- package/src/plugins/setting/actionHandlers/getUserSettingValues.ts +38 -38
- package/src/plugins/setting/actionHandlers/index.ts +6 -6
- package/src/plugins/setting/actionHandlers/setSystemSettingValues.ts +30 -30
- package/src/plugins/setting/models/SystemSettingGroupSetting.ts +57 -57
- package/src/plugins/setting/models/SystemSettingItem.ts +48 -48
- package/src/plugins/setting/models/SystemSettingItemSetting.ts +73 -73
- package/src/plugins/setting/models/UserSettingGroupSetting.ts +57 -57
- package/src/plugins/setting/models/UserSettingItem.ts +55 -55
- package/src/plugins/setting/models/UserSettingItemSetting.ts +73 -73
- package/src/plugins/setting/models/index.ts +8 -8
- package/src/plugins/setting/routes/getSystemSettingValues.ts +15 -15
- package/src/plugins/setting/routes/getUserSettingValues.ts +15 -15
- package/src/plugins/setting/routes/index.ts +5 -5
- package/src/plugins/setting/routes/setSystemSettingValues.ts +15 -15
- package/src/plugins/stateMachine/StateMachinePlugin.ts +196 -196
- package/src/plugins/stateMachine/StateMachinePluginTypes.ts +48 -48
- package/src/plugins/stateMachine/actionHandlers/index.ts +4 -4
- package/src/plugins/stateMachine/actionHandlers/sendStateMachineEvent.ts +54 -54
- package/src/plugins/stateMachine/models/StateMachine.ts +42 -42
- package/src/plugins/stateMachine/models/index.ts +3 -3
- package/src/plugins/stateMachine/routes/index.ts +3 -3
- package/src/plugins/stateMachine/routes/sendStateMachineEvent.ts +15 -15
- package/src/plugins/stateMachine/stateMachineHelper.ts +36 -36
- package/src/plugins/webhooks/WebhooksPlugin.ts +148 -148
- package/src/plugins/webhooks/pluginConfig.ts +75 -75
- package/src/polyfill.ts +5 -5
- package/src/proxy/mod.ts +38 -38
- package/src/proxy/types.ts +21 -21
- package/src/queryBuilder/index.ts +1 -1
- package/src/queryBuilder/queryBuilder.ts +755 -755
- package/src/server.ts +567 -556
- package/src/types/cron-job-types.ts +71 -71
- package/src/types.ts +884 -882
- package/src/utilities/accessControlUtility.ts +33 -33
- package/src/utilities/entityUtility.ts +18 -18
- package/src/utilities/errorUtility.ts +15 -15
- package/src/utilities/fsUtility.ts +137 -137
- package/src/utilities/httpUtility.ts +19 -19
- package/src/utilities/jwtUtility.ts +26 -26
- package/src/utilities/passwordUtility.ts +26 -26
- package/src/utilities/pathUtility.ts +14 -14
- package/src/utilities/timeUtility.ts +17 -17
- package/src/utilities/typeUtility.ts +15 -15
- package/tsconfig.json +19 -19
|
@@ -1,255 +1,255 @@
|
|
|
1
|
-
import { CronJob } from "cron";
|
|
2
|
-
import { IRpdServer } from "~/core/server";
|
|
3
|
-
import { find, isNil, isString, Many } from "lodash";
|
|
4
|
-
import { RouteContext } from "~/core/routeContext";
|
|
5
|
-
import { validateLicense } from "~/helpers/licenseHelper";
|
|
6
|
-
import { JobRunningResult, NamedCronJobInstance, SysCronJob, UpdateJobConfigOptions } from "../CronJobPluginTypes";
|
|
7
|
-
import { CronJobConfiguration } from "~/types/cron-job-types";
|
|
8
|
-
import { ActionHandlerContext } from "~/core/actionHandler";
|
|
9
|
-
import { formatDateTimeWithTimezone, getNowStringWithTimezone } from "~/utilities/timeUtility";
|
|
10
|
-
import { executeInRouteContext } from "~/helpers/dbTransactionHelper";
|
|
11
|
-
|
|
12
|
-
export default class CronJobService {
|
|
13
|
-
#server: IRpdServer;
|
|
14
|
-
#namedJobInstances: NamedCronJobInstance[];
|
|
15
|
-
|
|
16
|
-
constructor(server: IRpdServer) {
|
|
17
|
-
this.#server = server;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* 根据编号获取定时任务配置信息
|
|
22
|
-
* @param code
|
|
23
|
-
* @returns
|
|
24
|
-
*/
|
|
25
|
-
getJobConfigurationByCode(code: string) {
|
|
26
|
-
return find(this.#server.listCronJobs(), (job) => job.code === code);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
#createJobInstance(job: CronJobConfiguration) {
|
|
30
|
-
return CronJob.from({
|
|
31
|
-
...(job.jobOptions || {}),
|
|
32
|
-
cronTime: job.cronTime,
|
|
33
|
-
onTick: async () => {
|
|
34
|
-
await this.executeJob(job);
|
|
35
|
-
},
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
async #startJobInstance(routeContext: RouteContext, jobConfiguration: CronJobConfiguration, jobInstance: CronJob) {
|
|
40
|
-
const server = this.#server;
|
|
41
|
-
const jobCode = jobConfiguration.code;
|
|
42
|
-
const cronJobManager = server.getEntityManager<SysCronJob>("sys_cron_job");
|
|
43
|
-
const cronJobInDb = await cronJobManager.findEntity({
|
|
44
|
-
routeContext,
|
|
45
|
-
filters: [{ operator: "eq", field: "code", value: jobCode }],
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
if (cronJobInDb) {
|
|
49
|
-
let nextRunningTime: string | null;
|
|
50
|
-
nextRunningTime = formatDateTimeWithTimezone(jobInstance.nextDate().toISO());
|
|
51
|
-
|
|
52
|
-
await cronJobManager.updateEntityById({
|
|
53
|
-
routeContext,
|
|
54
|
-
id: cronJobInDb.id,
|
|
55
|
-
entityToSave: {
|
|
56
|
-
nextRunningTime,
|
|
57
|
-
} as Partial<SysCronJob>,
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
jobInstance.start();
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
async #setJobNextRunningTime(routeContext: RouteContext, jobCode: string, nextRunningTime: string | null) {
|
|
65
|
-
const server = this.#server;
|
|
66
|
-
const cronJobManager = server.getEntityManager<SysCronJob>("sys_cron_job");
|
|
67
|
-
const cronJobInDb = await cronJobManager.findEntity({
|
|
68
|
-
routeContext,
|
|
69
|
-
filters: [{ operator: "eq", field: "code", value: jobCode }],
|
|
70
|
-
});
|
|
71
|
-
await cronJobManager.updateEntityById({
|
|
72
|
-
routeContext,
|
|
73
|
-
id: cronJobInDb.id,
|
|
74
|
-
entityToSave: {
|
|
75
|
-
nextRunningTime,
|
|
76
|
-
} as Partial<SysCronJob>,
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* 重新加载定时任务
|
|
82
|
-
*/
|
|
83
|
-
async reloadJobs() {
|
|
84
|
-
const server = this.#server;
|
|
85
|
-
const routeContext = RouteContext.newSystemOperationContext(server);
|
|
86
|
-
|
|
87
|
-
if (this.#namedJobInstances) {
|
|
88
|
-
for (const job of this.#namedJobInstances) {
|
|
89
|
-
job.instance.stop();
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
this.#namedJobInstances = [];
|
|
94
|
-
|
|
95
|
-
const cronJobManager = server.getEntityManager<SysCronJob>("sys_cron_job");
|
|
96
|
-
const cronJobConfigurationsInDb = await cronJobManager.findEntities({ routeContext });
|
|
97
|
-
|
|
98
|
-
const cronJobConfigurations = server.listCronJobs();
|
|
99
|
-
for (const cronJobConfig of cronJobConfigurations) {
|
|
100
|
-
const jobCode = cronJobConfig.code;
|
|
101
|
-
const jobConfigInDb = find(cronJobConfigurationsInDb, { code: jobCode });
|
|
102
|
-
if (jobConfigInDb) {
|
|
103
|
-
overrideJobConfig(cronJobConfig, jobConfigInDb);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (cronJobConfig.disabled) {
|
|
107
|
-
await this.#setJobNextRunningTime(routeContext, jobCode, null);
|
|
108
|
-
continue;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const jobInstance = this.#createJobInstance(cronJobConfig);
|
|
112
|
-
await this.#startJobInstance(routeContext, cronJobConfig, jobInstance);
|
|
113
|
-
|
|
114
|
-
this.#namedJobInstances.push({
|
|
115
|
-
code: jobCode,
|
|
116
|
-
instance: jobInstance,
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* 执行指定任务
|
|
123
|
-
* @param job
|
|
124
|
-
* @param input
|
|
125
|
-
*/
|
|
126
|
-
async executeJob(job: CronJobConfiguration, input?: any) {
|
|
127
|
-
const server = this.#server;
|
|
128
|
-
const logger = server.getLogger();
|
|
129
|
-
|
|
130
|
-
const jobCode = job.code;
|
|
131
|
-
logger.info(`Executing cron job '${jobCode}'...`);
|
|
132
|
-
|
|
133
|
-
let handlerContext: ActionHandlerContext = {
|
|
134
|
-
logger,
|
|
135
|
-
routerContext: RouteContext.newSystemOperationContext(server),
|
|
136
|
-
server,
|
|
137
|
-
input,
|
|
138
|
-
results: [],
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
let result: JobRunningResult;
|
|
142
|
-
let lastErrorMessage: string | null;
|
|
143
|
-
let lastErrorStack: string | null;
|
|
144
|
-
try {
|
|
145
|
-
validateLicense(server);
|
|
146
|
-
|
|
147
|
-
await executeInRouteContext(handlerContext.routerContext, job.executeInDbTransaction, async () => {
|
|
148
|
-
if (job.actionHandlerCode) {
|
|
149
|
-
const actionHandler = server.getActionHandlerByCode(job.code);
|
|
150
|
-
await actionHandler(handlerContext, job.handleOptions);
|
|
151
|
-
} else {
|
|
152
|
-
await job.handler(handlerContext, job.handleOptions);
|
|
153
|
-
}
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
result = "success";
|
|
157
|
-
logger.info(`Completed cron job '${jobCode}'...`);
|
|
158
|
-
} catch (ex: any) {
|
|
159
|
-
logger.error('Cron job "%s" execution error: %s', jobCode, ex.message);
|
|
160
|
-
if (isString(ex)) {
|
|
161
|
-
lastErrorMessage = ex;
|
|
162
|
-
} else {
|
|
163
|
-
lastErrorMessage = ex.message;
|
|
164
|
-
lastErrorStack = ex.stack;
|
|
165
|
-
}
|
|
166
|
-
result = "failed";
|
|
167
|
-
|
|
168
|
-
if (job.onError) {
|
|
169
|
-
try {
|
|
170
|
-
await job.onError(handlerContext, ex);
|
|
171
|
-
} catch (ex) {
|
|
172
|
-
logger.error('Error handler of cron job "%s" execution failed: %s', jobCode, ex.message);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
try {
|
|
178
|
-
const cronJobManager = server.getEntityManager<SysCronJob>("sys_cron_job");
|
|
179
|
-
const cronJobInDb = await cronJobManager.findEntity({
|
|
180
|
-
filters: [{ operator: "eq", field: "code", value: jobCode }],
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
if (cronJobInDb) {
|
|
184
|
-
let nextRunningTime: string | null;
|
|
185
|
-
const namedJobInstance = find(this.#namedJobInstances, { code: jobCode });
|
|
186
|
-
if (namedJobInstance && namedJobInstance.instance) {
|
|
187
|
-
nextRunningTime = formatDateTimeWithTimezone(namedJobInstance.instance.nextDate().toISO());
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
await cronJobManager.updateEntityById({
|
|
191
|
-
id: cronJobInDb.id,
|
|
192
|
-
entityToSave: {
|
|
193
|
-
nextRunningTime,
|
|
194
|
-
lastRunningResult: result,
|
|
195
|
-
lastRunningTime: getNowStringWithTimezone(),
|
|
196
|
-
lastErrorMessage,
|
|
197
|
-
lastErrorStack,
|
|
198
|
-
} as Partial<SysCronJob>,
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
} catch (ex: any) {
|
|
202
|
-
logger.error("Failed to saving cron job running result. job code: %s, error: %s", jobCode, ex.message);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
async updateJobConfig(routeContext: RouteContext, options: UpdateJobConfigOptions) {
|
|
207
|
-
const server = this.#server;
|
|
208
|
-
const cronJobs = server.listCronJobs();
|
|
209
|
-
const jobCode = options.code;
|
|
210
|
-
if (!jobCode) {
|
|
211
|
-
throw new Error(`options.code is required.`);
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
const cronJobConfig = find(cronJobs, { code: jobCode });
|
|
215
|
-
if (!cronJobConfig) {
|
|
216
|
-
throw new Error(`Cron job with code "${jobCode}" not found.`);
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
if (!(["cronTime", "disabled", "jobOptions"] as (keyof typeof options)[]).some((field) => !isNil(options[field]))) {
|
|
220
|
-
return;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
overrideJobConfig(cronJobConfig, options);
|
|
224
|
-
|
|
225
|
-
const namedJobInstance = find(this.#namedJobInstances, { code: jobCode });
|
|
226
|
-
if (namedJobInstance && namedJobInstance.instance) {
|
|
227
|
-
namedJobInstance.instance.stop();
|
|
228
|
-
namedJobInstance.instance = null;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
if (cronJobConfig.disabled) {
|
|
232
|
-
await this.#setJobNextRunningTime(routeContext, jobCode, null);
|
|
233
|
-
} else {
|
|
234
|
-
const jobInstance = this.#createJobInstance(cronJobConfig);
|
|
235
|
-
await this.#startJobInstance(routeContext, cronJobConfig, jobInstance);
|
|
236
|
-
|
|
237
|
-
if (namedJobInstance) {
|
|
238
|
-
namedJobInstance.instance = jobInstance;
|
|
239
|
-
} else {
|
|
240
|
-
this.#namedJobInstances.push({
|
|
241
|
-
code: cronJobConfig.code,
|
|
242
|
-
instance: jobInstance,
|
|
243
|
-
});
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
function overrideJobConfig(original: Partial<SysCronJob> | CronJobConfiguration, overrides: Partial<SysCronJob>) {
|
|
250
|
-
(["cronTime", "disabled", "jobOptions"] as (keyof typeof overrides)[]).forEach((field: string) => {
|
|
251
|
-
if (!isNil(overrides[field])) {
|
|
252
|
-
original[field] = overrides[field];
|
|
253
|
-
}
|
|
254
|
-
});
|
|
255
|
-
}
|
|
1
|
+
import { CronJob } from "cron";
|
|
2
|
+
import { IRpdServer } from "~/core/server";
|
|
3
|
+
import { find, isNil, isString, Many } from "lodash";
|
|
4
|
+
import { RouteContext } from "~/core/routeContext";
|
|
5
|
+
import { validateLicense } from "~/helpers/licenseHelper";
|
|
6
|
+
import { JobRunningResult, NamedCronJobInstance, SysCronJob, UpdateJobConfigOptions } from "../CronJobPluginTypes";
|
|
7
|
+
import { CronJobConfiguration } from "~/types/cron-job-types";
|
|
8
|
+
import { ActionHandlerContext } from "~/core/actionHandler";
|
|
9
|
+
import { formatDateTimeWithTimezone, getNowStringWithTimezone } from "~/utilities/timeUtility";
|
|
10
|
+
import { executeInRouteContext } from "~/helpers/dbTransactionHelper";
|
|
11
|
+
|
|
12
|
+
export default class CronJobService {
|
|
13
|
+
#server: IRpdServer;
|
|
14
|
+
#namedJobInstances: NamedCronJobInstance[];
|
|
15
|
+
|
|
16
|
+
constructor(server: IRpdServer) {
|
|
17
|
+
this.#server = server;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* 根据编号获取定时任务配置信息
|
|
22
|
+
* @param code
|
|
23
|
+
* @returns
|
|
24
|
+
*/
|
|
25
|
+
getJobConfigurationByCode(code: string) {
|
|
26
|
+
return find(this.#server.listCronJobs(), (job) => job.code === code);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
#createJobInstance(job: CronJobConfiguration) {
|
|
30
|
+
return CronJob.from({
|
|
31
|
+
...(job.jobOptions || {}),
|
|
32
|
+
cronTime: job.cronTime,
|
|
33
|
+
onTick: async () => {
|
|
34
|
+
await this.executeJob(job);
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async #startJobInstance(routeContext: RouteContext, jobConfiguration: CronJobConfiguration, jobInstance: CronJob) {
|
|
40
|
+
const server = this.#server;
|
|
41
|
+
const jobCode = jobConfiguration.code;
|
|
42
|
+
const cronJobManager = server.getEntityManager<SysCronJob>("sys_cron_job");
|
|
43
|
+
const cronJobInDb = await cronJobManager.findEntity({
|
|
44
|
+
routeContext,
|
|
45
|
+
filters: [{ operator: "eq", field: "code", value: jobCode }],
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
if (cronJobInDb) {
|
|
49
|
+
let nextRunningTime: string | null;
|
|
50
|
+
nextRunningTime = formatDateTimeWithTimezone(jobInstance.nextDate().toISO());
|
|
51
|
+
|
|
52
|
+
await cronJobManager.updateEntityById({
|
|
53
|
+
routeContext,
|
|
54
|
+
id: cronJobInDb.id,
|
|
55
|
+
entityToSave: {
|
|
56
|
+
nextRunningTime,
|
|
57
|
+
} as Partial<SysCronJob>,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
jobInstance.start();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async #setJobNextRunningTime(routeContext: RouteContext, jobCode: string, nextRunningTime: string | null) {
|
|
65
|
+
const server = this.#server;
|
|
66
|
+
const cronJobManager = server.getEntityManager<SysCronJob>("sys_cron_job");
|
|
67
|
+
const cronJobInDb = await cronJobManager.findEntity({
|
|
68
|
+
routeContext,
|
|
69
|
+
filters: [{ operator: "eq", field: "code", value: jobCode }],
|
|
70
|
+
});
|
|
71
|
+
await cronJobManager.updateEntityById({
|
|
72
|
+
routeContext,
|
|
73
|
+
id: cronJobInDb.id,
|
|
74
|
+
entityToSave: {
|
|
75
|
+
nextRunningTime,
|
|
76
|
+
} as Partial<SysCronJob>,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* 重新加载定时任务
|
|
82
|
+
*/
|
|
83
|
+
async reloadJobs() {
|
|
84
|
+
const server = this.#server;
|
|
85
|
+
const routeContext = RouteContext.newSystemOperationContext(server);
|
|
86
|
+
|
|
87
|
+
if (this.#namedJobInstances) {
|
|
88
|
+
for (const job of this.#namedJobInstances) {
|
|
89
|
+
job.instance.stop();
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
this.#namedJobInstances = [];
|
|
94
|
+
|
|
95
|
+
const cronJobManager = server.getEntityManager<SysCronJob>("sys_cron_job");
|
|
96
|
+
const cronJobConfigurationsInDb = await cronJobManager.findEntities({ routeContext });
|
|
97
|
+
|
|
98
|
+
const cronJobConfigurations = server.listCronJobs();
|
|
99
|
+
for (const cronJobConfig of cronJobConfigurations) {
|
|
100
|
+
const jobCode = cronJobConfig.code;
|
|
101
|
+
const jobConfigInDb = find(cronJobConfigurationsInDb, { code: jobCode });
|
|
102
|
+
if (jobConfigInDb) {
|
|
103
|
+
overrideJobConfig(cronJobConfig, jobConfigInDb);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (cronJobConfig.disabled) {
|
|
107
|
+
await this.#setJobNextRunningTime(routeContext, jobCode, null);
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const jobInstance = this.#createJobInstance(cronJobConfig);
|
|
112
|
+
await this.#startJobInstance(routeContext, cronJobConfig, jobInstance);
|
|
113
|
+
|
|
114
|
+
this.#namedJobInstances.push({
|
|
115
|
+
code: jobCode,
|
|
116
|
+
instance: jobInstance,
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* 执行指定任务
|
|
123
|
+
* @param job
|
|
124
|
+
* @param input
|
|
125
|
+
*/
|
|
126
|
+
async executeJob(job: CronJobConfiguration, input?: any) {
|
|
127
|
+
const server = this.#server;
|
|
128
|
+
const logger = server.getLogger();
|
|
129
|
+
|
|
130
|
+
const jobCode = job.code;
|
|
131
|
+
logger.info(`Executing cron job '${jobCode}'...`);
|
|
132
|
+
|
|
133
|
+
let handlerContext: ActionHandlerContext = {
|
|
134
|
+
logger,
|
|
135
|
+
routerContext: RouteContext.newSystemOperationContext(server),
|
|
136
|
+
server,
|
|
137
|
+
input,
|
|
138
|
+
results: [],
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
let result: JobRunningResult;
|
|
142
|
+
let lastErrorMessage: string | null;
|
|
143
|
+
let lastErrorStack: string | null;
|
|
144
|
+
try {
|
|
145
|
+
validateLicense(server);
|
|
146
|
+
|
|
147
|
+
await executeInRouteContext(handlerContext.routerContext, job.executeInDbTransaction, async () => {
|
|
148
|
+
if (job.actionHandlerCode) {
|
|
149
|
+
const actionHandler = server.getActionHandlerByCode(job.code);
|
|
150
|
+
await actionHandler(handlerContext, job.handleOptions);
|
|
151
|
+
} else {
|
|
152
|
+
await job.handler(handlerContext, job.handleOptions);
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
result = "success";
|
|
157
|
+
logger.info(`Completed cron job '${jobCode}'...`);
|
|
158
|
+
} catch (ex: any) {
|
|
159
|
+
logger.error('Cron job "%s" execution error: %s', jobCode, ex.message);
|
|
160
|
+
if (isString(ex)) {
|
|
161
|
+
lastErrorMessage = ex;
|
|
162
|
+
} else {
|
|
163
|
+
lastErrorMessage = ex.message;
|
|
164
|
+
lastErrorStack = ex.stack;
|
|
165
|
+
}
|
|
166
|
+
result = "failed";
|
|
167
|
+
|
|
168
|
+
if (job.onError) {
|
|
169
|
+
try {
|
|
170
|
+
await job.onError(handlerContext, ex);
|
|
171
|
+
} catch (ex) {
|
|
172
|
+
logger.error('Error handler of cron job "%s" execution failed: %s', jobCode, ex.message);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
try {
|
|
178
|
+
const cronJobManager = server.getEntityManager<SysCronJob>("sys_cron_job");
|
|
179
|
+
const cronJobInDb = await cronJobManager.findEntity({
|
|
180
|
+
filters: [{ operator: "eq", field: "code", value: jobCode }],
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
if (cronJobInDb) {
|
|
184
|
+
let nextRunningTime: string | null;
|
|
185
|
+
const namedJobInstance = find(this.#namedJobInstances, { code: jobCode });
|
|
186
|
+
if (namedJobInstance && namedJobInstance.instance) {
|
|
187
|
+
nextRunningTime = formatDateTimeWithTimezone(namedJobInstance.instance.nextDate().toISO());
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
await cronJobManager.updateEntityById({
|
|
191
|
+
id: cronJobInDb.id,
|
|
192
|
+
entityToSave: {
|
|
193
|
+
nextRunningTime,
|
|
194
|
+
lastRunningResult: result,
|
|
195
|
+
lastRunningTime: getNowStringWithTimezone(),
|
|
196
|
+
lastErrorMessage,
|
|
197
|
+
lastErrorStack,
|
|
198
|
+
} as Partial<SysCronJob>,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
} catch (ex: any) {
|
|
202
|
+
logger.error("Failed to saving cron job running result. job code: %s, error: %s", jobCode, ex.message);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
async updateJobConfig(routeContext: RouteContext, options: UpdateJobConfigOptions) {
|
|
207
|
+
const server = this.#server;
|
|
208
|
+
const cronJobs = server.listCronJobs();
|
|
209
|
+
const jobCode = options.code;
|
|
210
|
+
if (!jobCode) {
|
|
211
|
+
throw new Error(`options.code is required.`);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const cronJobConfig = find(cronJobs, { code: jobCode });
|
|
215
|
+
if (!cronJobConfig) {
|
|
216
|
+
throw new Error(`Cron job with code "${jobCode}" not found.`);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (!(["cronTime", "disabled", "jobOptions"] as (keyof typeof options)[]).some((field) => !isNil(options[field]))) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
overrideJobConfig(cronJobConfig, options);
|
|
224
|
+
|
|
225
|
+
const namedJobInstance = find(this.#namedJobInstances, { code: jobCode });
|
|
226
|
+
if (namedJobInstance && namedJobInstance.instance) {
|
|
227
|
+
namedJobInstance.instance.stop();
|
|
228
|
+
namedJobInstance.instance = null;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (cronJobConfig.disabled) {
|
|
232
|
+
await this.#setJobNextRunningTime(routeContext, jobCode, null);
|
|
233
|
+
} else {
|
|
234
|
+
const jobInstance = this.#createJobInstance(cronJobConfig);
|
|
235
|
+
await this.#startJobInstance(routeContext, cronJobConfig, jobInstance);
|
|
236
|
+
|
|
237
|
+
if (namedJobInstance) {
|
|
238
|
+
namedJobInstance.instance = jobInstance;
|
|
239
|
+
} else {
|
|
240
|
+
this.#namedJobInstances.push({
|
|
241
|
+
code: cronJobConfig.code,
|
|
242
|
+
instance: jobInstance,
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function overrideJobConfig(original: Partial<SysCronJob> | CronJobConfiguration, overrides: Partial<SysCronJob>) {
|
|
250
|
+
(["cronTime", "disabled", "jobOptions"] as (keyof typeof overrides)[]).forEach((field: string) => {
|
|
251
|
+
if (!isNil(overrides[field])) {
|
|
252
|
+
original[field] = overrides[field];
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
}
|