Skip to content
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

fix: configuration inject plugin and more in production environment #680

Merged
merged 35 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
f12cd10
refactor: egg load and midway bootstrap
czy88840616 Oct 20, 2020
92c4cb6
feat: add container cache
czy88840616 Oct 21, 2020
692c32c
fix: web refactor case error
czy88840616 Oct 21, 2020
789cfa3
chore: clean code
czy88840616 Oct 21, 2020
1fb3ee5
chore: clean code
czy88840616 Oct 21, 2020
c344f42
fix: app.generateController
czy88840616 Oct 21, 2020
15754f8
test: complete case and enable old suit
czy88840616 Oct 21, 2020
7ca58aa
chore: add @deprecated jsdoc
czy88840616 Oct 22, 2020
1dd84bf
v2.3.17-beta.1
czy88840616 Oct 22, 2020
561ad17
Merge branch '2.x' into fix_egg_loader
czy88840616 Oct 22, 2020
297d419
refactor: use new method to resolve registry
czy88840616 Oct 23, 2020
600e1e5
chore: revert cache
czy88840616 Oct 23, 2020
626d825
v2.3.18-beta.1
czy88840616 Oct 23, 2020
f9daf1e
chore: try add definition meteadata
czy88840616 Oct 23, 2020
2754389
refactor: add cache for definition
czy88840616 Oct 24, 2020
f412a7b
chore: lint
czy88840616 Oct 24, 2020
b9e40e7
chore: update
czy88840616 Oct 24, 2020
35bf565
fix: move cache to midwayContainer
czy88840616 Oct 24, 2020
d5f8030
v2.3.18-beta.2
czy88840616 Oct 24, 2020
44896a5
fix: configuration emit twice in app
czy88840616 Oct 24, 2020
10d06b4
refactor: merge container and midwayContainer
czy88840616 Oct 25, 2020
a17f2c2
fix: lint
czy88840616 Oct 25, 2020
4814910
fix: lint
czy88840616 Oct 25, 2020
6f46753
fix: lint
czy88840616 Oct 25, 2020
bfe1926
refactor: merge loader and base framework
czy88840616 Oct 26, 2020
f7ad592
test: fix case
czy88840616 Oct 26, 2020
e2335ff
test: change lifecycle and pass case
czy88840616 Oct 26, 2020
1afb6db
fix: lifecycle trigger
czy88840616 Oct 26, 2020
595f272
v2.3.18-beta.3
czy88840616 Oct 26, 2020
df60453
fix: configuration trigger
czy88840616 Oct 26, 2020
a0948a7
v2.3.18-beta.4
czy88840616 Oct 26, 2020
92713d8
chore: update tsc
czy88840616 Oct 27, 2020
a603d23
v2.3.18-beta.5
czy88840616 Oct 27, 2020
d9de275
chore: update
czy88840616 Oct 27, 2020
8356e59
v2.3.18-beta.6
czy88840616 Oct 27, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: web refactor case error
  • Loading branch information
czy88840616 committed Oct 21, 2020
commit 692c32c744b7230a1c4a2419b605c344fee7aa92
52 changes: 20 additions & 32 deletions packages/core/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import {
} from '@midwayjs/decorator';
import { IMidwayContainer } from './interface';

import { debuglog } from 'util';
const debugLogger = debuglog('midway:loader');

