EggBornJS新增了Startup机制,允许在每个工作进程启动时执行初始化工作

声明Startup

src/suite-vendor/test-party/modules/test-party/backend/src/config/config.js

  1. 1module.exports = appInfo => {
  2. 2 const config = {};
  3. 3
  4. 4 // startups
  5. 5 config.startups = {
  6. 6 startupAll: {
  7. 7 bean: 'startupAll',
  8. 8 },
  9. 9 startupInstance: {
  10. 10 bean: 'startupInstance',
  11. 11 instance: true,
  12. 12 },
  13. 13 };
  14. 14
  15. 15 return config;
  16. 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

  1. 1const require3 = require('require3');
  2. 2const assert = require3('assert');
  3. 3
  4. 4module.exports = app => {
  5. 5 class Startup extends app.meta.BeanBase {
  6. 6
  7. 7 async execute() {
  8. 8 console.log('test/feat/startup: all');
  9. 9 assert.equal(this.ctx.instance, undefined);
  10. 10 }
  11. 11
  12. 12 }
  13. 13
  14. 14 return Startup;
  15. 15};

注册Bean组件

src/suite-vendor/test-party/modules/test-party/backend/src/beans.js

  1. 1const startupStartupAll = require('./bean/startup.startupAll.js');
  2. 2const startupStartupInstance = require('./bean/startup.startupInstance.js');
  3. 3
  4. 4module.exports = app => {
  5. 5 const beans = {
  6. 6 // startup
  7. 7 'startup.startupAll': {
  8. 8 mode: 'app',
  9. 9 bean: startupStartupAll,
  10. 10 },
  11. 11 'startup.startupInstance': {
  12. 12 mode: 'app',
  13. 13 bean: startupStartupInstance,
  14. 14 },
  15. 15 };
  16. 16 return beans;
  17. 17};
注册名称 场景 所属模块 global beanFullName
startupAll startup test-party false test-party.startup.startupAll
startupInstance startup test-party false test-party.startup.startupInstance