NodeJs runs again to reach Number of Processes limit in cPanel - node.js

I'm using shared hosting cPanel service.
I have ran a NodeJs application on server and everything is ok.
but after server backup runs in every day one NodeJs process add to the running processes and I can see these processes on terminal.
I have killed the processes but they came back after server backup!!!
I have write a code to exit from processes that run when port is busy:
tcpPortUsed.check(port, '127.0.0.1')
.then(function(inUse) {
console.log(`Port ${port} usage: `+inUse);
if (inUse) {
process.exit();
} else {
app.listen(port, () =>
console.log(`server is running on port: ${port}`)
);
}
}, function(err) {
console.error('Error on check:', err.message);
});
but the problem has remained...
this is the terminal after I run ps aux:
user 5435 1.0 0.0 9916 1820 pts/0 S 16:50 0:00 /bin/bash -l
user 5752 0.0 0.0 49844 1684 pts/0 R+ 16:50 0:00 ps aux
user 13658 0.0 0.1 1155556 36060 ? Sl 05:15 0:06 lsnode:/home1/user/node-server/
user 32346 0.0 0.1 1091012 37944 ? Sl Jul04 0:05 lsnode:/home1/user/node-server/

Related

Virtual size in docker is always increasing with puppeteer

I am developing a little program with nodejs and Puppeteer which goal is to generate a certain amount of PDF files. The program is working with the bluebird module in order to achieve concurrency. The problem is that the use of Physical and virtual memory does not stop to increase. The size of all the generated documents is approximately 25GB, but the used memory in the docker container is much bigger:
pdf-generator 64.2GB (virtual 68.4GB)
We generate the PDF with Puppeteer in this way:
async function generatePDF(browser, num) {
const page = await browser.newPage();
try {
const pdfUrl = pdfPathURL(num);
await page.goto(pdfUrl, {
waitUntil: ["domcontentloaded", "networkidle0", "load"],
timeout: 300000,
});
// await page.waitForLoadState({ waitUntil: "domcontentloaded" });
const buffer = await page.pdf({
format: "A4",
printBackground: true,
landscape: true,
preferCSSPageSize: true,
});
await page.close()
return buffer.toString("base64");
} catch (error) {
let messageError = error;
console.log(messageError);
return "";
}
} finally {
await page.close();
}
}
[EDIT]
This is the code that opens the Chromium instance. One per request:
async function generatePDFFile(id) {
let pdfFile;
let errorGeneration = "";
const browser = await launchPuppeteer();
try {
if (!browser) {
errorGeneration = `Error getting Chromium instance`;
}
if (!browser.isConnected()) {
errorGeneration = `Error connecting to Chromium`;
}
pdfFile = await generatePDFPage(browser, id);
console.log(`PDF file generated of id:`, id);
} catch (error) {
errorGeneration = error;
console.log("errorGeneration: ", error);
}
finally {
await browser.close();
}
return { id, pdf: pdfFile, error: errorGeneration };
}
const puppeteerParams = {
headless: true,
args: [
"--disable-gpu",
"--disable-dev-shm-usage",
"--disable-setuid-sandbox",
"--no-sandbox",
"--font-render-hinting=none",
'--single-process',
'--no-zygote'
],
};
The top command in the container
Tasks: 19 total, 1 running, 18 sleeping, 0 stopped, 0 zombie
%Cpu(s): 45.4 us, 11.0 sy, 0.0 ni, 42.5 id, 0.3 wa, 0.0 hi, 0.9 si, 0.0 st
MiB Mem : 15862.5 total, 1418.4 free, 9686.2 used, 4757.9 buff/cache
MiB Swap: 2048.0 total, 1291.0 free, 757.0 used. 4953.4 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
34855 myuser 20 0 1124.8g 222708 149664 S 81.0 1.4 0:02.74 chrome
34810 myuser 20 0 1124.8g 212544 146712 S 77.3 1.3 0:02.61 chrome
34918 myuser 20 0 1124.8g 184764 141052 S 36.7 1.1 0:01.10 chrome
31 myuser 20 0 706628 142100 33080 S 35.7 0.9 2:19.22 node
34968 myuser 20 0 1124.7g 136748 112832 S 9.3 0.8 0:00.28 chrome
35062 myuser 20 0 1124.7g 138452 114036 S 9.0 0.9 0:00.27 chrome
35013 myuser 20 0 1124.8g 137448 113456 S 8.3 0.8 0:00.25 chrome
60 myuser 20 0 965160 103512 33040 S 7.7 0.6 0:22.25 node
35106 myuser 20 0 1124.6g 105352 89208 S 5.0 0.6 0:00.15 chrome
8 myuser 20 0 630596 51892 32908 S 0.7 0.3 0:04.14 node
1 myuser 20 0 2420 524 452 S 0.0 0.0 0:00.05 sh
19 myuser 20 0 707412 57724 35064 S 0.0 0.4 0:02.26 npm start
30 myuser 20 0 2420 580 512 S 0.0 0.0 0:00.00 sh
48 myuser 20 0 705296 53336 34512 S 0.0 0.3 0:01.71 npm run example
59 myuser 20 0 2420 524 456 S 0.0 0.0 0:00.00 sh
24495 myuser 20 0 1124.7g 140500 116068 S 0.0 0.9 0:00.31 chrome
31812 myuser 20 0 4100 3376 2948 S 0.0 0.0 0:00.05 bash
31920 myuser 20 0 7012 3420 2848 R 0.0 0.0 0:00.03 top
34415 myuser 20 0 1124.8g 138368 114276 S 0.0 0.9 0:00.28 chrome
There are 8 chrome processes because I am concurrently doing 8 requests. The memory continues increasing

