Skip to content

Commit

Permalink
fix: delay load without layer and egg-cluster (#985)
Browse files Browse the repository at this point in the history
* fix: delay load without layer and egg-cluster

* fix: listener after app close

* fix: lint

* chore: clean resource when stop app

* fix: lint

* chore: add output

* fix: clear set
  • Loading branch information
czy88840616 authored Apr 13, 2021
1 parent 170d58e commit 52ba60d
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 8 deletions.
2 changes: 2 additions & 0 deletions packages/bootstrap/src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export class BootstrapStarter {

public async stop() {
await Promise.all(this.getActions('stop', {}));
global['MIDWAY_BOOTSTRAP_APP_READY'] = false;
}

public getActions(action: string, args?): any[] {
Expand Down Expand Up @@ -256,6 +257,7 @@ export class Bootstrap {

static async stop() {
await this.getStarter().stop();
this.reset();
}

static reset() {
Expand Down
29 changes: 28 additions & 1 deletion packages/midway-schedule/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,47 @@ export = agent => {
return;
}

const STRATEGY = Object.getOwnPropertySymbols(agent.schedule)[0];
const STRATEGY_INSTANCE = Object.getOwnPropertySymbols(agent.schedule)[1];

// ugly!! just support all and worker strategy
class AllStrategy extends agent['TimerScheduleStrategy'] {
timer;
handler() {
this.sendAll();
}

safeTimeout(...args) {
this.timer = super.safeTimeout(...args);
return this.timer;
}
close() {
clearTimeout(this.timer);
}
}

class WorkerStrategy extends agent['TimerScheduleStrategy'] {
timer;
handler() {
this.sendOne();
}
safeTimeout(...args) {
this.timer = super.safeTimeout(...args);
return this.timer;
}
close() {
clearTimeout(this.timer);
}
}

const strategyMap = new Map();
agent.schedule.close = () => {
agent.schedule.closed = true;
for (const instance of agent.schedule[STRATEGY_INSTANCE].values()) {
instance.close();
}
};

const strategyMap = agent.schedule[STRATEGY];
strategyMap.set('worker', WorkerStrategy);
strategyMap.set('all', AllStrategy);

Expand Down
7 changes: 4 additions & 3 deletions packages/midway-schedule/test/schedule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ describe('test/schedule.test.ts', () => {
await application.runSchedule('intervalCron#IntervalCron');
await application.runSchedule(IntervalCron2 as any);
await sleep(1000);
const log = getLogContent('worker-other');

// console.log(log);
expect(contains(log, 'hello decorator')).toEqual(1);
expect(contains(log, 'hello decorator2')).toEqual(1);
const log = getLogContent('worker-other');
expect(contains(log, 'hello decorator')).toBeGreaterThanOrEqual(1);
expect(contains(log, 'hello decorator2')).toBeGreaterThanOrEqual(1);
await closeApp(application);
});
});
Expand Down
2 changes: 2 additions & 0 deletions packages/midway-schedule/test/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { IMidwayWebConfigurationOptions, Framework } from '../../web/src';
import { join } from 'path';
import { createApp, close } from '@midwayjs/mock';
import { remove } from 'fs-extra';

export async function create(name, options: IMidwayWebConfigurationOptions = {}) {
const baseDir = join(__dirname, 'fixtures', name);
await remove(join(baseDir, 'logs'));
return createApp<Framework>(baseDir, options, Framework)
}

Expand Down
1 change: 1 addition & 0 deletions packages/mock/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export async function create<
clearAllModule();
}
lastAppDir = appDir;
global['MIDWAY_BOOTSTRAP_APP_SET'].clear();
clearContainerCache();
clearAllLoggers();

Expand Down
1 change: 1 addition & 0 deletions packages/rabbitmq/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class MidwayRabbitMQFramework extends BaseFramework<

public async run(): Promise<void> {
await Promise.all(this.consumerList);
this.logger.info('Rabbitmq server start success');
}

protected async beforeStop(): Promise<void> {
Expand Down
6 changes: 6 additions & 0 deletions packages/socketio/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,19 @@ export class MidwaySocketIOFramework extends BaseFramework<
this.applicationContext.get(HTTP_SERVER_KEY),
this.configurationOptions
);
this.logger.info(
'Socket.io server start success and attach to web server'
);
} else {
// listen port when http server not exist
if (this.configurationOptions.port) {
this.app.listen(
this.configurationOptions.port,
this.configurationOptions
);
this.logger.info(
`Socket.io server port = ${this.configurationOptions.port} start success`
);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"typings": "dist/index.d.ts",
"scripts": {
"build": "midway-bin build -c",
"test": "midway-bin test --ts --forceExit",
"cov": "midway-bin cov --ts --forceExit",
"test": "midway-bin test --ts",
"cov": "midway-bin cov --ts",
"link": "npm link"
},
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export const createAppWorkerLoader = () => {
})
.load(this.framework);

if (this.app.options['mode'] !== 'single') {
if (this.app.options['midwaySingleton'] !== true) {
// 这个代码只会在 egg-cluster 模式下执行
this.app.beforeStart(async () => {
await this.bootstrap.init();
Expand Down
8 changes: 7 additions & 1 deletion packages/web/src/framework/singleProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { MidwayKoaContextLogger } from '@midwayjs/koa';
export class MidwayWebSingleProcessFramework
implements IMidwayFramework<Application, IMidwayWebConfigurationOptions> {
public app: Application;
public agent;
public configurationOptions: IMidwayWebConfigurationOptions;
private isTsMode: boolean;
private server: Server;
Expand Down Expand Up @@ -68,11 +69,12 @@ export class MidwayWebSingleProcessFramework
mode: 'single',
isTsMode: this.isTsMode || true,
applicationContext: options.applicationContext,
midwaySingleton: true,
};

const Agent = require(opts.framework).Agent;
const Application = require(opts.framework).Application;
const agent = new Agent(Object.assign({}, opts));
const agent = (this.agent = new Agent(Object.assign({}, opts)));
await agent.ready();
const application = (this.app = new Application(Object.assign({}, opts)));
application.agent = agent;
Expand Down Expand Up @@ -118,7 +120,11 @@ export class MidwayWebSingleProcessFramework
}

async stop(): Promise<void> {
await new Promise(resolve => {
this.server.close(resolve);
});
await this.app.close();
await this.agent.close();
}

getBaseDir(): string {
Expand Down
1 change: 1 addition & 0 deletions packages/web/test/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('test/logger.test.js', () => {
mm(process.env, 'EGG_SERVER_ENV', 'local');
mm(process.env, 'EGG_LOG', 'WARN');
const logsDir = join(__dirname, 'fixtures/apps/mock-dev-app-logger/logs/ali-demo');
await remove(logsDir);
await ensureDir(logsDir);
const app = await creatApp('apps/mock-dev-app-logger', { cleanLogsDir: false});
app.coreLogger.warn('custom content');
Expand Down

0 comments on commit 52ba60d

Please sign in to comment.