Call function/script when server crashes (PM2?) [duplicate] - node.js

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
});
});

Related

How can you set up a nodejs web server to run asynchronously?

Okay, so I'm in the process of creating a Minecraft panel. I have a windows service, and that will call my express server. The problem is, that when the computer tries to start the process, it goes on infinitely because the server will run for eternity. I need this to be changed so that when it runs it will start up the web server, make sure it's running, and then finish the task and forget the webserver exists, but leave it running. How would this be possible? Many thanks :)
Update: After a little bit of though I'm going to refine my question to this: How can I start a process and not wait for it to finish in C#/nodejs(either work)
Edit: Lol i am refreshing this page like every two microseconds.... Its 4 AM for me so yeah :D brain - broke
Answer: So I figured out how to fix it. I used pm2 which can start a process and not wait for it to finish. We have a start.js and a stop.js which can start it. If you want to build something like it here is the code I used.
//Start.js
const pm2 = require('pm2');
pm2.connect((err) => {
if (err) {
console.log(err);
}
});
pm2.start({
name: 'webserver',
script: "webserver.js",
}, (err) => {
if (err) {
console.log(err)
} else {
console.log('Webserver started');
pm2.disconnect();
process.exit(0)
}
})
//Stop.js
const pm2 = require('pm2');
//Stop the webserver
pm2.stop('webserver', (err) => {
if (err) {
console.log(err);
} else {
console.log('Webserver stopped');
pm2.disconnect();
process.exit(0)
}
});

Electron handle crashes gracefully

I'm trying to implement libp2p inside an electron application, which (obviously) has node support turned on.
I tried catching errors with the following code:
const errorHandle = `
window.onerror = (err) => {
console.log(err);
};
process.on("uncaughtException", function (err) {
console.log(err);
});
process.on("unhandledRejection", function (err) {
console.log(err);
});
console.log("Injected startup code")`
await mainWindow.loadFile("./public/index.html");
await mainWindow.webContents.executeJavaScript(errorHandle);
However if I throw an error deliberately, or by accident, the app crashes, and gets reloaded, and I can't see the error, because chrome devtools clears its console, and prints out the "Devtools disconnected" message.
2 examples of error throwing:
console.error("test");
node.connect("INVALID_ADDRESS");
How can I properly handle errors, so electron (or node) doesn't crash?
As it turns out, the error was handled correctly, however the event was connected to a form, which by default refreshes the page if submitted. I had to cancel the default event by:
<form onsubmit="return false" on:submit={submit}></form>

Is it possible to get the process metadata from PM2?

I was wondering if it was possible to get process metadata using pm2 inside my node application.
Yes, you can get any information from pm2 inside your app. below will return all running process list. For further details, you can check pm2-api
var pm2 = require('pm2');
app.use('/all_process_list', function(req,res){
pm2.connect(function(err) {
if (err) {
console.error(err);
process.exit(2);
}
pm2.list(function(err, processDescriptionList) {
console.log(processDescriptionList)
res.json ({process_list:processDescriptionList})
pm2.disconnect(); // Disconnects from PM2
});
});
});

Is there a way or a trigger to notify when there is an error and restart happened with PM2

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
});
});

forever-monitor send email on crash

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);

Resources