function buildLoadDir(baseDir, dir) {
if (!path.isAbsolute(dir)) {
return path.join(baseDir, dir);
Expand All @@ -29,10 +26,10 @@ export class ContainerLoader {
disableConflictCheck: boolean;
duplicatedLoader: boolean;

static contextCache = new Map();
static clearContextCache = () => {
ContainerLoader.contextCache.clear();
}
/**
* 单个进程中上一次的 applicationContext
*/
static parentApplicationContext: IMidwayContainer;

constructor({
baseDir,
Expand All @@ -44,22 +41,20 @@ export class ContainerLoader {
this.isTsMode = isTsMode;
this.preloadModules = preloadModules;
this.disableConflictCheck = disableConflictCheck;
this.duplicatedLoader = false;
// this.duplicatedLoader = false;
}

initialize() {
this.pluginContext = new Container(this.baseDir);
if (ContainerLoader.contextCache.has(this.baseDir)) {
// 标识为重复的加载器,只做简单的缓存读取,单进程同一目录只扫描一次
this.duplicatedLoader = true;
this.applicationContext = ContainerLoader.contextCache.get(this.baseDir);
} else {
this.applicationContext = new MidwayContainer(this.baseDir, undefined);
this.applicationContext.disableConflictCheck = this.disableConflictCheck;
this.applicationContext.registerObject('baseDir', this.baseDir);
this.applicationContext.registerObject('isTsMode', this.isTsMode);
ContainerLoader.contextCache.set(this.baseDir, this.applicationContext);
}
this.applicationContext = new MidwayContainer(
this.baseDir,
ContainerLoader.parentApplicationContext
);
this.applicationContext.disableConflictCheck = this.disableConflictCheck;
this.applicationContext.registerObject('baseDir', this.baseDir);
this.applicationContext.registerObject('isTsMode', this.isTsMode);
// 保存到最新的上下文中,供其他容器获取
ContainerLoader.parentApplicationContext = this.applicationContext;
}

getApplicationContext(): IMidwayContainer {
Expand All @@ -86,10 +81,12 @@ export class ContainerLoader {
ignore?: string | string[];
} = {}
) {
if (this.duplicatedLoader) {
debugLogger(`This is a duplicate loader and skip loadDirectory, baseDir=${this.baseDir}`);
return;
}
// if (this.duplicatedLoader) {
// debugLogger(
// `This is a duplicate loader and skip loadDirectory, baseDir=${this.baseDir}`
// );
// return;
// }
if (!this.isTsMode && loadOpts.disableAutoLoad === undefined) {
// disable auto load in js mode by default
loadOpts.disableAutoLoad = true;
Expand Down Expand Up @@ -118,10 +115,6 @@ export class ContainerLoader {

async refresh() {
await this.pluginContext.ready();
if (this.duplicatedLoader) {
debugLogger(`This is a duplicate loader and skip refresh, baseDir=${this.baseDir}`);
return;
}
await this.applicationContext.ready();

// some common decorator implementation
Expand Down Expand Up @@ -160,11 +153,6 @@ export class ContainerLoader {

async stop() {
await this.pluginContext.stop();
if (this.duplicatedLoader) {
debugLogger(`This is a duplicate loader and skip stop, baseDir=${this.baseDir}`);
return;
}
await this.applicationContext.stop();
}

}
2 changes: 1 addition & 1 deletion packages/core/test/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class TestModule {
describe('/test/loader.test.ts', () => {
beforeEach(() => {
clearAllModule();
ContainerLoader.clearContextCache();
ContainerLoader.parentApplicationContext = null;
});
it('should create new loader', async () => {
const loader = new ContainerLoader({
Expand Down
2 changes: 2 additions & 0 deletions packages/mock/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BootstrapStarter } from '@midwayjs/bootstrap';
import {
ContainerLoader,
IMidwayApplication,
IMidwayFramework,
MidwayFrameworkType,
Expand Down Expand Up @@ -32,6 +33,7 @@ export async function create<
): Promise<T> {
process.env.MIDWAY_TS_MODE = 'true';
clearAllModule();
ContainerLoader.parentApplicationContext = null;
let framework: T = null;
let DefaultFramework = null;

Expand Down
2 changes: 1 addition & 1 deletion packages/web/agent.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const { BootstrapStarter } = require('@midwayjs/bootstrap');
const { MidwayWebFramework } = require('./src/framework');
const { MidwayWebFramework } = require('./dist/framework');

class AgentBootHook {
constructor(app) {
Expand Down
15 changes: 9 additions & 6 deletions packages/web/app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const { Bootstrap } = require('@midwayjs/bootstrap');
const { MidwayWebFramework } = require('./src/framework');
const { BootstrapStarter } = require('@midwayjs/bootstrap');
const { MidwayWebFramework } = require('./dist/framework');
const pathMatching = require('egg-path-matching');
const { safelyGet } = require('@midwayjs/core');

Expand All @@ -25,10 +25,13 @@ class AppBootHook {
app: this.app,
globalConfig: this.app.config,
});
Bootstrap.configure({
baseDir: this.app.appDir,
}).load(this.framework);
await Bootstrap.run();
this.bootstrap = new BootstrapStarter();
this.bootstrap
.configure({
baseDir: this.app.appDir,
})
.load(this.framework);
await this.bootstrap.init();
this.app.options['webFramework'] = this.framework;

// register plugin
Expand Down
17 changes: 17 additions & 0 deletions packages/web/app/extend/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
get appOptions() {
return this.options;
},

get midwayWebFramework() {
return this.appOptions['webFramework'];
},

get applicationContext() {
return this.midwayWebFramework.getApplicationContext();
},

get baseDir() {
return this.loader.baseDir;
},
};
118 changes: 45 additions & 73 deletions packages/web/src/application.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { MidwayWebFramework } from './framework';
import { RouterParamValue } from '@midwayjs/decorator';
// import type { MidwayWebFramework } from './framework';
// import { RouterParamValue } from '@midwayjs/decorator';
import { parseNormalDir } from './utils';
import * as extend from 'extend2';
import { EggAppInfo } from 'egg';
import { IMidwayWebApplication } from './interface';
import { join } from 'path';

const {
Expand All @@ -18,24 +17,17 @@ const EGG_PATH = Symbol.for('egg#eggPath');

export const createAppWorkerLoader = AppWorkerLoader => {
class EggAppWorkerLoader extends (AppWorkerLoader as any) {
app: IMidwayWebApplication & {
appOptions: {
typescript?: boolean;
isTsMode?: boolean;
};
appDir: string;
baseDir: string;
};
app: any;

getEggPaths() {
if (!this.appDir) {
// 这里的逻辑是为了兼容老 cluster 模式
if (this.app.appOptions.typescript || this.app.appOptions.isTsMode) {
if (this.app.options.typescript || this.app.options.isTsMode) {
process.env.EGG_TYPESCRIPT = 'true';
}
const result = parseNormalDir(
this.app.appOptions['baseDir'],
this.app.appOptions.isTsMode
this.app.options['baseDir'],
this.app.options.isTsMode
);
this.baseDir = result.baseDir;
this.options.baseDir = this.baseDir;
Expand Down Expand Up @@ -72,12 +64,12 @@ export const createAgentWorkerLoader = AppWorkerLoader => {
class EggAppWorkerLoader extends (AppWorkerLoader as any) {
getEggPaths() {
if (!this.appDir) {
if (this.app.appOptions.typescript || this.app.appOptions.isTsMode) {
if (this.app.options.typescript || this.app.options.isTsMode) {
process.env.EGG_TYPESCRIPT = 'true';
}
const result = parseNormalDir(
this.app.appOptions['baseDir'],
this.app.appOptions.isTsMode
this.app.options['baseDir'],
this.app.options.isTsMode
);
this.baseDir = result.baseDir;
this.options.baseDir = this.baseDir;
Expand Down Expand Up @@ -123,42 +115,42 @@ export const createEggApplication = Application => {
get [EGG_PATH]() {
return __dirname;
}

get appOptions() {
return this.options;
}

get midwayWebFramework(): MidwayWebFramework {
return this.appOptions['webFramework'];
}

get applicationContext() {
return this.midwayWebFramework.getApplicationContext();
}

getApplicationContext() {
return this.applicationContext;
}

generateController(
controllerMapping: string,
routeArgsInfo?: RouterParamValue[],
routerResponseData?: any[]
) {
return this.midwayWebFramework.generateController(
controllerMapping,
routeArgsInfo,
routerResponseData
);
}

async generateMiddleware(middlewareId: string) {
return this.midwayWebFramework.generateMiddleware(middlewareId);
}

get baseDir() {
return this.loader.baseDir;
}
//
// get appOptions() {
// return this.options;
// }
//
// get midwayWebFramework(): MidwayWebFramework {
// return this.appOptions['webFramework'];
// }
//
// get applicationContext() {
// return this.midwayWebFramework.getApplicationContext();
// }
//
// getApplicationContext() {
// return this.applicationContext;
// }
//
// generateController(
// controllerMapping: string,
// routeArgsInfo?: RouterParamValue[],
// routerResponseData?: any[]
// ) {
// return this.midwayWebFramework.generateController(
// controllerMapping,
// routeArgsInfo,
// routerResponseData
// );
// }
//
// async generateMiddleware(middlewareId: string) {
// return this.midwayWebFramework.generateMiddleware(middlewareId);
// }
//
// get baseDir() {
// return this.loader.baseDir;
// }
}

return EggApplication as any;
Expand All @@ -178,26 +170,6 @@ export const createEggAgent = Agent => {
get [EGG_PATH]() {
return __dirname;
}

get appOptions() {
return this.options;
}

get midwayWebFramework(): MidwayWebFramework {
return this.appOptions['webFramework'];
}

get applicationContext() {
return this.midwayWebFramework.getApplicationContext();
}

getApplicationContext() {
return this.applicationContext;
}

get baseDir() {
return this.loader.baseDir;
}
}

return EggAgent as any;
Expand Down
10 changes: 9 additions & 1 deletion packages/web/src/devFramework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ export class MidwayDevFramework
return MidwayFrameworkType.WEB;
}

public async run(): Promise<void> {}
public async run(): Promise<void> {
if (this.configurationOptions.port) {
new Promise(resolve => {
this.app.listen(this.configurationOptions.port, () => {
resolve();
});
});
}
}

configure(options: IMidwayWebConfigurationOptions): MidwayDevFramework {
this.configurationOptions = options;
Expand Down
15 changes: 3 additions & 12 deletions packages/web/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class MidwayWebFramework extends MidwayKoaBaseFramework<
this.getApplicationContext()
.getConfigService()
.addObject(this.configurationOptions.globalConfig);

console.log('4444444');
Object.defineProperty(this.app, 'config', {
get() {
return self.getConfiguration();
Expand Down Expand Up @@ -83,8 +83,7 @@ export class MidwayWebFramework extends MidwayKoaBaseFramework<

protected async afterInitialize(
options: Partial<IMidwayBootstrapOptions>
): Promise<void> {
}
): Promise<void> {}

public getApplication(): Application {
return this.app;
Expand All @@ -97,15 +96,7 @@ export class MidwayWebFramework extends MidwayKoaBaseFramework<
/**
* 这个方法 egg-cluster 不走,只有单进程模式使用 @midwayjs/bootstrap 才会执行
*/
public async run(): Promise<void> {
if (this.configurationOptions.port) {
new Promise(resolve => {
this.app.listen(this.configurationOptions.port, () => {
resolve();
});
});
}
}
public async run(): Promise<void> {}

/**
* 这个方法 egg-cluster 不走,只有单进程模式使用 @midwayjs/bootstrap 才会执行
Expand Down
Loading