Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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