简体   繁体   English

通过从NodeJS内部执行PHP脚本来重新启动NodeJS

[英]Restarting NodeJS by executing a PHP script from within NodeJS

I have a problem trying to update a folder and deploy its contents again. 我在尝试更新文件夹并再次部署其内容时遇到问题。 I have to use NodeJS and have gotten port 8080 to work with. 我必须使用NodeJS并获得8080端口才能使用。 I have tried to create a php script (update.php): 我试图创建一个PHP脚本(update.php):

<?php
echo exec("git pull");
echo exec("node app.js");
?>

Now i want to start the NodeJS script to update and the ultimate goal is to be able to restart the server. 现在,我想启动NodeJS脚本进行更新,最终目标是能够重新启动服务器。

I use the express module: 我使用快递模块:

app.get('/Update', function(req,res) {
   exec("php update.php", function(error, stdout, stderr) {
      process.exit(0);
   }
}

The problem is that the NodeJS server quits when it gets a response from the script but the script tries to start the NodeJS server. 问题在于,当NodeJS服务器从脚本获得响应时会退出,但脚本会尝试启动NodeJS服务器。 This obviously cannot happen since it is already running on the specified port. 由于它已经在指定的端口上运行,因此这显然不会发生。

Now i found on google to use the module called 'nodemon' however I am not given sudo access so installing this is out of the question. 现在我在谷歌上发现使用名为“ nodemon”的模块,但是我没有sudo访问权限,因此安装它是不可能的。 Other results are using 其他结果正在使用

ps aux | grep node
kill -9 PROCESS_ID

This also yields problems since it is hard to configure the PHP script to kill the specified process but aside from that there are other NodeJS servers running in other child folders of the parent folder I am given. 这也产生了问题,因为很难配置PHP脚本来杀死指定的进程,但除此之外,在我得到的父文件夹的其他子文件夹中还有其他NodeJS服务器正在运行。 This means that if I'd use 'killall node' I'd get a lot of people angry that I killed their servers. 这意味着,如果我使用“ killall node”,我会激怒很多人,因为我杀死了他们的服务器。

What is the best approach to solving this problem using only port 8080 and wanting to deploy the changes in the Github repo when accessing a certain link? 仅使用端口8080并希望在访问特定链接时部署Github存储库中的更改来解决此问题的最佳方法是什么?

You can get the node's process id by Node.js command process.pid . 您可以通过Node.js命令process.pid获取节点的进程ID。

You can then make a bash script that executes all your commands and give the PID in environment variable. 然后,您可以制作一个执行所有命令的bash脚本,并在环境变量中提供PID。

1) Forget the update.php 1)忘记update.php

2) route 2)路线

app.get('/update', function(req, res) {
  var spawn = require('child_process').spawn;
  spawn('sh', [ 'update.sh' ], {
    env: { NODE_PID: process.pid }
  });
});

3) update.sh 3)更新

git pull
kill -9 $NODE_PID
node app.js

There are a couple of different approaches you could use. 您可以使用几种不同的方法。 I would suggest using a Job Queue so that you never have to restart the process. 我建议您使用作业队列,这样您就不必重启进程。 Using something like Faye your express app could send update messages to the Faye server... 使用Faye之类的快捷应用程序可以将更新消息发送到Faye服务器...

app.get('/update', function (req, res) {

  // connect to local service on 8000
  var client = new faye.Client('http://localhost:8000/');

  client.publish('/update', {
    message: 'pull'
  });

  res.sendStatus(200);
});

Your server would accept requests as follows... 您的服务器将接受以下请求...

var server = http.createServer(),
bayeux = new faye.NodeAdapter({mount: '/'});

bayeux.attach(server);

server.listen(8000);

And your subscriber application could listen to the /update queue for pull requests. 您的订阅者应用程序可以侦听/ update队列中的拉取请求。 If you run this using forever or pm2 your problem is solved. 如果您使用everever或pm2来运行它,那么您的问题就解决了。

Subscriber... 订户...

var client = new faye.Client('http://localhost:8000/');

client.subscribe('/update', function (message) {
  exec("cd /your/working/dir && git pull origin master", function(error, stdout, stderr) {
    process.exit(0);
  }  
});

Or as another reader suggested you could and probably should use git hooks. 或者如另一个读者所建议的那样,您可以并且可能应该使用git hooks。 A nice way to deploy is to have a bare repo. 部署的一种好方法是拥有裸仓库。

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

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