简体   繁体   English

使用PM2运行完整的开发环境

[英]Using PM2 to run a complete development environment

I'm using PM2 to run my Node microservices, and in development I would also like to start MongoDB so that I can be up and running for development with a single command. 我正在使用PM2运行我的Node微服务,并且在开发中,我还想启动MongoDB,以便可以通过一个命令启动并运行以进行开发。 I have a development ecosystem.js file that looks something like this: 我有一个开发ecolog.js文件,看起来像这样:

apps: [
        {
            name: 'API',
            script: 'index.js',
            out_file: './logs/api.stdout.log',
            error_file: './logs/api.stderr.log'
        },
        {
            name: 'Emailer',
            script: 'services/emailer/index.js',
            out_file: './logs/emailer.stdout.log',
            error_file: './logs/emailer.stderr.log'
        },
        {
            name: 'MongoDB',
            script: 'services/development/mongodb.sh'
        }
    ]

and mongodb.sh looks like this: mongodb.sh看起来像这样:

#!/bin/bash
mongod

When I start PM2, everything runs just fine. 当我启动PM2时,一切运行正常。 When I call pm2 stop all , it kills the node processes, but doesn't kill MongoDB. 当我叫pm2 stop all ,它杀死了节点进程,但没有杀死MongoDB。

I'm guessing that this is because it's attaching to the bash instance, not the mongod instance. 我猜这是因为它附加在bash实例上,而不是mongod实例上。 Is there a way to make PM2 start and attach to an executable, rather than a script file? 有没有一种方法可以使PM2启动并附加到可执行文件而不是脚本文件?

As you know; 如你所知;

PM2 is a production process manager for Node.js / io.js applications with a built-in load balancer. PM2是具有内置负载平衡器的Node.js / io.js应用程序的生产过程管理器。 It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks. 它使您可以使应用程序永远保持活动状态,无需停机即可重新加载它们,并简化常见的系统管理任务。

So it has no promise for attaching executables or daemonized utilities. 因此,它没有附加附加可执行文件或守护程序的承诺。 But attaching bash script file is a very handy thing on PM2. 但是附加bash脚本文件在PM2上非常方便。 In my opinion if when stopped it closes bash process, mongo daemon will still live in this case you faced. 我认为,如果停止后它会关闭bash进程,则mongo守护程序将仍然存在于您遇到的这种情况下。 So you may catch bash script exit situation and while stopping pm2 processes that caught exit situation you will be able to call mongod --shutdown command. 因此,您可能会遇到bash脚本退出情况,并且在停止导致退出情况的mongod --shutdown进程时,可以调用mongod --shutdown命令。

Or another solution create a start and stop scripts and run and stop daemonized utilities in these bash scripts like mongodb. 或者另一个解决方案是在这些bash脚本(如mongodb)中创建startstop脚本以及运行和停止守护程序。

Extra solution is Docker? Docker是额外的解决方案吗?

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

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