midway-fatcms 0.0.1-beta.14 → 0.0.1-beta.17

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.
Files changed (85) hide show
  1. package/README.md +7 -0
  2. package/dist/configuration.d.ts +1 -0
  3. package/dist/configuration.js +21 -8
  4. package/dist/controller/base/BaseApiController.d.ts +1 -1
  5. package/dist/controller/gateway/AsyncTaskController.js +15 -4
  6. package/dist/controller/gateway/CrudMtdGatewayController.d.ts +3 -3
  7. package/dist/controller/gateway/CrudMtdGatewayController.js +5 -5
  8. package/dist/index.d.ts +3 -0
  9. package/dist/index.js +3 -0
  10. package/dist/libs/crud-pro/CrudPro.d.ts +1 -0
  11. package/dist/libs/crud-pro/CrudPro.js +5 -0
  12. package/dist/libs/crud-pro/defaultConfigs.js +2 -0
  13. package/dist/libs/crud-pro/interfaces.d.ts +6 -0
  14. package/dist/libs/crud-pro/models/ExecuteContext.d.ts +2 -1
  15. package/dist/libs/crud-pro/models/ExecuteContextFunc.d.ts +8 -1
  16. package/dist/libs/crud-pro/models/ExecuteContextFunc.js +8 -0
  17. package/dist/libs/crud-pro/models/ResModel.d.ts +16 -0
  18. package/dist/libs/crud-pro/models/ResModel.js +2 -0
  19. package/dist/libs/crud-pro/services/CrudProCachedCfgService.d.ts +1 -0
  20. package/dist/libs/crud-pro/services/CrudProCachedCfgService.js +22 -11
  21. package/dist/libs/crud-pro/services/CrudProServiceBase.d.ts +2 -6
  22. package/dist/libs/crud-pro/services/CrudProServiceBase.js +3 -2
  23. package/dist/libs/crud-pro/services/CrudProTableMetaService.d.ts +1 -0
  24. package/dist/libs/crud-pro/services/CrudProTableMetaService.js +31 -7
  25. package/dist/libs/crud-pro/utils/DatabaseName.js +24 -3
  26. package/dist/libs/global-config/global-config.d.ts +6 -0
  27. package/dist/libs/global-config/global-config.js +1 -0
  28. package/dist/models/AsyncTaskModel.d.ts +4 -2
  29. package/dist/schedule/anonymousContext.d.ts +13 -0
  30. package/dist/schedule/anonymousContext.js +59 -0
  31. package/dist/schedule/index.d.ts +4 -4
  32. package/dist/schedule/index.js +9 -68
  33. package/dist/schedule/runSchedule.d.ts +6 -0
  34. package/dist/schedule/runSchedule.js +34 -0
  35. package/dist/schedule/scheduleNames.d.ts +8 -0
  36. package/dist/schedule/scheduleNames.js +17 -0
  37. package/dist/service/asyncTask/AsyncTaskRunnerService.d.ts +23 -0
  38. package/dist/service/asyncTask/AsyncTaskRunnerService.js +86 -13
  39. package/dist/service/asyncTask/AsyncTaskService.js +2 -1
  40. package/dist/service/crudstd/CrudStdService.d.ts +1 -0
  41. package/dist/service/crudstd/CrudStdService.js +27 -0
  42. package/dist/service/curd/CrudProQuick.d.ts +24 -0
  43. package/dist/service/curd/CrudProQuick.js +105 -0
  44. package/dist/service/curd/CurdMixByLinkToCustomService.d.ts +10 -1
  45. package/dist/service/curd/CurdMixByLinkToCustomService.js +71 -23
  46. package/dist/service/curd/CurdMixService.d.ts +1 -1
  47. package/dist/service/curd/CurdMixService.js +2 -2
  48. package/dist/service/curd/CurdMixUtils.js +35 -28
  49. package/dist/service/curd/CurdProService.d.ts +2 -10
  50. package/dist/service/curd/CurdProService.js +28 -143
  51. package/dist/service/curd/fixCfgModel.d.ts +3 -0
  52. package/dist/service/curd/fixCfgModel.js +107 -0
  53. package/package.json +1 -1
  54. package/src/configuration.ts +30 -10
  55. package/src/controller/gateway/AnyApiGatewayController.ts +1 -1
  56. package/src/controller/gateway/AsyncTaskController.ts +18 -5
  57. package/src/controller/gateway/CrudMtdGatewayController.ts +5 -5
  58. package/src/controller/manage/DocManageApi.ts +5 -5
  59. package/src/index.ts +3 -0
  60. package/src/libs/crud-pro/CrudPro.ts +8 -0
  61. package/src/libs/crud-pro/defaultConfigs.ts +2 -0
  62. package/src/libs/crud-pro/interfaces.ts +10 -1
  63. package/src/libs/crud-pro/models/ExecuteContext.ts +5 -2
  64. package/src/libs/crud-pro/models/ExecuteContextFunc.ts +12 -1
  65. package/src/libs/crud-pro/models/ResModel.ts +24 -0
  66. package/src/libs/crud-pro/services/CrudProCachedCfgService.ts +26 -17
  67. package/src/libs/crud-pro/services/CrudProServiceBase.ts +5 -8
  68. package/src/libs/crud-pro/services/CrudProTableMetaService.ts +43 -9
  69. package/src/libs/crud-pro/utils/DatabaseName.ts +35 -15
  70. package/src/libs/global-config/global-config.ts +12 -1
  71. package/src/models/AsyncTaskModel.ts +5 -2
  72. package/src/schedule/anonymousContext.ts +79 -0
  73. package/src/schedule/index.ts +13 -72
  74. package/src/schedule/runSchedule.ts +35 -0
  75. package/src/schedule/scheduleNames.ts +20 -0
  76. package/src/service/asyncTask/AsyncTaskRunnerService.ts +113 -19
  77. package/src/service/asyncTask/AsyncTaskService.ts +3 -2
  78. package/src/service/base/BaseService.ts +2 -2
  79. package/src/service/crudstd/CrudStdService.ts +34 -6
  80. package/src/service/curd/CrudProQuick.ts +137 -0
  81. package/src/service/curd/CurdMixByLinkToCustomService.ts +100 -49
  82. package/src/service/curd/CurdMixService.ts +3 -3
  83. package/src/service/curd/CurdMixUtils.ts +51 -38
  84. package/src/service/curd/CurdProService.ts +42 -186
  85. package/src/service/curd/fixCfgModel.ts +139 -0
