I'm using forever
to run my Node.js server and I very like it's start
/stop
/restart
command line commands. Recently I decided that I need system that will email me about all server crashes. Unfortunately forever
doesn't support such kind of functionality out of the box.
So I decided to take forever-monitor
(the tool like forever
but with programmatic approach) and nodemailer
. Here is a script I wrote:
var forever = require('forever-monitor');
var nodemailer = require('nodemailer');
var sendmailTransport = require('nodemailer-sendmail-transport');
var transporter = nodemailer.createTransport(sendmailTransport({
path: '/usr/sbin/sendmail'
}));
var error = '';
function sendMail(subject, text) {
var message = {
// sender info
from: '"Server name" <[email protected]>',
// Comma separated list of recipients
to: '"My name" <[email protected]>',
// Subject of the message
subject: subject,
// plaintext body
text: text
};
transporter.sendMail(message, function (err) {
if (err) {
console.log('Error during trial email with server error report');
console.log(err);
}
});
}
var child = new (forever.Monitor)('app.js', {
outFile: './logs/stdout.log',
errFile: './logs/stderr.log'
});
child.on('stderr', function (data) {
error += data;
});
// Check for errors every second
setInterval(function () {
if (error) {
sendMail('Your server crashed', error);
error = '';
}
}, 1000);
child.start();
I called this file start.js
. All it does is: spawns new process (app.js
, which runs my Express web-server) with forever-monitor
, I listen for stderr
event of this child process and collect all errors into variable, then I check this variable every second and if it doesn't empty I send an email and clear variable. This code works fine.
Now to start my server I need execute something like nohup node start.js &
. The bad thing that there is no easy way to stop or restart my server. Previously I did it with forever stopall
and forever restartall
which is very convenient.
My next thought was try to combine forever
and forever-monitor
so I tried to start server like this forever start start.js
. Server started just like I supposed. But restarting didn't work anymore. Executing forever stopall
and then forever start start.js
didn't work too. Server started to complain that port is already busy. I assume it happens because forever
stops start.js
process, but the child app.js
process spawned by forever-monitor
still was occupying the port.
So my question is: how to run Node.js server, receive emails about it's crashes, automatically restart it when it crashed and have possibility to easily start
/stop
/restart
it like with forever
command line utility? Solution can use forever
, forever-monitor
or both of them, or actually any library you think may work for this kind of problem.
I think you should fork forever's git repository and add emails notifications in source code. This way you can still use forever CLI without additional scripts.
If you're not familiar with concept check out: https://help.github.com/articles/fork-a-repo/
Seems like such a feature have been already requested but without much reply.
With working solution you can either add your (forked) repo to your package.json or contribute to forever project by requesting pull. Maybe thanks to you forever will have email notifications.