web-infra-dev / modern.js

A progressive web framework based on React and Rsbuild.
https://modernjs.dev/en/
MIT License
4.45k stars 367 forks source link

@modern-js/plugin-egg 这个插件中 egg 的 load 生命周期触发了两次 #339

Closed nervouself closed 2 years ago

nervouself commented 2 years ago

node version:14.18.1 pnpm version:6.22.1

@modern-js/plugin-egg/src/framework/index.ts:22

第 22 行 super.load() 打 log 可以发现调用了两次,两次 trace:

Trace: ModernJsLoader load
    at /common/temp/node_modules/.pnpm/@modern-js+plugin-egg@1.2.0_egg@2.32.0+typescript@4.4.4/node_modules/@modern-js/plugin-egg/dist/js/node/framework/index.js:34:15
    at ModernJsLoader.mockBaseDir (/common/temp/node_modules/.pnpm/@modern-js+plugin-egg@1.2.0_egg@2.32.0+typescript@4.4.4/node_modules/@modern-js/plugin-egg/dist/js/node/framework/index.js:41:5)
    at ModernJsLoader.load (/common/temp/node_modules/.pnpm/@modern-js+plugin-egg@1.2.0_egg@2.32.0+typescript@4.4.4/node_modules/@modern-js/plugin-egg/dist/js/node/framework/index.js:33:10)
    at new Agent (/common/temp/node_modules/.pnpm/egg@2.32.0/node_modules/egg/lib/agent.js:24:17)
    at new Agent (/common/temp/node_modules/.pnpm/@modern-js+plugin-egg@1.2.0_egg@2.32.0+typescript@4.4.4/node_modules/@modern-js/plugin-egg/dist/js/node/framework/index.js:113:1)
    at initApp (/common/temp/node_modules/.pnpm/@modern-js+plugin-egg@1.2.0_egg@2.32.0+typescript@4.4.4/node_modules/@modern-js/plugin-egg/dist/js/node/plugin.js:74:17)
    at prepareApiServer (/common/temp/node_modules/.pnpm/@modern-js+plugin-egg@1.2.0_egg@2.32.0+typescript@4.4.4/node_modules/@modern-js/plugin-egg/dist/js/node/plugin.js:165:23)
    at /common/temp/node_modules/.pnpm/@modern-js+plugin@1.1.0/node_modules/@modern-js/plugin/dist/js/node/pipeline/async.js:57:61
    at run (/common/temp/node_modules/.pnpm/@modern-js+plugin@1.1.0/node_modules/@modern-js/plugin/dist/js/node/hook.js:45:14)
    at /common/temp/node_modules/.pnpm/@modern-js+plugin@1.1.0/node_modules/@modern-js/plugin/dist/js/node/pipeline/async.js:57:48

Trace: ModernJsLoader load
    at /common/temp/node_modules/.pnpm/@modern-js+plugin-egg@1.2.0_egg@2.32.0+typescript@4.4.4/node_modules/@modern-js/plugin-egg/dist/js/node/framework/index.js:34:15
    at ModernJsLoader.mockBaseDir (/common/temp/node_modules/.pnpm/@modern-js+plugin-egg@1.2.0_egg@2.32.0+typescript@4.4.4/node_modules/@modern-js/plugin-egg/dist/js/node/framework/index.js:41:5)
    at ModernJsLoader.load (/common/temp/node_modules/.pnpm/@modern-js+plugin-egg@1.2.0_egg@2.32.0+typescript@4.4.4/node_modules/@modern-js/plugin-egg/dist/js/node/framework/index.js:33:10)
    at new Application (/common/temp/node_modules/.pnpm/egg@2.32.0/node_modules/egg/lib/application.js:66:19)
    at new Application (/common/temp/node_modules/.pnpm/@modern-js+plugin-egg@1.2.0_egg@2.32.0+typescript@4.4.4/node_modules/@modern-js/plugin-egg/dist/js/node/framework/index.js:102:1)
    at initApp (/common/temp/node_modules/.pnpm/@modern-js+plugin-egg@1.2.0_egg@2.32.0+typescript@4.4.4/node_modules/@modern-js/plugin-egg/dist/js/node/plugin.js:76:23)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at prepareApiServer (/common/temp/node_modules/.pnpm/@modern-js+plugin-egg@1.2.0_egg@2.32.0+typescript@4.4.4/node_modules/@modern-js/plugin-egg/dist/js/node/plugin.js:165:17)
    at /common/temp/node_modules/.pnpm/@modern-js+plugin@1.1.0/node_modules/@modern-js/plugin/dist/js/node/pipeline/async.js:57:20
    at /common/temp/node_modules/.pnpm/@modern-js+plugin@1.1.0/node_modules/@modern-js/plugin/dist/js/node/pipeline/async.js:57:20

现象是会导致 orm 插件对同一个数据库连两次然后报错退出,具体为什么会触发两次 load 还没有 debug 到

yimingjfe commented 2 years ago

这里执行两次是因为分别初始化了 Application 和 Agent 的实例,是符合预期的;具体使用的是哪个 orm 插件呢?

nervouself commented 2 years ago

typeorm,在同名链接被创建的时候会报错退出

nervouself commented 2 years ago

agent 应该和 app 不在同一个进程?

yimingjfe commented 2 years ago

agent 应该和 app 不在同一个进程?

Modern.js 中 agent 和 app 是在同一个进程,考虑到使用容器化技术之后;已经不需要在框架层面做进程管理,单进程对 Modern.js 支持不同框架也更加友好。

复现的仓库能给一下吗?

nervouself commented 2 years ago

orm-test.zip

需要改下数据库配置再起

另,agent 设计原因就是起个新的进程干点公共的事,要是不新起进程的话感觉就失去意义了

yimingjfe commented 2 years ago

orm-test.zip

需要改下数据库配置再起

另,agent 设计原因就是起个新的进程干点公共的事,要是不新起进程的话感觉就失去意义了

这里支持 agent 是为了兼容 egg 生态圈的工具,单进程的话,agent 确实是不需要的。