Skip to content

Instantly share code, notes, and snippets.

@erayarslan
Created January 5, 2017 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erayarslan/c9477d51683daeeb0af09d60d2fdb840 to your computer and use it in GitHub Desktop.
Save erayarslan/c9477d51683daeeb0af09d60d2fdb840 to your computer and use it in GitHub Desktop.
mongodb health checker
var isAlive = require("./is-alive");
var Config = require("./config");
var app = new isAlive(Config.mongodb)
.mail(Config)
.success(function (db) {
console.log("[IS-ALIVE] alive!");
this.setStatus(false);
db.close();
})
.error(function (e) {
console.log("[IS-ALIVE] not alive :<");
if (!this.getStatus()) {
this.send(e.message);
}
})
.connect()
.job(Config.interval);
{
"mongodb": "",
"email": {
"user": "",
"password": "",
"host": "",
"ssl": false
},
"subject": "is-alive reporter",
"to": "",
"from": "",
"interval": 5000
}
var MongoDB = require('mongodb');
var EmailJS = require("emailjs");
var isAlive = function (url) {
this.client = MongoDB.MongoClient;
this.url = url;
this.status = false;
return this;
};
isAlive.prototype.connect = function () {
if (!!this.done && !!this.fail) {
this.client.connect(this.url, (function (e, db) {
!!e ? this.fail(e) : this.done(db);
}).bind(this));
return this;
} else {
throw new Error("success or error callback not defined.");
}
};
/**
* @param {String} options.email
* @param {String} options.to
* @param {String} options.from
* @param {String} options.subject
*/
isAlive.prototype.mail = function (options) {
this.server = EmailJS.server.connect(options.email);
this.to = options.to;
this.from = options.from;
this.subject = options.subject;
return this;
};
isAlive.prototype.success = function (success) {
this.done = success.bind(this);
return this;
};
isAlive.prototype.error = function (error) {
this.fail = error.bind(this);
return this;
};
isAlive.prototype.getStatus = function () {
return this.status;
};
isAlive.prototype.setStatus = function (status) {
this.status = status;
};
isAlive.prototype.send = function (message) {
this.server.send({
text: this.subject, from: this.from,
to: this.to, subject: this.subject,
attachment: [{data: message, alternative: true}]
}, (function (err, out) {
if (err) {
console.error("[IS-ALIVE] error", err.smtp);
} else {
console.log("[IS-ALIVE]", out.header.to);
this.setStatus(true);
}
}).bind(this));
};
isAlive.prototype.job = function (interval) {
setInterval(this.connect.bind(this), interval);
return this;
};
module.exports = isAlive;
{
"name": "is-alive",
"version": "0.0.1",
"private": true,
"dependencies": {
"mongodb": "2.1.14",
"emailjs": "1.0.4"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment