-
Notifications
You must be signed in to change notification settings - Fork 568
/
lifeCycleService.ts
208 lines (181 loc) · 5.51 KB
/
lifeCycleService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import { ILifeCycle, IMidwayContainer, ScopeEnum } from '../interface';
import {
CONFIGURATION_KEY,
Init,
Inject,
listModule,
Provide,
Scope,
} from '../decorator';
import { FunctionalConfiguration } from '../functional/configuration';
import { MidwayFrameworkService } from './frameworkService';
import { MidwayConfigService } from './configService';
import { debuglog } from 'util';
import { MidwayMockService } from './mockService';
import { MidwayHealthService } from './healthService';
const debug = debuglog('midway:debug');
@Provide()
@Scope(ScopeEnum.Singleton)
export class MidwayLifeCycleService {
@Inject()
protected frameworkService: MidwayFrameworkService;
@Inject()
protected configService: MidwayConfigService;
@Inject()
protected mockService: MidwayMockService;
@Inject()
protected healthService: MidwayHealthService;
private lifecycleInstanceList: Array<{
target: any;
namespace: string;
instance?: any;
}> = [];
constructor(readonly applicationContext: IMidwayContainer) {}
@Init()
protected async init() {
// exec simulator init
await this.mockService.initSimulation();
// run lifecycle
const cycles = listModule(CONFIGURATION_KEY) as Array<{
target: any;
namespace: string;
instance?: any;
}>;
debug(`[core]: Found Configuration length = ${cycles.length}`);
for (const cycle of cycles) {
if (cycle.target instanceof FunctionalConfiguration) {
// 函数式写法
cycle.instance = cycle.target;
} else {
// 普通类写法
debug(`[core]: Lifecycle run ${cycle.namespace} init`);
cycle.instance = await this.applicationContext.getAsync<ILifeCycle>(
cycle.target
);
}
cycle.instance && this.lifecycleInstanceList.push(cycle);
}
// init health check service
await this.healthService.init(this.lifecycleInstanceList);
// bind object lifecycle
await Promise.all([
this.runObjectLifeCycle(
this.lifecycleInstanceList,
'onBeforeObjectCreated'
),
this.runObjectLifeCycle(this.lifecycleInstanceList, 'onObjectCreated'),
this.runObjectLifeCycle(this.lifecycleInstanceList, 'onObjectInit'),
this.runObjectLifeCycle(
this.lifecycleInstanceList,
'onBeforeObjectDestroy'
),
]);
// bind framework lifecycle
// onAppError
// exec onConfigLoad()
await this.runContainerLifeCycle(
this.lifecycleInstanceList,
'onConfigLoad',
configData => {
if (configData) {
this.configService.addObject(configData);
}
}
);
await this.mockService.runSimulatorSetup();
// exec onReady()
await this.runContainerLifeCycle(this.lifecycleInstanceList, 'onReady');
// exec framework.run()
await this.frameworkService.runFramework();
// exec onServerReady()
await this.runContainerLifeCycle(
this.lifecycleInstanceList,
'onServerReady'
);
// clear config merge cache
if (!this.configService.getConfiguration('debug.recordConfigMergeOrder')) {
this.configService.clearConfigMergeOrder();
}
}
public async stop() {
await this.mockService.runSimulatorTearDown();
// stop lifecycle
const cycles = listModule(CONFIGURATION_KEY) || [];
for (const cycle of cycles.reverse()) {
let inst;
if (cycle.target instanceof FunctionalConfiguration) {
// 函数式写法
inst = cycle.target;
} else {
inst = await this.applicationContext.getAsync<ILifeCycle>(cycle.target);
}
await this.runContainerLifeCycle(inst, 'onStop');
}
// stop framework
await this.frameworkService.stopFramework();
}
/**
* run some lifecycle in configuration
* @param lifecycleInstanceOrList
* @param lifecycle
* @param resultHandler
*/
private async runContainerLifeCycle(
lifecycleInstanceOrList,
lifecycle,
resultHandler?: (result: any) => void
) {
if (Array.isArray(lifecycleInstanceOrList)) {
for (const cycle of lifecycleInstanceOrList) {
if (typeof cycle.instance[lifecycle] === 'function') {
debug(
`[core]: Lifecycle run ${cycle.instance.constructor.name} ${lifecycle}`
);
const result = await cycle.instance[lifecycle](
this.applicationContext,
this.frameworkService.getMainApp()
);
if (resultHandler) {
resultHandler(result);
}
}
}
} else {
if (typeof lifecycleInstanceOrList[lifecycle] === 'function') {
debug(
`[core]: Lifecycle run ${lifecycleInstanceOrList.constructor.name} ${lifecycle}`
);
const result = await lifecycleInstanceOrList[lifecycle](
this.applicationContext,
this.frameworkService.getMainApp()
);
if (resultHandler) {
resultHandler(result);
}
}
}
}
/**
* run object lifecycle
* @param lifecycleInstanceList
* @param lifecycle
*/
private async runObjectLifeCycle(
lifecycleInstanceList: any[],
lifecycle: string
) {
for (const cycle of lifecycleInstanceList) {
if (typeof cycle.instance[lifecycle] === 'function') {
debug(
`[core]: Lifecycle run ${cycle.instance.constructor.name} ${lifecycle}`
);
return this.applicationContext[lifecycle](
cycle.instance[lifecycle].bind(cycle.instance)
);
}
}
}
public getLifecycleInstanceList() {
return this.lifecycleInstanceList;
}
}