Repository: louislam/uptime-kuma
File: server/model/maintenance.js (JavaScript)

Code before:
                    let duration;

                    if (customDuration > 0) {
                        duration = customDuration;
                    } else if (this.end_date) {
                        let d = dayjs(this.end_date).diff(dayjs(), "second");
                        if (d < this.duration) {
                            duration = d * 1000;
                        }
                    } else {
                        duration = this.duration * 1000;
                    }

                    if (duration === undefined && this.strategy === "recurring-interval") {
                        duration = this.duration * 1000; // For recurring-interval, the duration needs to be defined
                    }

                    UptimeKumaServer.getInstance().sendMaintenanceListByUserID(this.user_id);

                    this.beanMeta.durationTimeout = setTimeout(() => {
                        // End of maintenance for this timeslot
                        this.beanMeta.status = "scheduled";
                        UptimeKumaServer.getInstance().sendMaintenanceListByUserID(this.user_id);
                    }, duration);
                };

                // Create Cron
                if (this.strategy === "recurring-interval") {
                    // For recurring-interval, Croner needs to have interval and startAt
                    const startDate = dayjs(this.startDate);
                    const [ hour, minute ] = this.startTime.split(":");
                    const startDateTime = startDate.hour(hour).minute(minute);
                    this.beanMeta.job = new Cron(this.cron, {
                        timezone: await this.getTimezone(),
                        interval: this.interval_day * 24 * 60 * 60,
                        startAt: startDateTime.toISOString(),
                    }, startEvent);
                } else {
                    this.beanMeta.job = new Cron(this.cron, {
                        timezone: await this.getTimezone(),
                    }, startEvent);
                }

                // Continue if the maintenance is still in the window
                let runningTimeslot = this.getRunningTimeslot();
                let current = dayjs();

                if (runningTimeslot) {
                    let duration = dayjs(runningTimeslot.endDate).diff(current, "second") * 1000;
                    log.debug("maintenance", "Maintenance id: " + this.id + " Remaining duration: " + duration + "ms");
                    startEvent(duration);

Review comment (suggestion):
nit: For these if-statements, I like to pull code which can be shared out, but am fine with your solution.
(feel free to accept/reject the below attached suggestion)

```suggestion
                const startDate = dayjs(this.startDate);
                const [ hour, minute ] = this.startTime.split(":");
                const startDateTime = startDate.hour(hour).minute(minute);
                const cronerOptions = {
                    timezone: await this.getTimezone(),
                    startAt: startDateTime.toISOString(),
                };
                if (this.strategy === "recurring-interval") {
                    cronerOptions.interval = this.interval_day * 24 * 60 * 60;
                }
                this.beanMeta.job = new Cron(this.cron, cronerOptions, startEvent);
```

Code after:

                    UptimeKumaServer.getInstance().sendMaintenanceListByUserID(this.user_id);

                    this.beanMeta.durationTimeout = setTimeout(() => {
                        // End of maintenance for this timeslot
                        this.beanMeta.status = "scheduled";
                        UptimeKumaServer.getInstance().sendMaintenanceListByUserID(this.user_id);
                    }, duration);
                };

                // Create Cron
                if (this.strategy === "recurring-interval") {
                    // For recurring-interval, Croner needs to have interval and startAt
                    const startDate = dayjs(this.startDate);
                    const [ hour, minute ] = this.startTime.split(":");
                    const startDateTime = startDate.hour(hour).minute(minute);
                    this.beanMeta.job = new Cron(this.cron, {
                        timezone: await this.getTimezone(),
                        interval: this.interval_day * 24 * 60 * 60,
                        startAt: startDateTime.toISOString(),
                    }, startEvent);
                } else {
                    this.beanMeta.job = new Cron(this.cron, {
                        timezone: await this.getTimezone(),
                    }, startEvent);
                }

                // Continue if the maintenance is still in the window
                let runningTimeslot = this.getRunningTimeslot();
                let current = dayjs();

                if (runningTimeslot) {
                    let duration = dayjs(runningTimeslot.endDate).diff(current, "second") * 1000;
                    log.debug("maintenance", "Maintenance id: " + this.id + " Remaining duration: " + duration + "ms");
                    startEvent(duration);
                }

            } catch (e) {
                log.error("maintenance", "Error in maintenance id: " + this.id);
                log.error("maintenance", "Cron: " + this.cron);
                log.error("maintenance", e);

                if (throwError) {
                    throw e;
                }
            }

        } else {
            log.error("maintenance", "Maintenance id: " + this.id + " has no cron");
        }
    }