Merge pull request #251 from bluewave-labs/feat/monitor-status-change
Monitor now has a status, status will be updated on new check, #235
This commit is contained in:
2 files changed
+32
No files matched your search
@@ -31,4 +31,32 @@ const CheckSchema = mongoose.Schema(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* When a check is created, we should update the stauts of the associated monitor
|
||||||
|
* if it has changed. Monitor starts with default status: true
|
||||||
|
* @param {function} next - Callback to proceed to the next middleware.
|
||||||
|
*/
|
||||||
|
|
||||||
|
CheckSchema.pre("save", async function (next) {
|
||||||
|
try {
|
||||||
|
const monitor = await mongoose.model("Monitor").findById(this.monitorId);
|
||||||
|
if (monitor && monitor.status !== this.status) {
|
||||||
|
if (monitor.status === true && this.status === false) {
|
||||||
|
// TODO issue alert
|
||||||
|
console.log("Monitor went down");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (monitor.status === false && this.status === true) {
|
||||||
|
// TODO issue alert
|
||||||
|
console.log("Monitor went up");
|
||||||
|
}
|
||||||
|
await monitor.save();
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
} finally {
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
module.exports = mongoose.model("Check", CheckSchema);
|
module.exports = mongoose.model("Check", CheckSchema);
|
||||||
@@ -14,6 +14,10 @@ const MonitorSchema = mongoose.Schema(
|
|||||||
description: {
|
description: {
|
||||||
type: String,
|
type: String,
|
||||||
},
|
},
|
||||||
|
status: {
|
||||||
|
type: Boolean,
|
||||||
|
default: true,
|
||||||
|
},
|
||||||
type: {
|
type: {
|
||||||
type: String,
|
type: String,
|
||||||
required: true,
|
required: true,
|
||||||
|
|||||||
Reference in new issue
Block a user