@hile/core 1.0.20 → 1.0.21
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/README.md +4 -2
- package/SKILL.md +2 -0
- package/dist/index.js +9 -3
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -94,7 +94,7 @@ export const userService = defineService(async (shutdown) => {
|
|
|
94
94
|
|
|
95
95
|
### 5) 资源销毁(Shutdown)
|
|
96
96
|
|
|
97
|
-
|
|
97
|
+
当服务初始化失败或手动执行全局关闭时,容器会按规则执行已注册的清理回调。`container.shutdown()` 会循环处理队列直到清空,并让出一次事件循环(`setImmediate`),确保在 shutdown 进行中才完成启动并注册的 teardown 也会被执行。
|
|
98
98
|
|
|
99
99
|
```typescript
|
|
100
100
|
export const connectionService = defineService(async (shutdown) => {
|
|
@@ -168,11 +168,13 @@ const startupOrder = container.getStartupOrder()
|
|
|
168
168
|
import container from '@hile/core'
|
|
169
169
|
|
|
170
170
|
process.on('SIGTERM', async () => {
|
|
171
|
-
await container.shutdown()
|
|
171
|
+
await container.shutdown() // 执行所有已注册的 teardown,含晚注册的
|
|
172
172
|
process.exit(0)
|
|
173
173
|
})
|
|
174
174
|
```
|
|
175
175
|
|
|
176
|
+
保证:每个在 defineService 中通过 `shutdown(fn)` 注册的回调都会在 `shutdown()` 时被执行;若某服务在 shutdown 期间才完成启动并调用 `shutdown(fn)`,也会在下一轮事件循环中被关掉。
|
|
177
|
+
|
|
176
178
|
### 9) 服务校验(isService)
|
|
177
179
|
|
|
178
180
|
```typescript
|
package/SKILL.md
CHANGED
|
@@ -16,6 +16,7 @@ description: "@hile/core 的代码生成与使用规范。适用于定义/加载
|
|
|
16
16
|
5. 禁止在模块顶层缓存 `await loadService(...)` 结果。
|
|
17
17
|
6. 依赖服务必须在服务函数内部加载。
|
|
18
18
|
7. 多个 teardown 默认按 LIFO 顺序执行。
|
|
19
|
+
8. `container.shutdown()` 必须执行所有已通过 `shutdown(fn)` 注册的回调;在 shutdown 进行中才完成启动并调用 `curDown` 的服务,也会在让出一次事件循环后被关掉(避免竞态漏关)。
|
|
19
20
|
|
|
20
21
|
## 2. 生命周期与超时约束
|
|
21
22
|
|
|
@@ -110,3 +111,4 @@ export const good = defineService(async (shutdown) => {
|
|
|
110
111
|
- [ ] teardown 抛错是否不覆盖原始业务错误
|
|
111
112
|
- [ ] 并发 resolve 同一服务是否只初始化一次
|
|
112
113
|
- [ ] shutdown 重复调用是否幂等
|
|
114
|
+
- [ ] shutdown 期间才注册的 teardown 是否在一次 setImmediate 让出后被正确执行
|
package/dist/index.js
CHANGED
|
@@ -238,9 +238,15 @@ export class Container {
|
|
|
238
238
|
async shutdown() {
|
|
239
239
|
const startedAt = Date.now();
|
|
240
240
|
this.emit({ type: 'container:shutdown:start' });
|
|
241
|
-
|
|
242
|
-
while (
|
|
243
|
-
|
|
241
|
+
// 循环直到队列清空;再让出一次事件循环,处理「shutdown 期间才调用 curDown」的晚注册 teardown
|
|
242
|
+
while (true) {
|
|
243
|
+
while (this.shutdownQueues.length > 0) {
|
|
244
|
+
const id = this.shutdownQueues[this.shutdownQueues.length - 1];
|
|
245
|
+
await this.shutdownService(id);
|
|
246
|
+
}
|
|
247
|
+
await new Promise(r => setImmediate(r));
|
|
248
|
+
if (this.shutdownQueues.length === 0)
|
|
249
|
+
break;
|
|
244
250
|
}
|
|
245
251
|
this.shutdownFunctions.clear();
|
|
246
252
|
this.shutdownQueues.length = 0;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hile/core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.21",
|
|
4
4
|
"description": "Hile core - lightweight async service container",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -24,5 +24,5 @@
|
|
|
24
24
|
"fix-esm-import-path": "^1.10.3",
|
|
25
25
|
"vitest": "^4.0.18"
|
|
26
26
|
},
|
|
27
|
-
"gitHead": "
|
|
27
|
+
"gitHead": "f5d59f9f131456cf14e5ca62eeea6becde84ebef"
|
|
28
28
|
}
|