FabricioG
FabricioG

Reputation: 3320

Node js process stays on linux ps

I'm currently running a node js server accessible by a specific url on my apache server.

The entire code is:

// server.js

    var webshot = require('./lib/webshot');
    var fs = require('fs');
    var http = require('http');
    var bodyParser = require('body-parser');
    const used = process.memoryUsage();

    var express = require('express');
    var app = express();
    app.use( bodyParser.urlencoded() );

    // your express configuration here
    var httpServer = http.createServer(app);
    // For http
    httpServer.listen(8080);


    app.post('/', function (req, res) {
        console.log(req.body);
        var firstLine = req.body.firstLine;
        var secondLine = req.body.secondLine;
        var previewID = req.body.previewId;
        var productPlate = req.body.prodName;
        res.header('Access-Control-Allow-Origin', 'https://citylocs.com');
        res.header('Access-Control-Allow-Methods', 'GET, POST, PUT');
        res.header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
        takeWebshot(firstLine, secondLine, previewID, productPlate)
        res.end()
    });

    function takeWebshot(fLine, sLine, pID, prodPlate) {
        var options = {
          onLoadFinished: {
            fn: function() {
              document.getElementById("user_input").value=this.fLineSnapshot;
              document.getElementById("second_user_input").value=this.sLineSnapshot;
              document.getElementById("preview_btn").click();
            },
            context: {
              fLineSnapshot: fLine,
              sLineSnapshot: sLine,
            }
          },
          takeShotOnCallback: true,
          captureSelector: '#img_preview_fancybox',
          licensePlate: 'Guzman Plate'
        };

        webshot('example.com/preview/productpreview/testy.html?prod=' + prodPlate, '../screenshot/' + pID +'.png', options, function(err) {
          if(!err) { process.exit() }
          else { console.log(err);
process.exit() }
        });
      };

It basically takes some data and does a screen shot of a website using phantom js through this method webshot(). to save memory since this functions multiple times I have process.exit() after webshot() completes in the call back. My expectation is that server.js exists. It's then retired up by pm2. The problem is I'm getting a memory error after some time. After checking running ps aux --sort -rss

I get this:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root     12019  0.0  2.5 224036 105992 ?       Ss   04:13   0:06 /usr/local/cpanel/3rdparty/perl/528/bin/perl -T -w /usr/local/cpanel/3rdparty/bin/spamd --max-spare=
root     12237  0.0  2.4 225184 103664 ?       S    04:26   0:03 spamd child
root     12238  0.0  2.4 224036 102128 ?       S    04:26   0:00 spamd child
root     12239  0.0  2.4 224036 102124 ?       S    04:26   0:00 spamd child
mysql     1592  0.2  1.3 1586436 57104 ?       Sl   Aug29   1:56 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user
named     1056  0.0  0.9 1924092 41828 ?       Ssl  Aug29   0:00 /usr/sbin/named -u named
root      1380  0.0  0.8 902416 37480 ?        Ssl  Aug29   0:19 PM2 v3.5.1: God Daemon (/root/.pm2)         
root      5032  0.0  0.8 2037540 35732 ?       Sl   Aug29   0:01 phantomjs /home/ogdi/public_html/preview/productpreview/node-webshot/lib/webshot.phantom.js {
root      9778  0.0  0.8 2037500 35708 ?       Sl   02:57   0:01 phantomjs /home/ogdi/public_html/preview/productpreview/node-webshot/lib/webshot.phantom.js {
root     18725  0.0  0.8 2037500 35680 ?       Sl   08:09   0:00 phantomjs /home/ogdi/public_html/preview/productpreview/node-webshot/lib/webshot.phantom.js {
root      7577  0.0  0.8 2037460 35676 ?       Sl   01:46   0:01 phantomjs /home/ogdi/public_html/preview/productpreview/node-webshot/lib/webshot.phantom.js {

It's telling me the phantom js script is still active since yesterday. It appears several more times. Why is that if I process.exit() successfully?

EDIT: I manually killed a bunch of those phantom js commands. I diagnosed one a little more. and it says STAT Sl which means it's Sleep, multi thread. according to what I read.

Webshot link: https://raw.githubusercontent.com/brenden/node-webshot/master/lib/webshot.js Webshot phantom link: https://raw.githubusercontent.com/brenden/node-webshot/master/lib/webshot.phantom.js

Upvotes: 2

Views: 373

Answers (1)

suspectus
suspectus

Reputation: 17268

process.exit() is not called if there is an error. In the event of an error, is there any point in the process continuing?

webshot('example.com/preview/productpreview/testy.html?prod=' + prodPlate, '../screenshot/' + pID +'.png', options, function(err) {

      if (err) { console.log(err) }

      process.exit(err)   
});

Upvotes: 2

Related Questions