-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] egg-core增加应用启动阶段 #2520
Comments
Translation of this issue: [RFC] egg-core increase appReadyBackground descriptionSome plugins may need to be asynchronous before they can be loaded. They need to use At the same time, before the application starts, if such a plugin is used in E.g: // pluginA/index.js
app.beforeStart(async ()=>{
Const bar = await pull();
app.addSingleton('foo', (config,app) => {
Return new Foo(bar);
});
}); // app.js
app.beforeStart(async ()=>{
Await app.foo.foooo(); // app.foo may not be injected yet
}); solutionAdd appReady to beforeAppStart(scope) {
Const done = this.appReady.readyCallback(name);
process.nextTick(() => {
This.ready()
.then(()=>utils.callFn(scope))
.then(() => done(), done);
});
} The server should also be modified to ʻapp.appReady.ready(startServer);` |
app.appReady 下面还有几个 API? |
|
@killagu
|
@tong3jie 你讲的没错。 但是我讲的情况都是在应用启动前的, |
addSingleton had support async since last month |
两个想法
|
|
可能还可以有 health checker |
implement checker loader, load `app/checker`. Checkers will be called after all readyCallbacks called, then emit `app.ready(true)`. Refs: eggjs/egg#2520
boot +1 |
boot + 1 |
starter 呢? |
直觉让我反对新加一个目录 |
新加目录比新加 API 友好些,比如 schedule,notify 都是新增目录的 |
这个功能比较反对新加目录的原因主要还是这些功能都是应用开发者基本用不到的。 egg 的启动,有点像 react 的生命周期的意思了。是否应该再完善一点,把启动时期的各个生命周期给定义下来:
|
life cycle +1 |
而且 issue 中描述的这个问题,通过新增一个应用启动阶段,看似可以解决当下的问题,但是其实是在埋坑,最多只能算是一个临时解决方案。现在如果 B 依赖 A,把 B 放在启动阶段,A 放在 beforeStart 是可以解决了,那如果出现了 C 依赖 B, B 依赖 A 呢?C 放哪里? 本质上这还是执行顺序的问题,想清楚之前不建议加新的 |
如果 C 和 B 都是应用定义的话,那在应用里面可以写在一起。 本质还是应用开发者对 beforeStart 的真正启动顺序不理解,不改变 beforeStart 的前提下,需要提供应用启动的环节。增加 API 和增加目录都是具体的解决方案,应用启动的这个环节是要真正增加的。 |
现在给应用层的 app.js 实在是太尴尬了。90% 的人是用做应用启动阶段的,但是还有部分的人在里面操作 middleware,这个又不能算启动阶段。 |
我本身是不太建议在应用层用 app.js 的,因为对传统开发者来说这就是入口文件,感觉对顺序还是比较难理解的。 |
关于生命周期这个, 我觉得是这样的:
现在 要把原来的这些方法改造相比于增加一个目录来说成本太大了。 |
同意,但是觉得没必要像 react 那么复杂 |
我能接受的三个方案 1. 不改 API应用里面的
2. 加启动目录如上描述 3. 规范插件,保证 app 上的实例挂载是同步的,通过 app.xxx.ready 来管理依赖之前有问题的 case 是: app.beforeStart(async function() {
// 异步操作
await fetchPWD();
app.addSingleton('mysql', () => {
// ...
return instance;
});
}); 可以改成如下,确保 app.mysql 一开始就有 app.beforeStart(async function() {
app.addSingleton('mysql', async () => {
// 异步操作
await fetchPWD();
// ...
return instance;
});
}); |
@okoala 提供的示例代码中 面向应用开发者提供一种实现方式,达到统一的规范。 还有没看懂 运行时如何调用是实现细节了, 就不在此处讨论了。 |
@killagu |
@okoala 你这样说就好像 |
1. add `Boot` Class, developer can inherit from it. 2. add `startBoot`, application like egg should call `startBoot` after all files loaded. Refs: eggjs/egg#2520
1. add `Boot` Class, developer can inherit from it. 2. add `startBoot`, application like egg should call `startBoot` after all files loaded. Refs: eggjs/egg#2520
启动时间这里也需要加下打点, #1898 (comment) |
1. add `Boot` Class, developer can inherit from it. 2. add `startBoot`, application like egg should call `startBoot` after all files loaded. Refs: eggjs/egg#2520
1. add `Boot` Class, developer can inherit from it. 2. add `startBoot`, application like egg should call `startBoot` after all files loaded. Refs: eggjs/egg#2520
1. add `Boot` Class, developer can inherit from it. 2. add `startBoot`, application like egg should call `startBoot` after all files loaded. Refs: eggjs/egg#2520
1. add `Boot` Class, developer can inherit from it. 2. add `startBoot`, application like egg should call `startBoot` after all files loaded. Refs: eggjs/egg#2520
1. Add `Lifecycle` class, control EggCore lifecycle. 2. After config, extend, app.js, angent.js loaded, loader should call `initBoots`. 3. After all files loaded, loader should call `triggerDidLoad`. Refs: eggjs/egg#2520
文档是不是也要改改 |
@Maledong // app.js
module.exports = app => {
// e.g. modify core middlewares order
}; It have be done before other files load, so the modifications can take effect. |
1. Add `Lifecycle` class, control EggCore lifecycle. 2. After config, extend, app.js, angent.js loaded, loader should call `initBoots`. 3. After all files loaded, loader should call `triggerDidLoad`. Refs: eggjs/egg#2520
背景描述
有的插件可能需要异步操作后才能加载, 需要使用
beforeStart
来执行异步操作,挂载到
app
上。同时应用启动前, 又在
beforeStart
中使用了这样的插件, 就可能不能正常启动。例如:
解决方案
在EggCore
上增加appReady。服务器启动时也应该修改为app.appReady.ready(startServer);
定义一个新目录, 暂时命名为
app/checker
。在应用启动阶段(所有的
beforeStart
都执行完毕),调用
app/checker
目录下的checker
实现, 调用完成后调用通过app.ready
注册的回调。现征集该目录的命名, 暂有两个选项:
cc: @gxcsoccer @XadillaX @popomore @coolme200
The text was updated successfully, but these errors were encountered: