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   荣锋亮  阅读(20)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2021-02-05 dremio 中文查询问题解决
2021-02-05 nodejs 几个不错的文件系统抽象包
2020-02-05 pgspider 其他扩展的集成
2020-02-05 pgspider 简单试用
2020-02-05 pgspider sqlite mysql docker 镜像
2019-02-05 100 webhook implementations
2019-02-05 streamdataio 实时数据分发平台

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示