Prevent generating duplicated timeslots

This commit is contained in:
Louis Lam 2023-03-09 22:03:23 +08:00
parent a599f5149b
commit 533bc1505b
2 changed files with 27 additions and 3 deletions

View file

@ -58,7 +58,14 @@ class MaintenanceTimeslot extends BeanModel {
bean.start_date = maintenance.start_date; bean.start_date = maintenance.start_date;
bean.end_date = maintenance.end_date; bean.end_date = maintenance.end_date;
bean.generated_next = true; bean.generated_next = true;
return await R.store(bean);
if (!await this.isDuplicateTimeslot(bean)) {
await R.store(bean);
return bean;
} else {
log.debug("maintenance", "Duplicate timeslot, skip");
return null;
}
} else if (maintenance.strategy === "recurring-interval") { } else if (maintenance.strategy === "recurring-interval") {
// Prevent dead loop, in case interval_day is not set // Prevent dead loop, in case interval_day is not set
@ -144,6 +151,15 @@ class MaintenanceTimeslot extends BeanModel {
} }
} }
static async isDuplicateTimeslot(timeslot) {
let bean = await R.findOne("maintenance_timeslot", "maintenance_id = ? AND start_date = ? AND end_date = ?", [
timeslot.maintenance_id,
timeslot.start_date,
timeslot.end_date
]);
return bean !== null;
}
/** /**
* Generate a next timeslot for all recurring types * Generate a next timeslot for all recurring types
* @param maintenance * @param maintenance
@ -161,7 +177,7 @@ class MaintenanceTimeslot extends BeanModel {
// Keep generating from the first possible date, until it is ok // Keep generating from the first possible date, until it is ok
while (true) { while (true) {
log.debug("timeslot", "startDateTime: " + startDateTime.format()); //log.debug("timeslot", "startDateTime: " + startDateTime.format());
// Handling out of effective date range // Handling out of effective date range
if (startDateTime.diff(dayjs.utc(maintenance.end_date)) > 0) { if (startDateTime.diff(dayjs.utc(maintenance.end_date)) > 0) {
@ -193,7 +209,14 @@ class MaintenanceTimeslot extends BeanModel {
bean.start_date = localToUTC(startDateTime); bean.start_date = localToUTC(startDateTime);
bean.end_date = localToUTC(endDateTime); bean.end_date = localToUTC(endDateTime);
bean.generated_next = false; bean.generated_next = false;
return await R.store(bean);
if (!await this.isDuplicateTimeslot(bean)) {
await R.store(bean);
return bean;
} else {
log.debug("maintenance", "Duplicate timeslot, skip");
return null;
}
} }
} }

View file

@ -271,6 +271,7 @@ class UptimeKumaServer {
/** Load the timeslots for maintenance */ /** Load the timeslots for maintenance */
async generateMaintenanceTimeslots() { async generateMaintenanceTimeslots() {
log.debug("maintenance", "Routine: Generating Maintenance Timeslots");
// Prevent #2776 // Prevent #2776
// Remove duplicate maintenance_timeslot with same start_date, end_date and maintenance_id // Remove duplicate maintenance_timeslot with same start_date, end_date and maintenance_id