在 node-windows 中重新启动服务
Restarting a service in node-windows
使用 node-windows 包,我在本地安装节点服务器作为服务。然后有一个接口来修改 .env 文件,当我实际修改 .env 的配置并保存更改时,问题是服务没有按预期重新启动,以确认这些更改。如果有任何其他方法可以从此包或任何其他类似的解决方法处理服务重启,谁能指导我?我实际上正在尝试像这样处理重启:
const path = require("path");
let Service = require("node-windows").Service;
let EventLogger = require("node-windows").EventLogger;
const Messages= require("./models/messagesModel").getInstance();
let filePathServer = path.resolve(__dirname, "app.backend.js");
class ServiceInstall {
constructor(envConfig) {
this.log = new EventLogger(envConfig.SERVICE_NAME);
this.svc = new Service({
name: envConfig.SERVICE_NAME,
description: envConfig.SERVICE_DESCRIPTION,
script: filePathServer,
wait: envConfig.SERVICE_WAIT,
grow: envConfig.SERVICE_GROW
});
}
installWindowsService() {
// event handlers to install the service
}
restartWindowsService(){
this.svc.on("stop", () => {
this.log.info("Service " + this.svc.name + " stopped!");
Messages.info("Service " + this.svc.name + " stopped!");
});
this.svc.on("start", () => {
this.log.info("Service " + this.svc.name + " started!");
Messages.info("Service " + this.svc.name + " started!");
});
this.svc.restart();
}
}
module.exports = ServiceInstall;
在安装过程中,node-windows
主要执行两个步骤:
通过从 winsw.exe
复制并在 Windows 注册表中创建相应的条目来创建 .exe
文件,因此 Windows 可以识别此 .exe
作为 Windows 服务。
使用传递给服务构造函数的值生成同名的 .xml
文件。
这意味着一旦 .xml
被创建,任何应用于构造函数输入的更改都不会被带入 .xml
文件,除非你做一个完整的重新- 安装服务 [使用 svc.uninstall()
后跟 svc.install()
]
如果您想要动态更改输入,但不需要重新安装,您应该将这些值放在 config.json
中,然后简单地 require
即 config.json
在脚本中 您正在尝试作为 Windows 服务托管。
现在,如果您对 config.json
进行更改,您只需重新启动服务即可反映更改。
此外,如果您不想在每次 config.json
更改时手动重新启动,请在 execPath
中使用 nodemon
而不是传递的配置对象中的 node
到 Service
构造函数。
使用 node-windows 包,我在本地安装节点服务器作为服务。然后有一个接口来修改 .env 文件,当我实际修改 .env 的配置并保存更改时,问题是服务没有按预期重新启动,以确认这些更改。如果有任何其他方法可以从此包或任何其他类似的解决方法处理服务重启,谁能指导我?我实际上正在尝试像这样处理重启:
const path = require("path");
let Service = require("node-windows").Service;
let EventLogger = require("node-windows").EventLogger;
const Messages= require("./models/messagesModel").getInstance();
let filePathServer = path.resolve(__dirname, "app.backend.js");
class ServiceInstall {
constructor(envConfig) {
this.log = new EventLogger(envConfig.SERVICE_NAME);
this.svc = new Service({
name: envConfig.SERVICE_NAME,
description: envConfig.SERVICE_DESCRIPTION,
script: filePathServer,
wait: envConfig.SERVICE_WAIT,
grow: envConfig.SERVICE_GROW
});
}
installWindowsService() {
// event handlers to install the service
}
restartWindowsService(){
this.svc.on("stop", () => {
this.log.info("Service " + this.svc.name + " stopped!");
Messages.info("Service " + this.svc.name + " stopped!");
});
this.svc.on("start", () => {
this.log.info("Service " + this.svc.name + " started!");
Messages.info("Service " + this.svc.name + " started!");
});
this.svc.restart();
}
}
module.exports = ServiceInstall;
在安装过程中,node-windows
主要执行两个步骤:
通过从
winsw.exe
复制并在 Windows 注册表中创建相应的条目来创建.exe
文件,因此 Windows 可以识别此.exe
作为 Windows 服务。使用传递给服务构造函数的值生成同名的
.xml
文件。
这意味着一旦 .xml
被创建,任何应用于构造函数输入的更改都不会被带入 .xml
文件,除非你做一个完整的重新- 安装服务 [使用 svc.uninstall()
后跟 svc.install()
]
如果您想要动态更改输入,但不需要重新安装,您应该将这些值放在 config.json
中,然后简单地 require
即 config.json
在脚本中 您正在尝试作为 Windows 服务托管。
现在,如果您对 config.json
进行更改,您只需重新启动服务即可反映更改。
此外,如果您不想在每次 config.json
更改时手动重新启动,请在 execPath
中使用 nodemon
而不是传递的配置对象中的 node
到 Service
构造函数。