From d61688315def69a8d46b3ecde0c3c235b79e12f3 Mon Sep 17 00:00:00 2001
From: buzzinJohnnyBoi <john_campbell@email.com>
Date: Sat, 13 Jul 2024 13:58:36 -0400
Subject: [PATCH 1/7] Updated croner to 8.1.0 and fixed "recurring-interval"
 type maintenance

---
 package-lock.json           | 11 +++++------
 package.json                |  2 +-
 server/model/maintenance.js | 26 +++++++++++++++++++-------
 3 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/package-lock.json b/package-lock.json
index 918c3dd0..4af4f60d 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -24,7 +24,7 @@
                 "command-exists": "~1.2.9",
                 "compare-versions": "~3.6.0",
                 "compression": "~1.7.4",
-                "croner": "~6.0.5",
+                "croner": "^8.1.0",
                 "dayjs": "~1.11.5",
                 "dev-null": "^0.1.1",
                 "dotenv": "~16.0.3",
@@ -6738,12 +6738,11 @@
             }
         },
         "node_modules/croner": {
-            "version": "6.0.7",
-            "resolved": "https://registry.npmjs.org/croner/-/croner-6.0.7.tgz",
-            "integrity": "sha512-k3Xx3Rcclfr60Yx4TmvsF3Yscuiql8LSvYLaphTsaq5Hk8La4Z/udmUANMOTKpgGGroI2F6/XOr9cU9OFkYluQ==",
-            "license": "MIT",
+            "version": "8.1.0",
+            "resolved": "https://registry.npmjs.org/croner/-/croner-8.1.0.tgz",
+            "integrity": "sha512-sz990XOUPR8dG/r5BRKMBd15MYDDUu8oeSaxFD5DqvNgHSZw8Psd1s689/IGET7ezxRMiNlCIyGeY1Gvxp/MLg==",
             "engines": {
-                "node": ">=6.0"
+                "node": ">=18.0"
             }
         },
         "node_modules/cronstrue": {
diff --git a/package.json b/package.json
index 31a89a7c..21536283 100644
--- a/package.json
+++ b/package.json
@@ -89,7 +89,7 @@
         "command-exists": "~1.2.9",
         "compare-versions": "~3.6.0",
         "compression": "~1.7.4",
-        "croner": "~6.0.5",
+        "croner": "^8.1.0",
         "dayjs": "~1.11.5",
         "dev-null": "^0.1.1",
         "dotenv": "~16.0.3",
diff --git a/server/model/maintenance.js b/server/model/maintenance.js
index 516c0377..7aa824ff 100644
--- a/server/model/maintenance.js
+++ b/server/model/maintenance.js
@@ -253,6 +253,10 @@ class Maintenance extends BeanModel {
                         duration = this.duration * 1000;
                     }
 
+                    if (duration === undefined && this.strategy === "recurring-interval") {
+                        duration = this.duration * 1000;
+                    }
+
                     UptimeKumaServer.getInstance().sendMaintenanceListByUserID(this.user_id);
 
                     this.beanMeta.durationTimeout = setTimeout(() => {
@@ -263,9 +267,20 @@ class Maintenance extends BeanModel {
                 };
 
                 // Create Cron
-                this.beanMeta.job = new Cron(this.cron, {
-                    timezone: await this.getTimezone(),
-                }, startEvent);
+                if (this.strategy === "recurring-interval") {
+                    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();
@@ -395,10 +410,7 @@ class Maintenance extends BeanModel {
         } else if (!this.strategy.startsWith("recurring-")) {
             this.cron = "";
         } else if (this.strategy === "recurring-interval") {
-            let array = this.start_time.split(":");
-            let hour = parseInt(array[0]);
-            let minute = parseInt(array[1]);
-            this.cron = minute + " " + hour + " */" + this.interval_day + " * *";
+            this.cron = "* * * * *";
             this.duration = this.calcDuration();
             log.debug("maintenance", "Cron: " + this.cron);
             log.debug("maintenance", "Duration: " + this.duration);

From 4750bd1e3693869a0674d1ff11feab555724c71a Mon Sep 17 00:00:00 2001
From: buzzinJohnnyBoi <john_campbell@email.com>
Date: Sat, 13 Jul 2024 14:08:53 -0400
Subject: [PATCH 2/7] Fixed "recurring-interval" type maintenance - added
 comments

---
 server/model/maintenance.js | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/server/model/maintenance.js b/server/model/maintenance.js
index 7aa824ff..6eb5273b 100644
--- a/server/model/maintenance.js
+++ b/server/model/maintenance.js
@@ -254,7 +254,7 @@ class Maintenance extends BeanModel {
                     }
 
                     if (duration === undefined && this.strategy === "recurring-interval") {
-                        duration = this.duration * 1000;
+                        duration = this.duration * 1000; // For recurring-interval, the duration needs to be defined
                     }
 
                     UptimeKumaServer.getInstance().sendMaintenanceListByUserID(this.user_id);
@@ -268,6 +268,7 @@ class Maintenance extends BeanModel {
 
                 // 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);
@@ -410,7 +411,7 @@ class Maintenance extends BeanModel {
         } else if (!this.strategy.startsWith("recurring-")) {
             this.cron = "";
         } else if (this.strategy === "recurring-interval") {
-            this.cron = "* * * * *";
+            this.cron = "* * * * *"; // Because it is interval, it will be calculated in the run function
             this.duration = this.calcDuration();
             log.debug("maintenance", "Cron: " + this.cron);
             log.debug("maintenance", "Duration: " + this.duration);

From 7f64badb062ac28f7eccc33c9251c200918f0448 Mon Sep 17 00:00:00 2001
From: John Campbell <114193843+buzzinJohnnyBoi@users.noreply.github.com>
Date: Sat, 13 Jul 2024 22:03:13 -0400
Subject: [PATCH 3/7] Update comment on recurring-interval type maintenance

Co-authored-by: Frank Elsinga <frank@elsinga.de>
---
 server/model/maintenance.js | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/server/model/maintenance.js b/server/model/maintenance.js
index 6eb5273b..468cb2c4 100644
--- a/server/model/maintenance.js
+++ b/server/model/maintenance.js
@@ -411,7 +411,8 @@ class Maintenance extends BeanModel {
         } else if (!this.strategy.startsWith("recurring-")) {
             this.cron = "";
         } else if (this.strategy === "recurring-interval") {
-            this.cron = "* * * * *"; // Because it is interval, it will be calculated in the run function
+            // For intervals, the pattern is calculated in the run function as the interval-option is set
+            this.cron = "* * * * *";
             this.duration = this.calcDuration();
             log.debug("maintenance", "Cron: " + this.cron);
             log.debug("maintenance", "Duration: " + this.duration);

From 4836f72cb83e239d03c7fddb7725ffd91d0b313c Mon Sep 17 00:00:00 2001
From: buzzinJohnnyBoi <john_campbell@email.com>
Date: Mon, 15 Jul 2024 16:54:56 -0400
Subject: [PATCH 4/7] added inferDuration to calculate cron duration

---
 server/model/maintenance.js | 36 +++++++++++++++++++-----------------
 1 file changed, 19 insertions(+), 17 deletions(-)

diff --git a/server/model/maintenance.js b/server/model/maintenance.js
index 468cb2c4..c1643d5a 100644
--- a/server/model/maintenance.js
+++ b/server/model/maintenance.js
@@ -239,23 +239,7 @@ class Maintenance extends BeanModel {
                     this.beanMeta.status = "under-maintenance";
                     clearTimeout(this.beanMeta.durationTimeout);
 
-                    // Check if duration is still in the window. If not, use the duration from the current time to the end of the window
-                    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
-                    }
+                    let duration = this.inferDuration(customDuration);
 
                     UptimeKumaServer.getInstance().sendMaintenanceListByUserID(this.user_id);
 
@@ -327,6 +311,24 @@ class Maintenance extends BeanModel {
         }
     }
 
+    /**
+    * Calculate the maintenance duration
+    * @returns {number}
+    */
+
+    inferDuration(customDuration) {
+        // Check if duration is still in the window. If not, use the duration from the current time to the end of the window
+        if (customDuration > 0) {
+            return customDuration;
+        } else if (this.end_date) {
+            let d = dayjs(this.end_date).diff(dayjs(), "second");
+            if (d < this.duration) {
+                return d * 1000;
+            }
+        }
+        return this.duration * 1000;
+    }
+
     /**
      * Stop the maintenance
      * @returns {void}

From 30f7b4898758ea5422983d00a3d3a3597e7e1524 Mon Sep 17 00:00:00 2001
From: buzzinJohnnyBoi <john_campbell@email.com>
Date: Mon, 15 Jul 2024 17:04:16 -0400
Subject: [PATCH 5/7] updated comments and ran linter on inferDuration

---
 server/model/maintenance.js | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/server/model/maintenance.js b/server/model/maintenance.js
index c1643d5a..7111a18c 100644
--- a/server/model/maintenance.js
+++ b/server/model/maintenance.js
@@ -312,10 +312,10 @@ class Maintenance extends BeanModel {
     }
 
     /**
-    * Calculate the maintenance duration
-    * @returns {number}
-    */
-
+     * Calculate the maintenance duration
+     * @param {number} customDuration - The custom duration in milliseconds.
+     * @returns {number} The inferred duration in milliseconds.
+     */
     inferDuration(customDuration) {
         // Check if duration is still in the window. If not, use the duration from the current time to the end of the window
         if (customDuration > 0) {

From 76deb6e0fad31a34bf9040d9449655a441801b3a Mon Sep 17 00:00:00 2001
From: John Campbell <114193843+buzzinJohnnyBoi@users.noreply.github.com>
Date: Mon, 15 Jul 2024 17:21:27 -0400
Subject: [PATCH 6/7] package.json - Update dependency version range for croner

Co-authored-by: Louis Lam <louislam@users.noreply.github.com>
---
 package.json | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/package.json b/package.json
index 21536283..83ce1ede 100644
--- a/package.json
+++ b/package.json
@@ -89,7 +89,7 @@
         "command-exists": "~1.2.9",
         "compare-versions": "~3.6.0",
         "compression": "~1.7.4",
-        "croner": "^8.1.0",
+        "croner": "~8.1.0",
         "dayjs": "~1.11.5",
         "dev-null": "^0.1.1",
         "dotenv": "~16.0.3",

From e791c70c47583c942358c0518a3c6f6032d453a5 Mon Sep 17 00:00:00 2001
From: Frank Elsinga <frank@elsinga.de>
Date: Tue, 16 Jul 2024 00:29:56 +0200
Subject: [PATCH 7/7] Fixed the package-lock.json

---
 package-lock.json | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/package-lock.json b/package-lock.json
index 4af4f60d..7a567f37 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -24,7 +24,7 @@
                 "command-exists": "~1.2.9",
                 "compare-versions": "~3.6.0",
                 "compression": "~1.7.4",
-                "croner": "^8.1.0",
+                "croner": "~8.1.0",
                 "dayjs": "~1.11.5",
                 "dev-null": "^0.1.1",
                 "dotenv": "~16.0.3",