]> git.mxchange.org Git - friendica.git/commitdiff
Define a maintenance window
authorMichael <heluecht@pirati.ca>
Sat, 17 Oct 2020 12:39:42 +0000 (12:39 +0000)
committerMichael <heluecht@pirati.ca>
Sat, 17 Oct 2020 12:39:42 +0000 (12:39 +0000)
src/Core/Worker.php
src/Worker/Cron.php
src/Worker/ExpirePosts.php
static/defaults.config.php

index 426389ab00c7a706393902736a58e4cf89accd2b..9ab061baace6276526644390331fb1a5320708c0 100644 (file)
@@ -1394,4 +1394,48 @@ class Worker
 
                return (bool)$row['jobs'];
        }
+
+       /**
+        * Check if the system is inside the defined maintenance window
+        *
+        * @return boolean
+        */
+       public static function isInMaintenanceWindow(bool $check_last_execution = false)
+       {
+               // Calculate the seconds of the start end end of the maintenance window
+               $start = strtotime(DI::config()->get('system', 'maintenance_start')) % 86400;
+               $end = strtotime(DI::config()->get('system', 'maintenance_end')) % 86400;
+
+               Logger::info('Maintenance window', ['start' => date('H:i:s', $start), 'end' => date('H:i:s', $end)]);
+
+               if ($check_last_execution) {
+                       // Calculate the window duration
+                       $duration = max($start, $end) - min($start, $end);
+
+                       // Quit when the last cron execution had been after the previous window
+                       $last_cron = DI::config()->get('system', 'last_cron_daily');
+                       if ($last_cron + $duration > time()) {
+                               Logger::info('The Daily cron had been executed recently', ['last' => date(DateTimeFormat::MYSQL, $last_cron), 'start' => date('H:i:s', $start), 'end' => date('H:i:s', $end)]);
+                               return false;
+                       }
+               }
+
+               $current = time() % 86400;
+
+               if ($start < $end) {
+                       // Execute if we are inside the window
+                       $execute = ($current >= $start) && ($current <= $end);
+               } else {
+                       // Don't execute if we are outside the window
+                       $execute = !(($current > $end) && ($current < $start));
+               }
+
+               if (!$execute) {
+                       Logger::info('We are outside the maintenance window', ['current' => date('H:i:s', $current), 'start' => date('H:i:s', $start), 'end' => date('H:i:s', $end)]);
+               } else {
+                       Logger::info('We are inside the maintenance window', ['current' => date('H:i:s', $current), 'start' => date('H:i:s', $start), 'end' => date('H:i:s', $end)]);
+               }
+               
+               return $execute;
+       }
 }
index 90042e30f6631d039c7cdb72de6e8bd4f258cc74..3ce5fb4605a4aa505ccf5a4df72eff5ca2e9d6b3 100644 (file)
@@ -93,8 +93,8 @@ class Cron
                        DI::config()->set('system', 'last_cron_hourly', time());
                }
 
-               // Daily cron calls
-               if (DI::config()->get('system', 'last_cron_daily', 0) + 86400 < time()) {
+               // Daily maintenance cron calls
+               if (Worker::isInMaintenanceWindow(true)) {
 
                        Worker::add(PRIORITY_LOW, 'UpdateContactBirthdays');
 
index 09684fccc481a7198cb6dbc1586adf41c597766e..3a49453257a19218866834397029a3ec9b786e70 100644 (file)
@@ -22,6 +22,7 @@
 namespace Friendica\Worker;
 
 use Friendica\Core\Logger;
+use Friendica\Core\Worker;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Util\DateTimeFormat;
@@ -63,6 +64,11 @@ class ExpirePosts
 
                                $rows = DBA::affectedRows();
                                Logger::notice('Deleted expired threads', ['result' => $ret, 'rows' => $rows]);
+
+                               if (!Worker::isInMaintenanceWindow()) {
+                                       Logger::notice('We are outside of the maintenance window, quitting');
+                                       return;
+                               }
                        } while ($rows >= $limit);
                }
 
@@ -80,6 +86,11 @@ class ExpirePosts
 
                                $rows = DBA::affectedRows();
                                Logger::notice('Deleted unclaimed public items', ['result' => $ret, 'rows' => $rows]);
+
+                               if (!Worker::isInMaintenanceWindow()) {
+                                       Logger::notice('We are outside of the maintenance window, quitting');
+                                       return;
+                               }
                        } while ($rows >= $limit);
                }
        }
index 4174ebcad917d64dcb4ede8ce12984ceebb937d3..b0832d88f35f4bfc6358a5534286744e2e17ef11 100644 (file)
@@ -300,6 +300,16 @@ return [
                // Sets the logging adapter of Friendica globally (monolog, syslog, stream)
                'logger_config' => 'stream',
 
+               // maintenance_start (String)
+               // Start of the window for the daily maintenance cron call.
+               // The system timezone is used when no timezone is defined here.
+               'maintenance_start' => '01:00 +00:00',
+
+               // maintenance_end (String)
+               // End of the window for the daily maintenance cron call
+               // The system timezone is used when no timezone is defined here.
+               'maintenance_end' => '03:00 +00:00',
+
                // max_batch_queue (Integer)
                // Maximum number of batched queue items for a single contact before subsequent messages are discarded.
                'max_batch_queue' => 1000,