I want to use forever-monitor package to send me an email when my server crashes.
By going to the homepage I can see that an error is created and my server is restarted after the error. But I want to be able to capture these events (restart and error) so that I can log and send warning emails. But my debugger doesn't stop on error and restart events. Also, those console messages aren't printed.
I run it from command line as node myfilename.js, app.js is my main file.
Thanks for the help !
var forever = require('forever-monitor');
var child = new (forever.Monitor)('app.js', {
max: 3,
silent: false,
args: [],
});
child.on('error', function (err) {
console.log('An error occured '+err);
});
child.on('restart', function() {
console.log('Restarting file');
});
child.start();
setTimeout(function () {
throw new Error('I created an error');
}, 200);
Related
I have an express application running and I am using PM2 to keep it alive and restart when there is any error in the system. PM2 logs the error and restarts which is perfectly fine.
But I need to notify the user who submitted this with the error message. Is there any way or event or trigger which is activated when the restart happens or when the error is being written to the log file so that I can try to capture that and notify the actual user?
I implemented as per suggestion below,
var pm2 = require('pm2');
pm2.connect(function(err) {
if (err) {
console.error(err);
process.exit(2);
}
console.log("connected to pm2")
pm2.launchBus(function(err, bus) {
bus.on('log:err', function(e) {
// Send emails
console.log("error in pm2 send email");
});
});
});
I can see "connected to pm2" printed in output.log but I can't see "error in pm2 send email" printed anywhere. But I see an error log that I am triggering to force the error scenario in error.log.
1) You can use alert system from Keymetrics
2) You can connect to pm2 bus and catch errors:
// alerts.js
var pm2 = require('pm2')
pm2.launchBus(function(err, bus) {
bus.on('log:err', function(e) {
// Send emails
});
});
I have a NodeJs application running in the following directory
First Application's Path '/users/user1/projects/sampleProject' which is running at 3000 port.
Second Application's Path '/users/user1/demoProjects/demo1' which is going to run at 5000 port on triggering the router function from first application.
The second NodeJs application is not yet started(It will run at port 5000). It need to run independently on hitting a router function in the first NodeJs Application which is running on port 3000 ie(http://localhost:3000/server/startServer). I'm new to NodeJs child processes, Kindly correct me if i'm wrong. And suggest me a right way to do it. Thanks
Start another node application using node.js?
I have tried it like below
// First NodeJs application
import { exec } from "child_process";
router.get('/startServer', async (req, res, next) => {
console.log("Initiated request")
let startServerInstance = 'cd "/users/user1/demoProjects/demo1" && npm run dev'; // path for the second NodeJs application
console.log("Server instance path => " + startServerInstance)
try {
// exec from child process, Spawns a shell then executes the command within that shell
let child = exec(startServerInstance, function (err, stdout, stderr) {
if (err) throw err;
else {
console.log("result ")
res.json({
status: 'success'
});
}
});
} catch (error) {
res.json({
status: 'error',
message: error
});
}
});
The above code executes the command and triggered the second application to run in background but it doesn't return anything. Either error or success result.
You need to use stout and stderror to check other server logs. Also your code is not correct. If you use if without {} it will not go to else statement. That is why you don't see 'result' text in console.
import {
exec
} from "child_process";
router.get('/startServer', async (req, res, next) => {
console.log("Initiated request")
let startServerInstance = 'cd "/users/user1/demoProjects/demo1" && npm run dev'; // path for the second NodeJs application
console.log("Server instance path => " + startServerInstance)
try {
// exec from child process, Spawns a shell then executes the command within that shell
let child = exec(startServerInstance, function(err) {
if (err) throw err;
console.log("Server started");
});
child.stdout.on('data', (data) => {
// this is new server output
console.log(data.toString());
});
child.stderr.on('data', (data) => {
// this is new server error output
console.log(data.toString());
});
res.json({
status: 'success'
});
} catch (error) {
res.json({
status: 'error',
message: error
});
}
});
Child process callback is only called once the process terminates. If the process keeps running, callback is not triggered.
Explained here - https://nodejs.org/docs/latest-v10.x/api/child_process.html#child_process_child_process_exec_command_options_callback
I have an express application running and I am using PM2 to keep it alive and restart when there is any error in the system. PM2 logs the error and restarts which is perfectly fine.
But I need to notify the user who submitted this with the error message. Is there any way or event or trigger which is activated when the restart happens or when the error is being written to the log file so that I can try to capture that and notify the actual user?
I implemented as per suggestion below,
var pm2 = require('pm2');
pm2.connect(function(err) {
if (err) {
console.error(err);
process.exit(2);
}
console.log("connected to pm2")
pm2.launchBus(function(err, bus) {
bus.on('log:err', function(e) {
// Send emails
console.log("error in pm2 send email");
});
});
});
I can see "connected to pm2" printed in output.log but I can't see "error in pm2 send email" printed anywhere. But I see an error log that I am triggering to force the error scenario in error.log.
1) You can use alert system from Keymetrics
2) You can connect to pm2 bus and catch errors:
// alerts.js
var pm2 = require('pm2')
pm2.launchBus(function(err, bus) {
bus.on('log:err', function(e) {
// Send emails
});
});
Is there any alternative to PHP error_reporting(0) in nodejs? I want to prevent app from closing after an error, I tried the try and catch method, but it doesn't work.
So, how can I prevent node.js server from closing after an error ?
Edit:
There is an event for uncaught errors:
process.on('uncaughtException', function (err) {
console.log('Caught exception: ' + err);
});
See duplicate: Make node.js not exit on error
You could also use pm2 to run your script, pm2 will automatically restart your script on crash.
You need to catch the error.
For example
try {
error();
} catch(err) {
// do nothing
}
There is no other way as far as i know. So you could fix or catch these errors only.
After i review source code of node, I found there is a beautiful method to implement "ctrl+c" existing.
Create file block.js, content is:
const readline = require('readline')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
const block = (data) => {
process.on('uncaughtException', function (err) {
console.log('Caught exception: ' + err)
})
rl.on('SIGINT', () => {
console.log(data)
rl.pause()
})
}
module.exports = {
block
}
Then require this js file in whatever main js file, and run this:
const { block } = require('./block.js')
block('Your exiting message!')
// your other js code after here
// ...
The block.js file would catch err and "ctrl+c" SIGINT exit.
With this code where deliberately I create some error, why nodejs/socket.io stop respond to the client? Note that nodejs process still up and nothing crash nor exit.
socket.on('message', function (data) {
var d = domain.create();
d.on('error', function(err) {
socket.emit('error', err.message);
});
d.run(function() {
execError();
}
});
Everything ok, was my code that create the issue.
More here about domains:
http://blog.evantahler.com/blog/on-domains-and-connections-with-node-js.html
https://gist.github.com/evantahler/4274698