简体   繁体   English

使用 PM2 的 Nodejs 应用程序的启动顺序

[英]Startup Sequence of Nodejs Apps using PM2

I am using the command pm2 start apps.json to start multiple apps in a single command.我正在使用命令pm2 start apps.json在单个命令中启动多个应用程序。 These apps are defined in apps.json :这些应用程序在apps.json中定义:

{
  "apps": [
    {
      "name": "foo",
      "script": "./foo.js",
    },
    {
      "name": "bar",
      "script": "./bar.js",
    },
    {
      "name": "baz",
      "script": "./baz.js",
    }
  ]
}

Question: Is it possible to define the startup sequence, such that foo.js has to finish starting first before bar.js and baz.js can start?问题:是否可以定义启动顺序,使得foo.js必须先完成启动,然后才能启动bar.jsbaz.js

For example, foo.js can perform a graceful start , running process.send('ready') to change its pm2 status to online .例如, foo.js可以执行优雅的启动,运行process.send('ready')以将其 pm2 状态更改为online Only then will bar.js and baz.js be started by pm2.只有这样, bar.jsbaz.js才会由 pm2 启动。 This will be similar to Docker Compose's depend_on parameter.这将类似于 Docker Compose 的depend_on参数。

No such thing can be done via the configuration file only, but PM2 has a programmatic api which allows you to perform IPC (Inter-Process communication).仅通过配置文件不能完成这样的事情,但 PM2 有一个 可编程的 api允许您执行IPC (进程间通信)。

The following methods are the ones to work with:以下方法是可以使用的:

  • pm2.list To list the processes that are running and get their names / IDs pm2.list列出正在运行的进程并获取它们的名称/ID
  • pm2.launchBus For the processes that will receive information and react in consequence pm2.launchBus用于接收信息并做出反应的进程
  • pm2.sendDataToProcessId For sending informations to another process pm2.sendDataToProcessId用于向另一个进程发送信息

This way you can run multiple scripts and make one wait for another.这样,您可以运行多个脚本并让一个等待另一个。 Once a script recieve a message on the message bus , it can start a process with pm2.start一旦脚本在消息总线上收到一条消息,它就可以使用pm2.start启动一个进程。

Here's a piece of PSEUDO-CODE to illustrate my point:这是一段伪代码来说明我的观点:

const pm2 = require('pm2');

pm2.connect(() => {
  pm2.list(function(err, processes) {
    const fooProcess = processes.find(p => p.name == 'foo');

    pm2.launchBus((err, bus) => {
      bus.on('process:msg', packet => {
        if (packet.startBar === true) {
          pm.start({ script: 'bar.js' }, (err, apps) => { ... })
        }
      });
      bus.on('error', console.error);
    });
  });
});

In another script, you would have the following:在另一个脚本中,您将拥有以下内容:

pm2.sendDataToProcessId(barProcessID, {
  data : { startBar : true },
  topic: 'process:msg'
}, (err, res) => console.log(err, res));

Best regards此致

A little hack, you can write: "start": "pm2 start server.js && pm2 start server1.js" inside package.json .一点小技巧,你可以在 package.json 里面写: "start": "pm2 start server.js && pm2 start server1.js" package.json start server1.js" 。 If you'd run it as npm start, it'll run the start script, similarly, you can create the script to stop it.如果您将它作为 npm 启动运行,它将运行启动脚本,同样,您可以创建脚本来停止它。

If not then you can also use child_process [it comes by default with nodejs] to run commands from inside your script by using childProcess.exec('pm2 start server.js && pm2 start server1.js');如果没有,那么您也可以使用child_process [它默认带有 nodejs] 通过使用childProcess.exec('pm2 start server.js && pm2 start server1.js');从脚本内部运行命令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM