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

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就够了",
"private": true,
"dependencies": {
"@cool-midway/core": "^7.1.10",
"@cool-midway/core": "^7.1.11",
"@cool-midway/rpc": "^7.0.0",
"@cool-midway/task": "^7.0.0",
"@midwayjs/bootstrap": "^3.15.0",
@ -61,8 +61,7 @@
"ci": "npm run cov",
"build": "mwtsc --cleanOutDir",
"pm2:start": "pm2 start ./bootstrap.js -i max --name cool-admin",
"pm2:stop": "pm2 stop cool-admin & pm2 delete cool-admin",
"pm2:debug": "pm2-runtime start ./bootstrap.js -i max --name cool-admin"
"pm2:stop": "pm2 stop cool-admin & pm2 delete cool-admin"
},
"midway-bin-clean": [
".vscode/.tsbuildinfo",

View File

@ -21,21 +21,38 @@ export class BaseAppEvent {
@Event('onServerReady')
async onServerReady() {
this.checkConfig();
}
/**
*
*/
async checkConfig() {
if (this.config.base.jwt.secret == 'cool-admin-xxxxxx') {
const filePath = path.join(
this.app.getBaseDir(),
'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'
this.coreLogger.warn(
'检测到模块[base] jwt.secret 配置是默认值,即将自动修改...'
);
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()
coolEventManager: CoolEventManager;
@Post('/send')
async send() {
await this.coolEventManager.emit('demo', { a: 1 }, 1);
@Post('/comm', { summary: '普通事件,本进程生效' })
async comm() {
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();
}
}

View File

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

View File

@ -21,21 +21,35 @@ export class UserAppEvent {
@Event('onServerReady')
async onServerReady() {
this.checkConfig();
}
/**
*
*/
async checkConfig() {
if (this.config.user.jwt.secret == 'cool-app-xxxxxx') {
const filePath = path.join(
this.app.getBaseDir(),
'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'
this.coreLogger.warn(
'检测到模块[user] jwt.secret 配置是默认值,即将自动修改...'
);
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);
}
}
}