【问题标题】:Restarting a service in node-windows在节点窗口中重新启动服务
【发布时间】:2018-10-26 10:05:43
【问题描述】:

使用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;

【问题讨论】:

    标签: javascript node.js windows-services daemon node-windows


    【解决方案1】:

    在安装过程中,node-windows 基本上执行了两个步骤:

    1. 通过从winsw.exe 复制并在Windows 注册表中创建相应条目来创建.exe 文件,以便Windows 可以将此.exe 识别为Windows 服务。

    2. 使用传递给服务构造函数的值来生成一个同名的.xml文件。

    这意味着一旦创建了.xml,任何应用于构造函数输入的更改都不会被带到.xml文件中,除非您完全重新安装服务[使用svc.uninstall() 后跟svc.install()]

    如果您想要动态更改输入,但不需要重新安装,您应该将这些值放在 config.json 中,然后只需将 requireconfig.json 放在脚本中正在尝试作为 Windows 服务托管。

    现在,如果您对 config.json 进行更改,您只需重新启动服务以反映更改。

    另外,如果您不想在每次config.json 更改时手动重新启动,请在传递给Service 构造函数的配置对象中使用nodemon 中的execPath 而不是node

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 1970-01-01
      • 2013-05-09
      • 2013-11-15
      相关资源
      最近更新 更多