Created
January 5, 2017 08:42
-
-
Save erayarslan/c9477d51683daeeb0af09d60d2fdb840 to your computer and use it in GitHub Desktop.
mongodb health checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"mongodb": "", | |
"email": { | |
"user": "", | |
"password": "", | |
"host": "", | |
"ssl": false | |
}, | |
"subject": "is-alive reporter", | |
"to": "", | |
"from": "", | |
"interval": 5000 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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