meteor Verifying Deployment - Connection refused

I am trying to deploy a meteor Application, But I am receiving this error message on the Verifying Deployment section with the following error message -
------------------------------------STDERR------------------------------------
: (7) Failed to connect to 172.17.0.2 port 3000: Connection refused
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
curl: (7) Failed to connect to 172.17.0.2 port 3000: Connection refused
=> Logs:
=> Setting node version
NODE_VERSION=14.17.4
v14.17.4 is already installed.
Now using node v14.17.4 (npm v6.14.14)
default -> 14.17.4 (-> v14.17.4 *)
=> Starting meteor app on port 3000
=> Redeploying previous version of the app
When I do the sudo netstat -tulpn | grep LISTEN in the server it shows this
tcp 0 0 10.0.3.1:53 0.0.0.0:* LISTEN 609/dnsmasq
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 406/systemd-resolve
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 745/sshd: /usr/sbin
tcp6 0 0 :::22 :::* LISTEN 745/sshd: /usr/sbin
When I run sudo docker ps i receive the following message -
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e51b1b4bf3a3 mup-appName:latest "/bin/sh -c 'exec $M…" About an hour ago Restarting (1) 49 seconds ago appName
68b723183f3d mongo:3.4.1 "/entrypoint.sh mong…" 9 days ago Restarting (100) 9 seconds ago mongodb
In my firewall i have also opened the Port 3000
If I check the Docker is running it seems like there is no docker running!!
Also in my mup.js file I am using http and not https
module.exports = {
servers: {
one: {
host: 'xx.xx.xxx.xxx',
username: 'ubuntu',
pem: '/home/runner/.ssh/id_rsa'
}
},
meteor: {
name: 'appName',
path: '../../',
docker: {
image: 'zodern/meteor:latest',
},
servers: {
one: {}
},
buildOptions: {
serverOnly: true
},
env: {
PORT: 3000,
ROOT_URL: 'http://dev-api.appName.com/',
NODE_ENV: 'production',
MAIL_URL: 'smtp://xxxx:xxx/eLPCB3nw3jubkq:#email-smtp.eu-north-1.amazonaws.com:587',
MONGO_URL: 'mongodb+srv://xxx:xx#xxx.iiitd.mongodb.net/Development?retryWrites=true&w=majority'
},
deployCheckWaitTime: 15
}
proxy: {
domains: 'dev.xxx.com',
ssl: {
letsEncryptEmail: 'info#xxx.com'
}
}
}
Any idea what might cause this issue?
I don't know why, but in the MUP docs the correct image name is zodern/meteor:root
If your app is slow to start, increase the deployCheckWaitTime . In my complex apps I put 600, just to ensure the app is up.

