actionherojs 插件restart 说明

actionherojs 是提供了一种restart的机制,以下是简单说明下

服务restart api

// 引入api
import {Action, api} from "actionhero"
module.exports = class MyDemoAction extends Action {
    async run() {
        // 调用api restart
        api.process.restart();
        return {name:"dalong"};
    }
    constructor(){
        super()
        this.name="dalong"
        this.description="demoapp",
        this.version=1
    }
}

简单原理说明

actionherojs 的入口是一个Process,Process 实际上会加载各类的Initializer,因为Initializer 是基于动态import 的,而且
Initializer 实现了start,stop 方法,所以实现restart 功能就比较简单了,restart 有几个问题需要处理.一个是Initializer 的restart
一个是配置的重新加载

  • Initializer restart
    这个相对简单,先stop,然后start
 
  async restart() {
    if (this.running === true) {
      await this.stop();
      rebuildConfig();
      await this.start();
    } else {
      await this.start();
    }
  }

对于start 以及stop 方法,具体可以参考源码 src/classes/process.ts

  • 配置重新加载
// 主要包含了环境变量,模块配置的重新合并处理,具体可以参考如下,代码在src/modules/config.ts
export const rebuildConfig = () => {
  recalculateEnv();
  recalculateActionheroVersion();
  recalcuateId();
  recalculateProjectRoot();
  recalculateIsTypescript();
  config = buildConfig();
};

说明

actionherojs 因为是依赖的动态import 特性,以及自己构建一个生命周期,所以实现restart相对比较简单,是一种值得参考的模式

参考资料

https://docs.actionherojs.com/classes/Process.html

posted on 2022-02-05 19:40  荣锋亮  阅读(17)  评论(0编辑  收藏  举报

导航