]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/System.php
Pause the worker execution when the load is too high
[friendica.git] / src / Core / System.php
index dbff4bf3f5cb5f5d21c2bd537a230b315c30bbfe..eb27fa351775b3e90ec6be9d230d42701f2acee0 100644 (file)
@@ -435,6 +435,33 @@ class System
                return max($load_arr[0], $load_arr[1]);
        }
 
+       /**
+        * Fetch the load and number of processes
+        *
+        * @return array
+        */
+       public static function getLoadAvg(): array
+       {
+               $content = file_get_contents('/proc/loadavg');
+               if (empty($content)) {
+                       $content = shell_exec('cat /proc/loadavg');
+               }
+               if (empty($content)) {
+                       return [];
+               }
+
+               if (!preg_match("#([.\d]+)\s([.\d]+)\s([.\d]+)\s(\d+)/(\d+)#", $content, $matches)) {
+                       return [];
+               }
+               return [
+                       'average1'  => (float)$matches[1],
+                       'average5'  => (float)$matches[2],
+                       'average15' => (float)$matches[3],
+                       'runnable'  => (float)$matches[4],
+                       'scheduled' => (float)$matches[5]
+               ];
+       }
+
        /**
         * Redirects to an external URL (fully qualified URL)
         * If you want to route relative to the current Friendica base, use App->internalRedirect()