What makes my node app restart every 30 seconds using pm2

I'm still quite new to pm2 and currently trying to get a node app running. This workes fine except the fact that the app gets restarted without any reason.
The app runs fine without any crash on the development machine. The log files do not show any crash. (Already testet that the log files would log a crash.)
Did I configure pm2 wrong?
ecosystem.json
{
"name" : "app1",
"cwd" : "/home/pm2/apps/app1/prod",
"script" : "dist/main.js",
"log_date_format" : "YYYY-MM-DD HH:mm Z",
"error_file" : "/home/pm2/apps/app1/logs/stderr.log",
"out_file" : "/home/pm2/apps/app1/logs/stdout.log",
"pid_file" : "/home/pm2/apps/app1/pids/app1.pid",
"instances" : 1, //or 0 => 'max'
"min_uptime" : "200s", // 200 seconds, defaults to 1000
"max_restarts" : 10, // defaults to 15
"max_memory_restart": "1M", // 1 megabytes, e.g.: "2G", "10M", "100K", 1024 the default unit is byte.
"cron_restart" : "1 0 * * *",
"watch" : false,
"ignore_watch" : "./logs",
"exec_interpreter" : "node",
"env": {
"NODE_ENV": "production"
}
}
logs/stdout.log
2016-10-24 14:28 +02:00: server listening on port 3100
2016-10-24 14:28 +02:00: server listening on port 3100
2016-10-24 14:29 +02:00: server listening on port 3100
2016-10-24 14:29 +02:00: server listening on port 3100
2016-10-24 14:30 +02:00: server listening on port 3100
2016-10-24 14:30 +02:00: server listening on port 3100
logs/stderr.log
//empty...
This directive could be problem: 'max_memory_restart'. One megabyte for app is pretty low value. Try to increase to ~200MB - depending on your app memory requirements.
EDIT: this directive is expressing maximum amount of memory which can be consumpted. If process will allocate more, it will be restarted.

Trouble with VisualEditor in mediawiki

