Skip to content

Commit

Permalink
Feat: Retries persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
chakflying committed Sep 28, 2023
1 parent e798011 commit 243a747
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 29 deletions.
15 changes: 15 additions & 0 deletions db/knex_migrations/2023-09-29-0000-heartbeat-retires.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
exports.up = function (knex) {
// Add new column heartbeat.retries
return knex.schema
.alterTable("heartbeat", function (table) {
table.integer("retries").notNullable().defaultTo(0);
});

};

exports.down = function (knex) {
return knex.schema
.alterTable("heartbeat", function (table) {
table.dropColumn("retries");
});
};
15 changes: 8 additions & 7 deletions server/model/heartbeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ class Heartbeat extends BeanModel {
*/
toJSON() {
return {
monitorID: this.monitor_id,
status: this.status,
time: this.time,
msg: this.msg,
ping: this.ping,
important: this.important,
duration: this.duration,
monitorID: this._monitorId,
status: this._status,
time: this._time,
msg: this._msg,
ping: this._ping,
important: this._important,
duration: this._duration,
retries: this._retries,
};
}

Expand Down
14 changes: 10 additions & 4 deletions server/model/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ class Monitor extends BeanModel {
previousBeat = await R.findOne("heartbeat", " monitor_id = ? ORDER BY time DESC", [
this.id,
]);
if (previousBeat) {
retries = previousBeat.retries;
}
}

const isFirstBeat = !previousBeat;
Expand Down Expand Up @@ -596,6 +599,7 @@ class Monitor extends BeanModel {
// If the previous beat was down or pending we use the regular
// beatInterval/retryInterval in the setTimeout further below
if (previousBeat.status !== (this.isUpsideDown() ? DOWN : UP) || msSinceLastBeat > beatInterval * 1000 + bufferTime) {
bean.duration = Math.round(msSinceLastBeat / 1000);
throw new Error("No heartbeat in the time window");
} else {
let timeout = beatInterval * 1000 - msSinceLastBeat;
Expand Down Expand Up @@ -871,9 +875,14 @@ class Monitor extends BeanModel {
} else if ((this.maxretries > 0) && (retries < this.maxretries)) {
retries++;
bean.status = PENDING;
} else {
// Continue counting retries during DOWN
retries++;
}
}

bean.retries = retries;

log.debug("monitor", `[${this.name}] Check isImportant`);
let isImportant = Monitor.isImportantBeat(isFirstBeat, previousBeat?.status, bean.status);

Expand Down Expand Up @@ -1384,10 +1393,7 @@ class Monitor extends BeanModel {
* @returns {Promise<LooseObject<any>>} Previous heartbeat
*/
static async getPreviousHeartbeat(monitorID) {
return await R.getRow(`
SELECT ping, status, time FROM heartbeat
WHERE id = (select MAX(id) from heartbeat where monitor_id = ?)
`, [
return await R.findOne("heartbeat", " id = (select MAX(id) from heartbeat where monitor_id = ?)", [
monitorID
]);
}
Expand Down
80 changes: 62 additions & 18 deletions server/routers/api-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,38 +53,31 @@ router.get("/api/push/:pushToken", async (request, response) => {

const previousHeartbeat = await Monitor.getPreviousHeartbeat(monitor.id);

if (monitor.isUpsideDown()) {
status = flipStatus(status);
}

let isFirstBeat = true;
let previousStatus = status;
let duration = 0;

let bean = R.dispense("heartbeat");
bean.time = R.isoDateTimeMillis(dayjs.utc());
bean.monitor_id = monitor.id;
bean.ping = ping;
bean.msg = msg;

if (previousHeartbeat) {
isFirstBeat = false;
previousStatus = previousHeartbeat.status;
duration = dayjs(bean.time).diff(dayjs(previousHeartbeat.time), "second");
bean.duration = dayjs(bean.time).diff(dayjs(previousHeartbeat.time), "second");
}

if (await Monitor.isUnderMaintenance(monitor.id)) {
msg = "Monitor under maintenance";
status = MAINTENANCE;
bean.status = MAINTENANCE;
} else {
determineStatus(status, previousHeartbeat, monitor.maxretries, monitor.isUpsideDown(), bean);
}

log.debug("router", `/api/push/ called at ${dayjs().format("YYYY-MM-DD HH:mm:ss.SSS")}`);
log.debug("router", "PreviousStatus: " + previousStatus);
log.debug("router", "Current Status: " + status);
log.debug("router", "PreviousStatus: " + previousHeartbeat?.status);
log.debug("router", "Current Status: " + bean.status);

bean.important = Monitor.isImportantBeat(isFirstBeat, previousStatus, status);
bean.monitor_id = monitor.id;
bean.status = status;
bean.msg = msg;
bean.ping = ping;
bean.duration = duration;
bean.important = Monitor.isImportantBeat(isFirstBeat, previousHeartbeat?.status, status);

await R.store(bean);

Expand All @@ -97,7 +90,7 @@ router.get("/api/push/:pushToken", async (request, response) => {
ok: true,
});

if (Monitor.isImportantForNotification(isFirstBeat, previousStatus, status)) {
if (Monitor.isImportantForNotification(isFirstBeat, previousHeartbeat?.status, status)) {
await Monitor.sendNotification(isFirstBeat, monitor, bean);
}

Expand Down Expand Up @@ -553,4 +546,55 @@ router.get("/api/badge/:id/response", cache("5 minutes"), async (request, respon
}
});

/**
* Determines the status of the next beat in the push route handling.
* @param {string} status - The reported new status.
* @param {object} previousHeartbeat - The previous heartbeat object.
* @param {number} maxretries - The maximum number of retries allowed.
* @param {boolean} isUpsideDown - Indicates if the monitor is upside down.
* @param {object} bean - The new heartbeat object.
* @returns {void}
*/
function determineStatus(status, previousHeartbeat, maxretries, isUpsideDown, bean) {
console.log(status, previousHeartbeat, maxretries, isUpsideDown, bean);

if (isUpsideDown) {
status = flipStatus(status);
}

if (previousHeartbeat) {
if (previousHeartbeat.status === UP && status === DOWN) {
// Going Down
if ((maxretries > 0) && (previousHeartbeat.retries < maxretries)) {
// Retries available
bean.retries = previousHeartbeat.retries + 1;
bean.status = PENDING;
} else {
// No more retries
bean.retries = 0;
bean.status = DOWN;
}
} else if (previousHeartbeat.status === PENDING && status === DOWN && previousHeartbeat.retries < maxretries) {
// Retries available
bean.retries = previousHeartbeat.retries + 1;
bean.status = PENDING;
} else {
// No more retries or not pending
bean.retries = 0;
bean.status = status;
}
} else {
// First beat?
if (status === DOWN && maxretries > 0) {
// Retries available
bean.retries = 1;
bean.status = PENDING;
} else {
// Retires not enabled
bean.retries = 0;
bean.status = status;
}
}
}

module.exports = router;

0 comments on commit 243a747

Please sign in to comment.