新增全局事件,多进程通信,优化启动速度和开发监听

This commit is contained in:
cool 2024-03-20 21:33:58 +08:00
parent 791527c2f0
commit 651ffe8b76
5 changed files with 73 additions and 38 deletions

View File

@ -4,7 +4,7 @@
"description": "一个项目用COOL就够了", "description": "一个项目用COOL就够了",
"private": true, "private": true,
"dependencies": { "dependencies": {
"@cool-midway/core": "^7.1.10", "@cool-midway/core": "^7.1.11",
"@cool-midway/rpc": "^7.0.0", "@cool-midway/rpc": "^7.0.0",
"@cool-midway/task": "^7.0.0", "@cool-midway/task": "^7.0.0",
"@midwayjs/bootstrap": "^3.15.0", "@midwayjs/bootstrap": "^3.15.0",
@ -61,8 +61,7 @@
"ci": "npm run cov", "ci": "npm run cov",
"build": "mwtsc --cleanOutDir", "build": "mwtsc --cleanOutDir",
"pm2:start": "pm2 start ./bootstrap.js -i max --name cool-admin", "pm2:start": "pm2 start ./bootstrap.js -i max --name cool-admin",
"pm2:stop": "pm2 stop cool-admin & pm2 delete cool-admin", "pm2:stop": "pm2 stop cool-admin & pm2 delete cool-admin"
"pm2:debug": "pm2-runtime start ./bootstrap.js -i max --name cool-admin"
}, },
"midway-bin-clean": [ "midway-bin-clean": [
".vscode/.tsbuildinfo", ".vscode/.tsbuildinfo",

View File

@ -21,21 +21,38 @@ export class BaseAppEvent {
@Event('onServerReady') @Event('onServerReady')
async onServerReady() { async onServerReady() {
this.checkConfig();
}
/**
*
*/
async checkConfig() {
if (this.config.base.jwt.secret == 'cool-admin-xxxxxx') { if (this.config.base.jwt.secret == 'cool-admin-xxxxxx') {
const filePath = path.join( this.coreLogger.warn(
this.app.getBaseDir(), '检测到模块[base] jwt.secret 配置是默认值,即将自动修改...'
'modules',
'base',
'config.ts'
);
// 替换文件内容
let fileData = fs.readFileSync(filePath, 'utf8');
const secret = uuid().replace(/-/g, '');
this.config.base.jwt.secret = secret;
fs.writeFileSync(filePath, fileData.replace('cool-admin-xxxxxx', secret));
this.coreLogger.info(
'\x1B[36m [cool:module:base] midwayjs cool module base auto modify jwt.secret\x1B[0m'
); );
setTimeout(() => {
const filePath = path.join(
this.app.getBaseDir(),
'..',
'src',
'modules',
'base',
'config.ts'
);
// 替换文件内容
let fileData = fs.readFileSync(filePath, 'utf8');
const secret = uuid().replace(/-/g, '');
this.config.base.jwt.secret = secret;
fs.writeFileSync(
filePath,
fileData.replace('cool-admin-xxxxxx', secret)
);
this.coreLogger.info(
'\x1B[36m [cool:module:base] midwayjs cool module base auto modify jwt.secret\x1B[0m'
);
}, 6000);
} }
} }
} }

View File

@ -13,9 +13,15 @@ export class OpenDemoEventController extends BaseController {
@Inject() @Inject()
coolEventManager: CoolEventManager; coolEventManager: CoolEventManager;
@Post('/send') @Post('/comm', { summary: '普通事件,本进程生效' })
async send() { async comm() {
await this.coolEventManager.emit('demo', { a: 1 }, 1); await this.coolEventManager.emit('demo', { a: 2 }, 1);
return this.ok();
}
@Post('/global', { summary: '全局事件,多进程都有效' })
async global() {
await this.coolEventManager.globalEmit('demo', false, { a: 2 }, 1);
return this.ok(); return this.ok();
} }
} }

View File

@ -3,19 +3,18 @@ import { CoolEvent, Event } from '@cool-midway/core';
import { Scope, ScopeEnum } from '@midwayjs/core'; import { Scope, ScopeEnum } from '@midwayjs/core';
/** /**
* *
*/ */
@Provide()
@Scope(ScopeEnum.Singleton)
@CoolEvent() @CoolEvent()
export class DemoEvent { export class DemoCommEvent {
/** /**
* *
* @param msg * @param msg
* @param a * @param a
*/ */
@Event('demo') @Event('demo')
async updatdemoeUser(msg, a) { async demo(msg, a) {
console.log('收到消息', msg, a); console.log(`comm当前进程的ID是: ${process.pid}`);
console.log('comm收到消息', msg, a);
} }
} }

View File

@ -21,21 +21,35 @@ export class UserAppEvent {
@Event('onServerReady') @Event('onServerReady')
async onServerReady() { async onServerReady() {
this.checkConfig();
}
/**
*
*/
async checkConfig() {
if (this.config.user.jwt.secret == 'cool-app-xxxxxx') { if (this.config.user.jwt.secret == 'cool-app-xxxxxx') {
const filePath = path.join( this.coreLogger.warn(
this.app.getBaseDir(), '检测到模块[user] jwt.secret 配置是默认值,即将自动修改...'
'modules',
'user',
'config.ts'
);
// 替换文件内容
let fileData = fs.readFileSync(filePath, 'utf8');
const secret = uuid().replace(/-/g, '');
this.config.user.jwt.secret = secret;
fs.writeFileSync(filePath, fileData.replace('cool-app-xxxxxx', secret));
this.coreLogger.info(
'\x1B[36m [cool:module:user] midwayjs cool module user auto modify jwt.secret\x1B[0m'
); );
setTimeout(() => {
const filePath = path.join(
this.app.getBaseDir(),
'..',
'src',
'modules',
'user',
'config.ts'
);
// 替换文件内容
let fileData = fs.readFileSync(filePath, 'utf8');
const secret = uuid().replace(/-/g, '');
this.config.user.jwt.secret = secret;
fs.writeFileSync(filePath, fileData.replace('cool-app-xxxxxx', secret));
this.coreLogger.info(
'\x1B[36m [cool:module:user] midwayjs cool module user auto modify jwt.secret\x1B[0m'
);
}, 8000);
} }
} }
} }