Hi I've installed mediawiki 1.26.2 with the extensión Visual Editor, nodejs and parsoid, the question is that when I start parsoid, it seems every process is working right but the the configuration of parsoid and visualeditor, I can't see any editor in my wiki.
I describe below all my configurations, how I start parsoid, the processes of parsoid involved and the configurations lines in localsettings of media wiki configuration file.
/etc/init.d/parsoid2 start-end script:
#!/bin/bash
#
# chkconfig: 35 90 12
# description: Foo server
#
# Get function from functions library
#. /etc/init.d/functions
# Start the service PARSOID
SCRIPT_PATH="/usr/lib/parsoid/src/bin/server.js"
DAEMON="/usr/bin/node $SCRIPT_PATH"
DAEMON_ARGS=""
start() {
#initlog -c "echo -n Starting PARSOID server: "
ulimit -n 64000
/usr/bin/node /usr/lib/parsoid/src/bin/server.js >> /var/log/parsoid/parsoid.log 2>&1 &
### Create the lock file ###
#touch /var/lock/subsys/parsoid
success $"PARSOID server startup"
echo
}
# Restart the service PARSOID
stop() {
#initlog -c "echo -n Stopping PARSOID server: "
pkill -f server.js
### Now, delete the lock file ###
rm -f /var/lock/subsys/parsoid
echo
}
### main logic ###
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status parsoid_nodejs.sh
;;
restart|reload|condrestart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|restart|reload|status}"
exit 1
esac
exit 0
processes of parsoid involved after I run /etc/init.d/parsoid2 start
root#vscj016mlinuxserver:~# ps -ef | grep parsoid
root 2244 1 0 08:21 pts/0 00:00:00 /usr/bin/node /usr/lib/parsoid/src/bin/server.js
root 2251 2244 0 08:21 pts/0 00:00:00 /usr/bin/nodejs /usr/lib/parsoid/src/bin/server.js
root 2252 2244 0 08:21 pts/0 00:00:00 /usr/bin/nodejs /usr/lib/parsoid/src/bin/server.js
root 2258 2244 0 08:21 pts/0 00:00:00 /usr/bin/nodejs /usr/lib/parsoid/src/bin/server.js
root 2264 2244 0 08:21 pts/0 00:00:00 /usr/bin/nodejs /usr/lib/parsoid/src/bin/server.js
root 2437 2023 0 08:36 pts/0 00:00:00 grep --color=auto parsoid
root#vscj016mlinuxserver:~#
the Localsetting.js parsoid configuration file:
exports.setup = function(parsoidConfig) {
// Set your own user-agent string
// Otherwise, defaults to "Parsoid/<current-version-defined-in- package.json>"
//parsoidConfig.userAgent = "My-User-Agent-String";
// Configure Parsoid to point to your MediaWiki instance.
parsoidConfig.setMwApi({
// The "prefix" is the name given to this wiki configuration in the
// (deprecated) Parsoid v1 API.
prefix: 'localhost', // optional
// The "domain" is used for communication with Visual Editor
// and RESTBase. It defaults to the hostname portion of
// the `uri` property below, but you can manually set it
// to an arbitrary string.
domain: 'localhost', // optional
// This is the only required parameter:
// the URL of you MediaWiki API endpoint.
uri: 'http://localhost/mediawiki/api.php',
// To specify a proxy (or proxy headers) specific to this prefix
// (which overrides defaultAPIProxyURI). Alternatively, set `proxy`
// to `null` to override and force no proxying when a default proxy
// has been set.
/*
proxy: {
uri: 'http://my.proxy:1234/',
headers: { 'X-Forwarded-Proto': 'https' } // headers are optional
}
*/
});
The configuration for VisualEditor at /var/www/HTML/mediawiki/Localsettings.php:
require_once "$IP/extensions/VisualEditor/VisualEditor.php";
wfLoadExtension ( 'VisualEditor' );
$wgDefaultUserOptions['visualeditor-enable'] = 1;
$wgDefaultUserOptions['minordefault'] = 1;
$wgHiddenPrefs[] = 'visualeditor-enable';
$wgVisualEditorParsoidURL = 'http://localhost:8000';
$wgVirtualRestConfig['modules']['parsoid'] = array('url' => 'http://localhost:8000', 'domain' => 'localhost', 'prefix' => 'localhost');
$wgSessionsInObjectCache = true;
$wgVirtualRestConfig['modules']['parsoid']['forwardCookies'] = true;
Please ensure that your parsoid version match your Visual Editor version, there is a chance that you should use old-way to configure Visual Editor:
$wgVisualEditorParsoidURL = 'http://127.0.0.1:8000';
$wgVisualEditorParsoidPrefix = 'localhost';
If you see the button "Edit source" on your pages only, make sure the Visual Editor is enabled for some name spaces in `LocalSettings.phpExample:
$wgVisualEditorNamespaces = array(NS_MAIN, NS_USER);
Source: https://www.mediawiki.org/wiki/Extension:VisualEditor#Complete_list_of_configuration_options
Check if Visual Editor is enabled in the User Preferences and you see the enabled name spaces as well

how to get the list of process

I am playing around with node and just installed it on my machine. Now I want to get a list of processes running on my machine so I can see whether Apache is running, MySQL is started, etc? How can I do that? I just have very basic code in my js file. I don't even know where to begin on this.
Here is my code:
var http = require('http');
http.createServer(function(request, response){
response.writeHead(200);
response.write("Hello world");
console.log('Listenning on port 1339');
response.end();
}).listen(8080);
As far as I know there isn't a module (yet) to do this cross-platform. You can use the child process API to launch tools that will give the data you want. For Windows, just launch the built-in tasklist process.
var exec = require('child_process').exec;
exec('tasklist', function(err, stdout, stderr) {
// stdout is a string containing the output of the command.
// parse it and look for the apache and mysql processes.
});
See ps-node
To get a list of processes in node:
var ps = require('ps-node');
ps.lookup({
command: 'node',
arguments: '--debug',
}, function(err, resultList ) {
if (err) {
throw new Error( err );
}
resultList.forEach(function( process ){
if( process ){
console.log( 'PID: %s, COMMAND: %s, ARGUMENTS: %s', process.pid, process.command, process.arguments );
}
});
});
ps-list is a better node package for the job, it is working on Linux, BSD, and Windows platforms too.
const psList = require('ps-list');
psList().then(data => {
console.log(data);
//=> [{pid: 3213, name: 'node', cmd: 'node test.js', cpu: '0.1'}, ...]
});
You can also use current-processes which lists all the processes.
https://www.npmjs.com/package/current-processes
The result includes name,pid,cpu and memory used by process.
You can also sort the result and limit the number of processes.
The result looks like this:
[ Process {
pid: 31834,
name: 'atom',
cpu: 84,
mem: { private: 19942400, virtual: 2048, usage: 0.4810941724241514 } }]
Solution for unix-like systems:
const child_process = require('child_process');
const displayProcessBy = (pattern) => {
let command = `ps -aux | grep ${pattern}`;
child_process.exec(command, (err, stdout, stdin) => {
if (err) throw err;
console.log(stdout);
});
}
Examples usage and results
displayProcessBy("nodejs");
setivol+ 7912 0.0 0.0 12732 2108 ? S 10:56 0:00 grep nodejs
setivol+ 12427 0.0 0.0 669552 712 pts/3 Tl Dec16 0:00 nodejs
setivol+ 14400 0.0 0.0 669552 644 pts/2 Tl Dec15 0:00 nodejs
setivol+ 14412 0.0 0.0 670576 224 pts/3 Tl Dec16 0:00 nodejs
setivol+ 14567 0.0 0.0 669552 436 pts/3 Tl Dec15 0:00 nodejs
setivol+ 14911 0.0 0.0 669552 0 pts/3 Tl Dec15 0:00 nodejs
setivol+ 15489 0.0 0.0 669552 712 pts/3 Tl Dec16 0:00 nodejs
setivol+ 15659 0.0 0.0 669520 0 pts/3 Tl Dec16 0:00 nodejs --harmony
setivol+ 16469 0.0 0.0 669520 704 pts/3 Tl Dec16 0:00 nodejs --harmony
setivol+ 20514 0.0 0.0 669552 664 pts/2 Tl Dec15 0:00 nodejs
displayProcessBy("python2")
setivol+ 8012 0.0 0.0 4336 712 ? S 10:58 0:00 /bin/sh -c ps -aux | grep python2
setivol+ 8014 0.0 0.0 12728 2240 ? S 10:58 0:00 grep python2
Testing environment
$ uname -a
Linux localhost 3.16.0-4-amd64 #1 SMP Debian 3.16.36-1+deb8u2 (2016-10-19) x86_64 GNU/Linux
$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 8.6 (jessie)
Release: 8.6
Codename: jessie
Seems there isn't any direct methods
but below videos might help.
Monitoring Long Running Processes with Node. (http://www.youtube.com/watch?v=zamGXhjim00)
CPU usage in real time with node.js & socket.io (http://www.youtube.com/watch?v=bPYgx9V6qAk)
Use the command line tools rather than node scripts.
ps -aef | grep node
This would list the visual studio code helpers and .nvm env processes. We can exclude them using
ps -aef | grep node | grep -v "Visual Studio" | grep -v ".nvm"
Additional tip to kill all the listed processes:
killall node

Resources