]> git.mxchange.org Git - friendica.git/commitdiff
Adding a cooldown phase for the daemon
authorMichael <heluecht@pirati.ca>
Wed, 19 Aug 2020 18:21:40 +0000 (18:21 +0000)
committerMichael <heluecht@pirati.ca>
Wed, 19 Aug 2020 18:21:40 +0000 (18:21 +0000)
bin/daemon.php
src/Core/Worker.php

index c2ce05c8e04b346aededf84fcb4fa90e8e51195e..596f4de56fe5bbcf687aae313b6206f4d46f3494 100755 (executable)
@@ -185,7 +185,12 @@ while (true) {
                $do_cron = true;
        }
 
-       Worker::spawnWorker($do_cron);
+       if ($do_cron || (!DI::process()->isMaxLoadReached() && Worker::entriesExists() && Worker::isReady())) {
+               Worker::spawnWorker($do_cron);
+       } else {
+               Logger::info('Cool down', ['pid' => $pid]);
+               sleep(10);
+       }
 
        if ($do_cron) {
                // We force a reconnect of the database connection.
index c9a8bcbddaa0ad908f1b86f0d0c45b3dc4e908ed..1cde61351c0f2fc0ce9a30fdc1f2b694b3226b70 100644 (file)
@@ -81,27 +81,8 @@ class Worker
                        self::killStaleWorkers();
                }
 
-               // Count active workers and compare them with a maximum value that depends on the load
-               if (self::tooMuchWorkers()) {
-                       Logger::info('Pre check: Active worker limit reached, quitting.');
-                       return;
-               }
-
-               // Do we have too few memory?
-               if (DI::process()->isMinMemoryReached()) {
-                       Logger::info('Pre check: Memory limit reached, quitting.');
-                       return;
-               }
-
-               // Possibly there are too much database connections
-               if (self::maxConnectionsReached()) {
-                       Logger::info('Pre check: maximum connections reached, quitting.');
-                       return;
-               }
-
-               // Possibly there are too much database processes that block the system
-               if (DI::process()->isMaxProcessesReached()) {
-                       Logger::info('Pre check: maximum processes reached, quitting.');
+               // Check if the system is ready
+               if (!self::isReady()) {
                        return;
                }
 
@@ -174,13 +155,49 @@ class Worker
                Logger::info("Couldn't select a workerqueue entry, quitting process", ['pid' => getmypid()]);
        }
 
+       /**
+        * Checks if the system is ready.
+        *
+        * Several system parameters like memory, connections and processes are checked.
+        *
+        * @return boolean
+        */
+       public static function isReady()
+       {
+               // Count active workers and compare them with a maximum value that depends on the load
+               if (self::tooMuchWorkers()) {
+                       Logger::info('Active worker limit reached, quitting.');
+                       return false;
+               }
+
+               // Do we have too few memory?
+               if (DI::process()->isMinMemoryReached()) {
+                       Logger::info('Memory limit reached, quitting.');
+                       return false;
+               }
+
+               // Possibly there are too much database connections
+               if (self::maxConnectionsReached()) {
+                       Logger::info('Maximum connections reached, quitting.');
+                       return false;
+               }
+
+               // Possibly there are too much database processes that block the system
+               if (DI::process()->isMaxProcessesReached()) {
+                       Logger::info('Maximum processes reached, quitting.');
+                       return false;
+               }
+               
+               return true;
+       }
+
        /**
         * Check if non executed tasks do exist in the worker queue
         *
         * @return boolean Returns "true" if tasks are existing
         * @throws \Exception
         */
-       private static function entriesExists()
+       public static function entriesExists()
        {
                $stamp = (float)microtime(true);
                $exists = DBA::exists('workerqueue', ["NOT `done` AND `pid` = 0 AND `next_try` < ?", DateTimeFormat::utcNow()]);