EggBornJS新增了Startup
机制,允许在每个工作进程启动时执行初始化工作
声明Startup
src/suite-vendor/test-party/modules/test-party/backend/src/config/config.js
- 1module.exports = appInfo => {
- 2 const config = {};
- 3
- 4 // startups
- 5 config.startups = {
- 6 startupAll: {
- 7 bean: 'startupAll',
- 8 },
- 9 startupInstance: {
- 10 bean: 'startupInstance',
- 11 instance: true,
- 12 },
- 13 };
- 14
- 15 return config;
- 16};
名称 | 说明 |
---|---|
bean | Startup对应的Bean组件 |
instance | Startup执行的时机 |
属性
instance
的本质是什么?
- 前面我们提到EggBornJS支持多实例
- 如果
instance
设为false,那么就是当系统初始化
时触发- 如果
instance
设为true,那么就是当实例初始化
时触发
定义Bean组件
以startupAll
为例
src/suite-vendor/test-party/modules/test-party/backend/src/bean/startup.startupAll.js
- 1const require3 = require('require3');
- 2const assert = require3('assert');
- 3
- 4module.exports = app => {
- 5 class Startup extends app.meta.BeanBase {
- 6
- 7 async execute() {
- 8 console.log('test/feat/startup: all');
- 9 assert.equal(this.ctx.instance, undefined);
- 10 }
- 11
- 12 }
- 13
- 14 return Startup;
- 15};
注册Bean组件
src/suite-vendor/test-party/modules/test-party/backend/src/beans.js
- 1const startupStartupAll = require('./bean/startup.startupAll.js');
- 2const startupStartupInstance = require('./bean/startup.startupInstance.js');
- 3
- 4module.exports = app => {
- 5 const beans = {
- 6 // startup
- 7 'startup.startupAll': {
- 8 mode: 'app',
- 9 bean: startupStartupAll,
- 10 },
- 11 'startup.startupInstance': {
- 12 mode: 'app',
- 13 bean: startupStartupInstance,
- 14 },
- 15 };
- 16 return beans;
- 17};
注册名称 | 场景 | 所属模块 | global | beanFullName |
---|---|---|---|---|
startupAll | startup | test-party | false | test-party.startup.startupAll |
startupInstance | startup | test-party | false | test-party.startup.startupInstance |
评论: