Skip to content
Browse files

Initial commit

  • Loading branch information...
0 parents commit 387d6a28330ac70dcadb5ff64c358256a3bba950 @BerkeleyTrue BerkeleyTrue committed
Showing with 104 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +3 −0 README.md
  3. +69 −0 index.js
  4. +30 −0 package.json
2 .gitignore
@@ -0,0 +1,2 @@
+*.env
+node_modules
3 README.md
@@ -0,0 +1,3 @@
+PM2 Pager
+
+Get notifications on exception from pm2 through nodemailer
69 index.js
@@ -0,0 +1,69 @@
+require('dotenv').load();
+var pm2 = require('pm2');
+var nodemailer = require('nodemailer');
+var moment = require('moment-timezone');
+var _ = require('lodash');
+
+var mailReceiver = process.env.MAIL_RECEIVER || false;
+var mailSender = process.env.MAIL_SENDER;
+var user = process.env.MANDRILL_USER || false;
+var pass = process.env.MANDRILL_PASSWORD;
+
+if (!mailReceiver || !user || !pass) {
+ throw new Error('User || pass || receiver not specified');
+}
+
+var transportOptions = {
+ type: 'smtp',
+ service: 'Mandrill',
+ auth: {
+ user: user,
+ pass: pass
+ }
+};
+
+var transporter = nodemailer.createTransport(transportOptions);
+
+pm2.connect(function(err) {
+ if (err) { throw err; }
+ console.log('connected to pm2');
+ console.log('setting up exception event listener');
+
+ var compiled = _.template(
+ 'An error has occurred on server ' +
+ '<% name %>\n' +
+ 'Stack Trace:\n\n\n<%= stack %>\n\n\n' +
+ 'Context:\n\n<%= text %>'
+ );
+
+ pm2.launchBus(function(err, bus) {
+ if (err) { throw err; }
+
+ console.log('event bus connected');
+
+ bus.on('process:exception', function(data) {
+ var text;
+ var stack;
+ var name;
+ try {
+ data.date = moment(data.at || new Date())
+ .tz('America/Los_Angeles')
+ .format('MMMM Do YYYY, h:mm:ss a z');
+
+ text = JSON.stringify(data, null, 2);
+ stack = data.data.stack;
+ name = data.process.name;
+ } catch (e) {
+ console.error('Error parsing exception' + e);
+ return e;
+ }
+
+ transporter.sendMail({
+ to: mailReceiver,
+ from: mailSender || user + '@yourserver.com',
+ subject: 'Server exception',
+ text: compiled({ name: name, text: text, stack: stack })
+ });
+ });
+ });
+});
30 package.json
@@ -0,0 +1,30 @@
+{
+ "name": "pm2-pager",
+ "version": "0.0.1",
+ "description": "Send email notification on exceptions using pm2 and node mailer",
+ "main": "index.js",
+ "scripts": {
+ "test": "echo 'Happy Coding!'"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git+https://github.com/FreeCodeCamp/pm2-pager.git"
+ },
+ "keywords": [
+ "pm2",
+ "email",
+ "page",
+ "pager"
+ ],
+ "author": "Berkeley Martinez <Berkeley@RoboTie.com> (http://RoboTie.com)",
+ "license": "BSD-3-Clause",
+ "bugs": {
+ "url": "https://github.com/FreeCodeCamp/pm2-pager/issues"
+ },
+ "homepage": "https://github.com/FreeCodeCamp/pm2-pager#readme",
+ "dependencies": {
+ "dotenv": "^2.0.0",
+ "moment-timezone": "^0.5.0",
+ "nodemailer": "^2.1.0"
+ }
+}

0 comments on commit 387d6a2

Please sign in to comment.
Something went wrong with that request. Please try again.