• 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

  • 相关阅读:
    13.6 线程通信
    13.5 线程同步
    13.4 控制线程
    13.3 线程的生命周期
    13.2 线程的创建与启动
    13.1 线程概述
    12.10 NIO.2的功能和用法
    bs4
    mysql基本命令
    HDU-1021 Fibonacci Again
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/15864659.html
Copyright © 2020-2023  润新知