mirror of
https://github.com/bluewave-labs/Checkmate.git
synced 2026-01-14 21:59:42 -06:00
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:
@@ -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);
|
||||
|
||||
@@ -14,6 +14,10 @@ const MonitorSchema = mongoose.Schema(
|
||||
description: {
|
||||
type: String,
|
||||
},
|
||||
status: {
|
||||
type: Boolean,
|
||||
default: true,
|
||||
},
|
||||
type: {
|
||||
type: String,
|
||||
required: true,
|
||||
|
||||
Reference in New Issue
Block a user