]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Worker.php
Use a process identifier for logging that contains the pid (#5359)
[friendica.git] / src / Core / Worker.php
index bc45be2d5b75da128966c7d98d56d0cb3dda2315..296d40b5046e182ba86cb219f8384f272f170314 100644 (file)
@@ -4,15 +4,11 @@
  */
 namespace Friendica\Core;
 
-use Friendica\Core\Addon;
-use Friendica\Core\Config;
-use Friendica\Core\System;
+use dba;
 use Friendica\Database\DBM;
 use Friendica\Model\Process;
 use Friendica\Util\DateTimeFormat;
-use Friendica\Util\Lock;
 use Friendica\Util\Network;
-use dba;
 
 require_once 'include/dba.php';
 
@@ -108,16 +104,16 @@ class Worker
                                }
 
                                // If possible we will fetch new jobs for this worker
-                               if (!$refetched && Lock::set('worker_process', 0)) {
+                               if (!$refetched && Lock::acquire('worker_process', 0)) {
                                        $stamp = (float)microtime(true);
                                        $refetched = self::findWorkerProcesses($passing_slow);
                                        self::$db_duration += (microtime(true) - $stamp);
-                                       Lock::remove('worker_process');
+                                       Lock::release('worker_process');
                                }
                        }
 
                        // To avoid the quitting of multiple workers only one worker at a time will execute the check
-                       if (Lock::set('worker', 0)) {
+                       if (Lock::acquire('worker', 0)) {
                                $stamp = (float)microtime(true);
                                // Count active workers and compare them with a maximum value that depends on the load
                                if (self::tooMuchWorkers()) {
@@ -130,7 +126,7 @@ class Worker
                                        logger('Memory limit reached, quitting.', LOGGER_DEBUG);
                                        return;
                                }
-                               Lock::remove('worker');
+                               Lock::release('worker');
                                self::$db_duration += (microtime(true) - $stamp);
                        }
 
@@ -145,7 +141,7 @@ class Worker
                if (Config::get('system', 'worker_daemon_mode', false)) {
                        self::IPCSetJobState(false);
                }
-               logger("Couldn't select a workerqueue entry, quitting.", LOGGER_DEBUG);
+               logger("Couldn't select a workerqueue entry, quitting process " . getmypid() . ".", LOGGER_DEBUG);
        }
 
        /**
@@ -313,7 +309,7 @@ class Worker
 
                $argc = count($argv);
 
-               $new_process_id = uniqid("wrk", true);
+               $new_process_id = System::processID("wrk");
 
                logger("Process ".$mypid." - Prio ".$queue["priority"]." - ID ".$queue["id"].": ".$funcname." ".$queue["parameter"]." - Process PID: ".$new_process_id);
 
@@ -625,34 +621,14 @@ class Worker
                if ($load) {
                        $maxsysload = intval(Config::get("system", "maxloadavg", 50));
 
-                       if (Config::get('system', 'worker_linear_load', false)) {
-                               /* The linear load calculation works fine if there is a low
-                                * number of maximum queues and a high load base level.
-                                * This can be present at shared hosters.
-                               */
-                               $tinyload = 1;
-
-                               if ($load > $maxsysload) {
-                                       $queues = 0;
-                               } elseif ($load > $tinyload) {
-                                       //Provide $queues number between 1 (below max load) and $maxqueues - 1 (above tiny load).
-                                       $range = $maxsysload - $tinyload;
-                                       $slope = 1.00 - (($load - $tinyload) / $range);
-                                       $target = $slope * ($maxqueues - 1);
-                                       $queues = intval(ceil($target));
-                               }
-                       } else {
-                               /* The exponentional load calculation respects the load behaviour
-                                * of Linux systems with regular hardware that normally idles
-                                * with load values near 0.
-                               */
-                               $maxworkers = $queues;
-
-                               // Some magical mathemathics to reduce the workers
-                               $exponent = 3;
-                               $slope = $maxworkers / pow($maxsysload, $exponent);
-                               $queues = ceil($slope * pow(max(0, $maxsysload - $load), $exponent));
-                       }
+                       /* Default exponent 3 causes queues to rapidly decrease as load increases.
+                        * If you have 20 max queues at idle, then you get only 5 queues at 37.1% of $maxsysload.
+                        * For some environments, this rapid decrease is not needed.
+                        * With exponent 1, you could have 20 max queues at idle and 13 at 37% of $maxsysload.
+                        */
+                       $exponent = intval(Config::get('system', 'worker_load_exponent', 3));
+                       $slope = pow(max(0, $maxsysload - $load) / $maxsysload, $exponent);
+                       $queues = intval(ceil($slope * $maxqueues));
 
                        $processlist = '';
 
@@ -903,7 +879,7 @@ class Worker
                dba::close($r);
 
                $stamp = (float)microtime(true);
-               if (!Lock::set('worker_process')) {
+               if (!Lock::acquire('worker_process')) {
                        return false;
                }
                self::$lock_duration = (microtime(true) - $stamp);
@@ -912,7 +888,7 @@ class Worker
                $found = self::findWorkerProcesses($passing_slow);
                self::$db_duration += (microtime(true) - $stamp);
 
-               Lock::remove('worker_process');
+               Lock::release('worker_process');
 
                if ($found) {
                        $r = dba::select('workerqueue', [], ['pid' => getmypid(), 'done' => false]);
@@ -1117,13 +1093,13 @@ class Worker
                }
 
                // If there is a lock then we don't have to check for too much worker
-               if (!Lock::set('worker', 0)) {
+               if (!Lock::acquire('worker', 0)) {
                        return true;
                }
 
                // If there are already enough workers running, don't fork another one
                $quit = self::tooMuchWorkers();
-               Lock::remove('worker');
+               Lock::release('worker');
 
                if ($quit) {
                        return true;