server.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. var http = require("http");
  2. var url = require("url");
  3. const child_process = require('child_process');
  4. var flagOfRepair = 1;
  5. var startRepairorder=function(){
  6. var child_pid = child_process.spawn('node',['./terry/repairorder.js',1]);
  7. flagOfRepair = 0;
  8. child_pid.stdout.on('data', function (data) {
  9. if (data!=null) {
  10. console.log('stdout: ' + data);
  11. }
  12. });
  13. child_pid.stderr.on('data', function (data) {
  14. console.log('stderr: ' + data);
  15. });
  16. child_pid.on('close', function (code) {
  17. console.log('子进程已退出,退出码 '+code);
  18. flagOfRepair = 1;
  19. });
  20. }
  21. //守护进程
  22. var watchProcess = function(){
  23. setInterval(function(){
  24. if (flagOfRepair==1) {
  25. startRepairorder();
  26. }
  27. }, 5000);
  28. }
  29. function start(app) {
  30. var httpServer = http.createServer(app);
  31. process.on('uncaughtException', function(e) {
  32.   console.log(e);
  33. });
  34. var server = app.listen(8081, function () {
  35. var host = server.address().address
  36. var port = server.address().port
  37. watchProcess()
  38. console.log("应用实例,访问地址为 https://%s:%s", host, port)
  39. })
  40. }
  41. exports.start = start;