0

I am new to JS/Express/Node/Kue (using Forever to keep it running). I have taken on this code base, which is causing our EC2 instance to spike the CPU utilization to 100% every 6 hours. Usage grows steadily from 0% at 08UTC to 100% at 14UTC to 0% at 20UTC to 100% at 08UTC. This happens non stop so looks like hills valley hill valley.

What is odd is that for this business there is 0 activity between 10pm and 8am on this app.

I am looking at the code, which is mostly post requests. I see some params that are not marked as a var so that makes it a global object correct? Wondering if those are not being destroyed? The initMe function is filled with objects not marked as vars either. I have looked at others POST code and they do not delete their post_options or post_options either?

jobs.process('notify_me', 50, function(job, done){
  var token = null;
  var ems_id = null;
  var title = null;
  var message = null;
  var type = null;
  var websocket_server = null;
  var payload = null;

  function initMe(){
    logMe("Got the following from payload: " + JSON.stringify(job));
    ems_id = job.data.ems_id;
    logMe("ems_id: " + ems_id);
    title = job.data.title;
    logMe("title: " + title);
    message = job.data.message;
    logMe("message: " + message);
    type = job.data.type;
    logMe("type: " + type);
    websocket_server = job.data.websocket_server;
    logMe("websocket_server: " + websocket_server);
    payload = {'title': title, 'message': message, 'type': type}

    login();
  }

  function displayTime() {
    var str = "";

    var currentTime = new Date()
    var hours = currentTime.getHours()
    var minutes = currentTime.getMinutes()
    var seconds = currentTime.getSeconds()

    if (minutes < 10) {
        minutes = "0" + minutes
    }
    if (seconds < 10) {
        seconds = "0" + seconds
    }
    str += hours + ":" + minutes + ":" + seconds;
    return str;
  }

  function logMe(str){
    console.log(displayTime() + " : " + str);
    job.log(displayTime() + " : " + str);
  }

  function login(){
    logMe("start login");
    params = {
      api_key: ***,
      email: ********,
      password: *******
    };
    var post_data = querystring.stringify(params);
    var post_options = {
        hostname: DOMAIN,
        port: 443,
        path: "/v1/login_bot",
        method: 'POST',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'Content-Length': post_data.length
        }
    };
    var post_req = https.request(post_options, function(res) {
      res.setEncoding('utf8');
      res.on('data', function (chunk) {
        token = JSON.parse(chunk).token;
        logMe("retrieved token: " + token);
        notifyME();
      });
    });
    post_req.write(post_data);
    post_req.end();
  }

  function notifyME(){
    logMe("connecting to " + websocket_server);
    var socket = io.connect(websocket_server, {'force new connection': true});
    socket.on('connect', function(){
      logMe("notifying ems: " + ems_id);
      socket.emit('notify_me', 'ems_' + ems_id, JSON.stringify({payload: payload}));
      socket.disconnect();
    });
    done();
  }

  initMe();
});
sshow
  • 8,820
  • 4
  • 51
  • 82
jdog
  • 10,351
  • 29
  • 90
  • 165
  • Is the env set to prod or dev? – Milan Velebit Jan 10 '18 at 21:22
  • Sorry I am new to all this. Do you mean a env variable set in node or express? Or the sever? – jdog Jan 11 '18 at 20:53
  • The NODE_ENV var, Node behaves very differently when it comes to its env. And what exactly is a 'jobs' class? – Milan Velebit Jan 11 '18 at 22:01
  • Jobs is a is a priority job queue backed by redis for node. https://github.com/Automattic/kue – jdog Jan 16 '18 at 15:49
  • 1
    Perhaps a shot in the dark, but if you're using the `-w` flag to watch for file changes, ensure you're narrowing your scope by also using --watchDirectory. If you don't specify the root location, the root of the file system will be used (which causes 100%+ CPU utilization). – John Paul Barbagallo Mar 15 '18 at 18:09

0 Answers0