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相对比较简单,是一种值得参考的模式
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用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 实时数据分发平台