节点冻结直到crtl + c

时间:2018-10-03 15:12:40

标签: node.js express socket.io

我有一个随机冻结的节点应用程序。 我必须按ctrl + c才能将其解冻-并自行恢复正常。

似乎无法在何时何地找到问题。它可以连续几天没有问题...

我可以说问题不在验证用户或用户/ conf时。 可能的嫌疑人:cmd.get函数中的getTaskStatus和websocket侦听器...

这是服务器代码

var app = require('express')();
var bodyParser = require('body-parser');
var server = require('http').Server(app);
var io = require('socket.io')(server);
var cmd = require('node-cmd');
var colors = require('colors');
var auth = require ('./assets/auth.js');
var Users = require ('./assets/users.js');
var Conf = require ('./assets/conf.js');
var _ = require('lodash');
let ConnectedUsers = 0;


let users = Users.compile();

// create tasks status var
let taskStatus ={}
Object.keys(users).forEach(function(key) {
  for(var i = 0 ; i< users[key].tasks.length; ++i) {
    let task = users[key].tasks[i];
    taskStatus[task] = '';
  }
});

// start server
server.listen(3131, function () {
   app.use(bodyParser.urlencoded({ extended: true }));
   app.use(bodyParser.json());

    console.log(new Date().toLocaleString() + ' - Server is running...waiting for connection');
});

//main page
app.get('/taskrunner', function (req, res) {
  res.sendFile(__dirname + '/public/index.html');
});

// for authentication REST
app.post('/auth', function (req, res, next) {
  const user = req.headers.user;
  const pwd = req.headers.pwd;
  console.log(colors.yellow(new Date().toLocaleString() + ' - authenticating '+user));
  auth.authenticate(user,pwd).then((result)=> {
    if(result === true) {
      let perm =[users[user].tasks,[]];
      Object.keys(users[user].tasks).forEach(function(key) {
        const task = users[user].tasks[key];
        perm[1].push(taskStatus[task])
      });
      res.send(perm)
    } else {
      res.send('error')
    }
  })
});

app.get('/auth/tasks', function (req, res, next) {
  const user = req.headers.user;
  let perm =[users[user].tasks,[]];
  Object.keys(users[user].tasks).forEach(function(key) {
    const task = users[user].tasks[key];
    perm[1].push(taskStatus[task])
  });
  res.send(perm)
});

// websocket listener
io.on('connection', function (socket) {
  if(ConnectedUsers == 0) {
    console.log(colors.grey(new Date().toLocaleString() + ' - Starting Task Listener'));
    var interval = setInterval(getTaskStatus, 1000)

  }; // start getting Tasks' status
  console.log(colors.grey(new Date().toLocaleString() + ' - Client connected. Total Connected: '+ (++ConnectedUsers)));
  socket.on('disconnect', function(){
    console.log(colors.grey(new Date().toLocaleString() + ' - User disconnect. Total Connected: '+ (--ConnectedUsers)));
    if(ConnectedUsers == 0) {
      console.log(colors.grey(new Date().toLocaleString() + ' - Stopping Task Listener'));
      clearInterval(interval)
    }; // stop getting Tasks' status
  });
});

// fetch Tasks' status
function getTaskStatus(){
  Object.keys(taskStatus).forEach(function(key) {
    cmd.get(
      `schtasks /query /S ${Conf.SERVER} /U ${Conf.USER} /P ${Conf.PWD} /TN "${key}" /FO LIST`,
    //  console.log(task);
      function(err, data, stderr){
        if(err){console.log(err.red)}
      //  console.log('DATA: ' + data.blue);
      let status = data.split("\n")[5].split(":")[1].trim() || "" ;
        if(taskStatus[key] != status) {  // check if status changed
          taskStatus[key] = status
          io.emit('StatusUpdate', {task: key, status: status})
        }
      }
    );
  });
}

// TASK run REST
app.get('/task',function(req,res){
  const task = req.query.task;
  const user = req.query.user;
  cmd.get(
      `schtasks /run /S ${Conf.SERVER} /U ${Conf.USER} /P ${Conf.PWD} /TN "${task}"`,
      function(err, data, stderr){
        if(data.substring(0, 7)=='SUCCESS') {
          //console.log(new Date() + ' - '+data);
          console.log(colors.green(new Date().toLocaleString() + ' - Started '+task+' Task ('+user+')'));
      }
    }
  );
  res.send('OK')
});

0 个答案:

没有答案