@@ -26,12 +26,14 @@ export interface SysAsyncTaskEntity {
26
26
  priority: number;
27
27
  retry_count: number;
28
28
  max_retries: number;
29
+
29
30
  parent_task_id: number | null;
30
31
  created_at: Date;
31
32
  started_at: Date | null;
32
- completed_at: Date | null;
33
33
  updated_at: Date;
34
34
  expired_at: Date | null;
35
+ completed_at: Date | null;
36
+ created_user_session: string;
35
37
  }
36
38
 
37
39
  export enum SysAsyncTaskStatus {
@@ -58,11 +60,12 @@ export enum SysAsyncFileFormat {
58
60
 
59
61
 
60
62
  export interface ISysAsyncTaskHandler {
61
- execute(ctx: SysAsyncTaskContext): Promise<void>;
63
+ execute(asyncTaskContext: SysAsyncTaskContext): Promise<void>;
62
64
  }
63
65
 
64
66
  export interface SysAsyncTaskContext {
65
67
  task: SysAsyncTaskEntity;
68
+ updateTaskStatus: ()=>any;
66
69
  }
67
70
 
68
71
  export interface SysAsyncTaskHandlerConfig {
@@ -0,0 +1,79 @@
1
+ import * as koa from '@midwayjs/koa';
2
+ import {ContextLogger} from "@/models/contextLogger";
3
+ import {Transaction} from "@/libs/crud-pro/models/Transaction";
4
+ import {ISessionInfo, SYS_ACCOUNT_TYPE, UserSessionInfo} from "@/models/userSession";
5
+ import {IWorkbenchEntity} from "@/models/SystemEntities";
6
+
7
+
8
+ const anonymousUserInfo:ISessionInfo = {
9
+ nickName: 'anonymous_user',
10
+ avatar: '',
11
+ roleCodes: [],
12
+ functionCodes: [],
13
+ loginName: 'anonymous_user',
14
+ sessionId: '',
15
+ accountId: '',
16
+ workbenchCode: '',
17
+ accountType: SYS_ACCOUNT_TYPE,
18
+ };
19
+
20
+ const anonymousWorkbenchInfo: IWorkbenchEntity = {
21
+ id: 0,
22
+ workbench_code: 'anonymous_workbench',
23
+ workbench_name: 'anonymous_workbench',
24
+ workbench_domain: 'anonymous_workbench',
25
+ workbench_desc: 'anonymous_workbench',
26
+ html_content: 'anonymous_workbench',
27
+ status: 1,
28
+ workbench_type: 0,
29
+ };
30
+
31
+
32
+ export type RunServiceAtAnonymousContextRunner = (ctx: koa.IMidwayKoaContext)=>any;
33
+
34
+
35
+ class AnonymousContext {
36
+ private app: koa.Application;
37
+
38
+ setApp(app: koa.Application){
39
+ this.app = app;
40
+ }
41
+
42
+ getApp(){
43
+ return this.app;
44
+ }
45
+
46
+
47
+ async runServiceAtAnonymousContext( runner: RunServiceAtAnonymousContextRunner) {
48
+ const app = this.app;
49
+ const ctx: koa.IMidwayKoaContext = app.createAnonymousContext();
50
+
51
+ ctx.app = app;
52
+ ctx.contextLogger = new ContextLogger(ctx as any);
53
+ ctx.transaction = new Transaction();
54
+ ctx.userSession = new UserSessionInfo(anonymousUserInfo, true);
55
+ ctx.workbenchInfo = anonymousWorkbenchInfo;
56
+
57
+ let result: any = null;
58
+ let error: any = null;
59
+ try {
60
+ result = await runner(ctx);
61
+ } catch (e) {
62
+ error = e;
63
+ ctx.logger.error(`runServiceAtAnonymousContext error `, e);
64
+ } finally {
65
+ await ctx.transaction.releaseTx();
66
+ }
67
+ return {result: result, error: error};
68
+ }
69
+
70
+ }
71
+
72
+
73
+ const ANONYMOUS_CONTEXT = new AnonymousContext()
74
+
75
+
76
+
77
+ export {
78
+ ANONYMOUS_CONTEXT
79
+ }
@@ -1,73 +1,14 @@
1
- import * as koa from '@midwayjs/koa';
2
- import { IScheduleService } from '../interface';
3
- import { ContextLogger } from '../models/contextLogger';
4
- import { Transaction } from '../libs/crud-pro/models/Transaction';
5
- import {ISessionInfo, SYS_ACCOUNT_TYPE, UserSessionInfo} from '../models/userSession';
6
- import { IWorkbenchEntity } from '../models/SystemEntities';
7
-
8
- const scheduleUserInfo:ISessionInfo = {
9
- nickName: 'schedule_user',
10
- avatar: '',
11
- roleCodes: [],
12
- functionCodes: [],
13
- loginName: 'schedule_user',
14
- sessionId: '',
15
- accountId: '',
16
- workbenchCode: '',
17
- accountType: SYS_ACCOUNT_TYPE,
18
- };
19
-
20
- const scheduleWorkbenchInfo: IWorkbenchEntity = {
21
- id: 0,
22
- workbench_code: 'schedule_workbench',
23
- workbench_name: 'schedule_workbench',
24
- workbench_domain: 'schedule_workbench',
25
- workbench_desc: 'schedule_workbench',
26
- html_content: 'schedule_workbench',
27
- status: 1,
28
- workbench_type: 0,
29
- };
30
-
31
- async function runScheduleService(app: koa.Application, serviceName: string) {
32
- const ctx = app.createAnonymousContext();
33
-
34
- ctx.app = app;
35
- ctx.contextLogger = new ContextLogger(ctx as any);
36
- ctx.transaction = new Transaction();
37
- ctx.userSession = new UserSessionInfo(scheduleUserInfo, true);
38
- ctx.workbenchInfo = scheduleWorkbenchInfo;
39
-
40
- try {
41
- const serviceObject: IScheduleService = await ctx.requestContext.getAsync(serviceName);
42
- if (serviceObject && serviceObject.runBySchedule) {
43
- await serviceObject.runBySchedule();
44
- } else {
45
- ctx.logger.error(`runSchedule error , serviceName = ${serviceName}, 没有找到服务`);
46
- }
47
- } catch (e) {
48
- ctx.logger.error(`runSchedule error , serviceName = ${serviceName}`, e);
49
- } finally {
50
- await ctx.transaction.releaseTx();
51
- }
1
+ import {startScheduleLoop, runScheduleTaskOnce} from './runSchedule'
2
+ import {ANONYMOUS_CONTEXT} from './anonymousContext'
3
+ import {
4
+ ALL_SCHEDULE_NAMES,
5
+ SCHEDULE_NAMES,
6
+ } from './scheduleNames'
7
+
8
+ export {
9
+ ALL_SCHEDULE_NAMES,
10
+ SCHEDULE_NAMES,
11
+ ANONYMOUS_CONTEXT,
12
+ startScheduleLoop,
13
+ runScheduleTaskOnce
52
14
  }
53
-
54
- async function runSchedule(app: koa.Application, serviceList: string[]) {
55
- for (let i = 0; i < serviceList.length; i++) {
56
- const serviceName = serviceList[i];
57
- try {
58
- await runScheduleService(app, serviceName);
59
- } catch (e) {
60
- const ctx = app.createAnonymousContext();
61
- ctx.logger.error(`runSchedule error , serviceName = ${serviceName}`, e);
62
- }
63
- }
64
- }
65
-
66
- async function startSchedule(app: koa.Application, serviceList: string[]) {
67
- await runSchedule(app, serviceList);
68
- setInterval(() => {
69
- runSchedule(app, serviceList);
70
- }, 2 * 60 * 1000);
71
- }
72
-
73
- export { startSchedule, runSchedule };
@@ -0,0 +1,35 @@
1
+ import * as koa from '@midwayjs/koa';
2
+ import { IScheduleService } from '@/interface';
3
+ import { ANONYMOUS_CONTEXT } from "./anonymousContext";
4
+
5
+
6
+ async function runScheduleTaskOnce(serviceName: string) {
7
+ return await ANONYMOUS_CONTEXT.runServiceAtAnonymousContext(async (ctx: koa.IMidwayKoaContext)=>{
8
+ const serviceObject: IScheduleService = await ctx.requestContext.getAsync(serviceName);
9
+ if (serviceObject && serviceObject.runBySchedule) {
10
+ await serviceObject.runBySchedule();
11
+ } else {
12
+ ctx.logger.error(`runSchedule error , serviceName = ${serviceName}, 没有找到服务`);
13
+ }
14
+ });
15
+ }
16
+
17
+ async function runScheduleServiceList(serviceList: string[]) {
18
+ for (let i = 0; i < serviceList.length; i++) {
19
+ const serviceName = serviceList[i];
20
+ try {
21
+ await runScheduleTaskOnce(serviceName);
22
+ } catch (e) {
23
+ ANONYMOUS_CONTEXT.getApp().getCoreLogger().error(`runSchedule error , serviceName = ${serviceName}`, e);
24
+ }
25
+ }
26
+ }
27
+
28
+ async function startScheduleLoop(serviceList: string[]) {
29
+ await runScheduleServiceList(serviceList);
30
+ setInterval(() => {
31
+ runScheduleServiceList(serviceList);
32
+ }, 2 * 60 * 1000);
33
+ }
34
+
35
+ export { startScheduleLoop, runScheduleTaskOnce };
@@ -0,0 +1,20 @@
1
+
2
+
3
+ const SCHEDULE_NAMES = {
4
+ proxyApiLoadService: 'proxyApiLoadService',
5
+ workbenchService: 'workbenchService',
6
+ visitStatService: 'visitStatService',
7
+ asyncTaskRunnerService: 'asyncTaskRunnerService',
8
+ }
9
+
10
+ const ALL_SCHEDULE_NAMES = [
11
+ SCHEDULE_NAMES.proxyApiLoadService,
12
+ SCHEDULE_NAMES.workbenchService,
13
+ SCHEDULE_NAMES.visitStatService,
14
+ SCHEDULE_NAMES.asyncTaskRunnerService
15
+ ];
16
+
17
+ export {
18
+ ALL_SCHEDULE_NAMES,
19
+ SCHEDULE_NAMES,
20
+ }
@@ -1,5 +1,5 @@
1
1
  import {Inject, Provide} from '@midwayjs/core';
2
- import {Context} from '@midwayjs/koa';
2
+ import {Context, IMidwayKoaContext} from '@midwayjs/koa';
3
3
  import {BaseService} from "@/service/base/BaseService";
4
4
  import {IScheduleService} from "@/interface";
5
5
  import {CurdProService} from "@/service/curd/CurdProService";
@@ -8,9 +8,10 @@ import {GLOBAL_STATIC_CONFIG} from "@/libs/global-config/global-config";
8
8
  import {SystemTables} from "@/models/SystemTables";
9
9
  import {KeysOfSimpleSQL} from "@/libs/crud-pro/models/keys";
10
10
  import {errorToString} from "@/libs/utils/errorToString";
11
+ import {ANONYMOUS_CONTEXT} from "@/schedule";
11
12
 
12
13
 
13
- class AsyncTaskRunner {
14
+ class AsyncTaskRunner {
14
15
  isBusy: boolean = false;
15
16
  taskHandlerMap: Map<string, ISysAsyncTaskHandler> = new Map();
16
17
 
@@ -18,19 +19,29 @@ class AsyncTaskRunner {
18
19
  if (!taskList || taskList.length === 0) {
19
20
  return;
20
21
  }
22
+
21
23
  this.isBusy = true;
22
24
 
23
25
  for (let i = 0; i < taskList.length; i++) {
24
26
  const taskElement = taskList[i];
27
+
25
28
  try {
29
+ // 任务状态的设置在自定义的执行器里面。
26
30
  await this.executeTask(taskElement);
27
- taskElement.task_status = SysAsyncTaskStatus.SUCCEEDED;
28
31
  } catch (error) {
29
32
  taskElement.task_status = SysAsyncTaskStatus.FAILED;
30
33
  taskElement.error_message = errorToString(error)
31
34
  }
35
+
36
+ try {
37
+ await this.updateTaskStatus(taskElement);
38
+ } catch (error) {
39
+ ANONYMOUS_CONTEXT.getApp().getCoreLogger().error("executeTaskList error", error);
40
+ }
32
41
  }
33
42
 
43
+ this.isBusy = false;
44
+
34
45
  }
35
46
 
36
47
  private async executeTask(taskElement: SysAsyncTaskEntity) {
@@ -39,18 +50,67 @@ class AsyncTaskRunner {
39
50
  if (!taskHandler) {
40
51
  throw new Error('TaskHandler not found , taskType = ' + taskType);
41
52
  }
42
- await taskHandler.execute({task: taskElement});
53
+ const updateTaskStatus = () => {
54
+ return this.updateTaskStatus(taskElement);
55
+ }
56
+ await taskHandler.execute({task: taskElement, updateTaskStatus});
43
57
  }
58
+
59
+
60
+ /**
61
+ * 更新任务状态或任务进度
62
+ * @param taskElement
63
+ * @private
64
+ */
65
+ private async updateTaskStatus(taskElement: SysAsyncTaskEntity) {
66
+ return await ANONYMOUS_CONTEXT.runServiceAtAnonymousContext(async (ctx: IMidwayKoaContext) => {
67
+ const curdProService: CurdProService = await ctx.requestContext.getAsync("curdProService");
68
+ const {SystemDbName, SystemDbType} = GLOBAL_STATIC_CONFIG.getConfig();
69
+
70
+ const {id, ...otherTaskProps} = taskElement;
71
+
72
+ const res = await curdProService.executeCrudByCfg({
73
+ condition: {
74
+ id: id
75
+ },
76
+ data: otherTaskProps
77
+ }, {
78
+ sqlTable: SystemTables.sys_async_tasks,
79
+ sqlSimpleName: KeysOfSimpleSQL.SIMPLE_UPDATE,
80
+ sqlDatabase: SystemDbName,
81
+ sqlDbType: SystemDbType,
82
+ });
83
+ return res.getResModel().affected;
84
+ });
85
+
86
+ }
87
+
88
+
89
+ /**
90
+ * 获取当前进程可以处理的任务类型。
91
+ */
92
+ public getHandlerTaskTypeList(): string[] {
93
+ const keys = this.taskHandlerMap.keys();
94
+ return [...keys];
95
+ }
96
+
97
+
44
98
  }
45
99
 
46
100
 
47
- const ASYNC_TASK_RUNNER = new AsyncTaskRunner();
101
+ /**
102
+ * 业务可以扩展
103
+ */
104
+ export const ASYNC_TASK_RUNNER = new AsyncTaskRunner();
48
105
 
49
106
 
107
+ /**
108
+ * Redis锁
109
+ */
50
110
  const LOCK_KEY_ASYNC_TASK_RUNNER = "LOCK_KEY_ASYNC_TASK_RUNNER"
51
111
 
52
112
  @Provide()
53
- export class AsyncTaskRunnerService extends BaseService implements IScheduleService{
113
+ export class AsyncTaskRunnerService extends BaseService implements IScheduleService {
54
114
 
55
115
  @Inject()
56
116
  protected ctx: Context;
@@ -58,39 +118,44 @@ export class AsyncTaskRunnerService extends BaseService implements IScheduleServ
58
118
  @Inject()
59
119
  private curdProService: CurdProService;
60
120
 
61
- async runBySchedule () {
62
- if (ASYNC_TASK_RUNNER.isBusy) {
63
- return Promise.resolve();
64
- }
65
121
 
66
- const lock = await this.redisService.set(LOCK_KEY_ASYNC_TASK_RUNNER, 1,"EX", 3600, "NX");
67
- if (lock !== 'OK') {
122
+ async fetchPendingTasks(): Promise<void> {
123
+
124
+ const {SystemDbName, SystemDbType} = GLOBAL_STATIC_CONFIG.getConfig();
125
+
126
+ // 只获取本进程能够处理的任务类型。
127
+ const taskTypeList = ASYNC_TASK_RUNNER.getHandlerTaskTypeList();
128
+ if (taskTypeList.length === 0) {
68
129
  return Promise.resolve();
69
130
  }
70
131
 
71
132
 
72
- const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
73
-
133
+ // 查询等待处理的任务。
74
134
  const queryRes = await this.curdProService.executeCrudByCfg({
75
135
  condition: {
76
- task_status: SysAsyncTaskStatus.PENDING
136
+ task_status: SysAsyncTaskStatus.PENDING,
137
+ task_type: {
138
+ "$in": taskTypeList
139
+ }
77
140
  },
141
+ orderBy: 'id+',
78
142
  limit: 10
79
- },{
143
+ }, {
80
144
  sqlTable: SystemTables.sys_async_tasks,
81
145
  sqlSimpleName: KeysOfSimpleSQL.SIMPLE_QUERY_PAGE,
82
146
  sqlDatabase: SystemDbName,
83
147
  sqlDbType: SystemDbType,
84
148
  });
85
149
 
150
+
86
151
  const taskList = queryRes.getResRows();
87
152
 
88
153
  if (taskList.length === 0) {
89
154
  return Promise.resolve();
90
155
  }
91
-
92
156
  const taskIds = taskList.map(elem => elem.id).filter(Boolean)
93
157
 
158
+ // 将状态更新为处理中。防止其它进程重复处理。
94
159
  await this.curdProService.executeCrudByCfg({
95
160
  condition: {
96
161
  id: {
@@ -100,7 +165,7 @@ export class AsyncTaskRunnerService extends BaseService implements IScheduleServ
100
165
  data: {
101
166
  task_status: SysAsyncTaskStatus.RUNNING
102
167
  }
103
- },{
168
+ }, {
104
169
  sqlTable: SystemTables.sys_async_tasks,
105
170
  sqlSimpleName: KeysOfSimpleSQL.SIMPLE_UPDATE,
106
171
  sqlDatabase: SystemDbName,
@@ -108,7 +173,36 @@ export class AsyncTaskRunnerService extends BaseService implements IScheduleServ
108
173
  });
109
174
 
110
175
 
111
- ASYNC_TASK_RUNNER.executeTaskList(taskList);
176
+ // 开始执行。
177
+ ASYNC_TASK_RUNNER.executeTaskList(taskList).then(() => {
178
+ console.log("ASYNC_TASK_RUNNER finished taskIds ==> " + JSON.stringify(taskIds))
179
+ })
180
+
181
+ }
182
+
183
+
184
+ async runBySchedule() {
185
+ if (ASYNC_TASK_RUNNER.isBusy) {
186
+ return Promise.resolve();
187
+ }
188
+
189
+ // 这里的过期时间1分钟即可。fetchPendingTasks函数不可能超过一分钟。
190
+ // 因为这里只是从数据库中获取一批任务,放到自己的任务队列里。还没触发执行。
191
+ const lock = await this.redisService.set(LOCK_KEY_ASYNC_TASK_RUNNER, 1, "EX", 60, "NX");
192
+ if (lock !== 'OK') {
193
+ return Promise.resolve();
194
+ }
195
+
196
+ try {
197
+ await this.fetchPendingTasks();
198
+ } catch (e) {
199
+ console.error("fetchPendingTasks error", errorToString(e));
200
+ }
201
+
202
+ await this.redisService.del(LOCK_KEY_ASYNC_TASK_RUNNER);
203
+
112
204
  return Promise.resolve();
113
205
  }
206
+
207
+
114
208
  }
@@ -1,7 +1,8 @@
1
1
  import {Inject, Provide} from '@midwayjs/core';
2
2
  import {Context} from '@midwayjs/koa';
3
3
  import {BaseService} from "@/service/base/BaseService";
4
- import {runSchedule} from "@/schedule";
4
+ import {runScheduleTaskOnce} from "@/schedule";
5
+ import {SCHEDULE_NAMES} from "@/schedule/scheduleNames";
5
6
 
6
7
  @Provide()
7
8
  export class AsyncTaskService extends BaseService {
@@ -9,7 +10,7 @@ export class AsyncTaskService extends BaseService {
9
10
  protected ctx: Context;
10
11
 
11
12
  async startTask() {
12
- runSchedule(this.app, ['asyncTaskRunnerService']).then(schedule => {
13
+ runScheduleTaskOnce(SCHEDULE_NAMES.asyncTaskRunnerService).then(schedule => {
13
14
  console.log(schedule);
14
15
  })
15
16
  }
@@ -3,8 +3,8 @@ import { Context } from '@midwayjs/koa';
3
3
  import { RedisService } from '@midwayjs/redis';
4
4
  import { OSSService, OSSServiceFactory } from '@midwayjs/oss';
5
5
  import * as koa from '@midwayjs/koa';
6
- import { consoleLogger, ContextLogger, ILoggerContext } from '../../models/contextLogger';
7
- import { isEnableDebug, isLocalEnv } from '../../libs/utils/fatcms-request';
6
+ import { consoleLogger, ContextLogger, ILoggerContext } from '@/models/contextLogger';
7
+ import { isEnableDebug, isLocalEnv } from '@/libs/utils/fatcms-request';
8
8
 
9
9
 
10
10
  @Provide()
@@ -2,18 +2,19 @@ import { Inject, Provide } from '@midwayjs/core';
2
2
  import { Context } from '@midwayjs/koa';
3
3
  import { CurdMixService } from '../curd/CurdMixService';
4
4
  import { IRequestCfgModel, IRequestModel } from '../../libs/crud-pro/interfaces';
5
- import { KeysOfAuthType, KeysOfSimpleSQL } from '../../libs/crud-pro/models/keys';
5
+ import { KeysOfAuthType, KeysOfSimpleSQL, SqlDbType } from '../../libs/crud-pro/models/keys';
6
6
  import { parseJsonObject } from '../../libs/utils/functions';
7
7
  import { ICrudStdAppInfo, ICrudStdAppInfoForSettingKey } from '../../models/bizmodels';
8
8
  import { BizException } from '../../models/devops';
9
9
  import { ExecuteContext } from '../../libs/crud-pro/models/ExecuteContext';
10
- import { SystemTables} from '../../models/SystemTables';
10
+ import { SystemTables } from '../../models/SystemTables';
11
11
  import { CrudStdActionService } from './CrudStdActionService';
12
12
  import { CrudStdRelationService } from './CrudStdRelationService';
13
13
  import * as _ from 'lodash';
14
14
  import { ApiBaseService } from '../base/ApiBaseService';
15
- import {parseDatabaseName} from "../../libs/crud-pro/utils/DatabaseName";
15
+ import { parseDatabaseName } from '../../libs/crud-pro/utils/DatabaseName';
16
16
  import { GLOBAL_STATIC_CONFIG } from '../../libs/global-config/global-config';
17
+ import { MixinUtils } from '@/libs/crud-pro/utils/MixinUtils';
17
18
 
18
19
  export const SPECIAL_SETTING_KEY = {
19
20
  QUERY_LIST: 'QUERY_LIST',
@@ -69,6 +70,9 @@ export class CrudStdService extends ApiBaseService {
69
70
  // 根据用户配置,设置关联查询的数据信息。
70
71
  await this.crudStdRelationService.addCfgModelColumnsRelation(cfgModel, appInfo);
71
72
 
73
+ // 根据表结构的数据类型,修正数据类型
74
+ await this.fixDataFieldTypeBySqlTableField(params, cfgModel, appInfo, { dbType, dbName });
75
+
72
76
  // 业务系统自定义的修改cfgModel
73
77
  await GLOBAL_STATIC_CONFIG.getConfig().bizUpdateCfgModelForCrudStd(params, cfgModel, appInfo, this.ctx);
74
78
 
@@ -138,11 +142,8 @@ export class CrudStdService extends ApiBaseService {
138
142
  * @private
139
143
  */
140
144
  private async getCrudStdAppInfo(appCode?: string) {
141
-
142
145
  const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
143
146
 
144
-
145
-
146
147
  this.logInfo('getCrudStdAppInfo', appCode);
147
148
 
148
149
  const ss = {
@@ -195,4 +196,31 @@ export class CrudStdService extends ApiBaseService {
195
196
  }
196
197
  return true; // 无需鉴权
197
198
  }
199
+
200
+ private async fixDataFieldTypeBySqlTableField(params: IRequestModel, cfgModel: IRequestCfgModel, appInfo: ICrudStdAppInfoForSettingKey, arg3: { dbType: SqlDbType; dbName: string }) {
201
+ const tableFields = _.get(appInfo, 'stdCrudCfgObj.tableFields');
202
+
203
+ const tableFieldMap = MixinUtils.toMap(tableFields, (e: any) => {
204
+ return e.fieldIndex;
205
+ });
206
+
207
+ if (arg3.dbType === SqlDbType.postgres) {
208
+ if (cfgModel.sqlSimpleName === KeysOfSimpleSQL.SIMPLE_INSERT || cfgModel.sqlSimpleName === KeysOfSimpleSQL.SIMPLE_UPDATE) {
209
+ const data = params.data || {};
210
+ const dataKeys = Object.keys(data);
211
+ for (let index = 0; index < dataKeys.length; index++) {
212
+ const dataKey = dataKeys[index];
213
+ const dataValue = data[dataKey];
214
+ const fieldInfo = tableFieldMap[dataKey];
215
+ const fieldType = ('' + _.get(fieldInfo, 'type')).toUpperCase();
216
+ if (fieldType === 'ARRAY') {
217
+ const dataValueArray = parseJsonObject(dataValue);
218
+ if (Array.isArray(dataValueArray)) {
219
+ data[dataKey] = `{${dataValueArray.map(v => `"${v}"`).join(',')}}`;
220
+ }
221
+ }
222
+ }
223
+ }
224
+ }
225
+ }
198
226
  }