]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Worker.php
Avoid memory issue in exception
[friendica.git] / src / Core / Worker.php
index 206a300ee46a9bc94485ae46560f46babc041a49..75d4e48740ef60867b179a3da2ac12d97b5c5525 100644 (file)
@@ -40,6 +40,9 @@ class Worker
        {
                $a = \get_app();
 
+               // Ensure that all "strtotime" operations do run timezone independent
+               date_default_timezone_set('UTC');
+
                self::$up_start = microtime(true);
 
                // At first check the maximum load. We shouldn't continue with a high load
@@ -367,7 +370,9 @@ class Worker
 
                $argc = count($argv);
 
-               $new_process_id = System::processID("wrk");
+               // Currently deactivated, since the new logger doesn't support this
+               //$new_process_id = System::processID("wrk");
+               $new_process_id = '';
 
                Logger::log("Process ".$mypid." - Prio ".$queue["priority"]." - ID ".$queue["id"].": ".$funcname." ".$queue["parameter"]." - Process PID: ".$new_process_id);
 
@@ -445,7 +450,7 @@ class Worker
                        Logger::log("Prio ".$queue["priority"].": ".$queue["parameter"]." - longer than 2 minutes (".round($duration/60, 3).")", Logger::DEBUG);
                }
 
-               Logger::log("Process ".$mypid." - Prio ".$queue["priority"]." - ID ".$queue["id"].": ".$funcname." - done in ".$duration." seconds. Process PID: ".$new_process_id);
+               Logger::log("Process ".$mypid." - Prio ".$queue["priority"]." - ID ".$queue["id"].": ".$funcname." - done in ".number_format($duration, 4)." seconds. Process PID: ".$new_process_id);
 
                // Write down the performance values into the log
                if (Config::get("system", "profiler")) {
@@ -741,7 +746,15 @@ class Worker
 
                        $idle_workers = $active;
 
+                       if (empty($deferred) && empty($entries)) {
+                               $deferred = self::deferredEntries();
+                               $entries = max(self::totalEntries() - $deferred, 0);
+                       }
+
+                       $waiting_processes = max(0, $entries - $deferred);
+
                        if (Config::get('system', 'worker_debug')) {
+                               $waiting_processes = 0;
                                // Now adding all processes with workerqueue entries
                                $stamp = (float)microtime(true);
                                $jobs = DBA::p("SELECT COUNT(*) AS `entries`, `priority` FROM `workerqueue` WHERE NOT `done` AND `next_try` < ? GROUP BY `priority`", DateTimeFormat::utcNow());
@@ -754,6 +767,7 @@ class Worker
                                        self::$db_duration_stat += (microtime(true) - $stamp);
                                        if ($process = DBA::fetch($processes)) {
                                                $idle_workers -= $process["running"];
+                                               $waiting_processes += $entry["entries"];
                                                $listitem[$entry["priority"]] = $entry["priority"].":".$process["running"]."/".$entry["entries"];
                                        }
                                        DBA::close($processes);
@@ -775,11 +789,6 @@ class Worker
 
                        $processlist .= ' ('.implode(', ', $listitem).')';
 
-                       if (empty($deferred) && empty($entries)) {
-                               $deferred = self::deferredEntries();
-                               $entries = max(self::totalEntries() - $deferred, 0);
-                       }
-
                        if (Config::get("system", "worker_fastlane", false) && ($queues > 0) && self::entriesExists() && ($active >= $queues)) {
                                $top_priority = self::highestPriority();
                                $high_running = self::processWithPriorityActive($top_priority);
@@ -790,7 +799,7 @@ class Worker
                                }
                        }
 
-                       Logger::log("Load: " . $load ."/" . $maxsysload . " - processes: " . $deferred . "/" . $active . "/" . ($entries - $deferred) . $processlist . " - maximum: " . $queues . "/" . $maxqueues, Logger::DEBUG);
+                       Logger::log("Load: " . $load ."/" . $maxsysload . " - processes: " . $deferred . "/" . $active . "/" . $waiting_processes . $processlist . " - maximum: " . $queues . "/" . $maxqueues, Logger::DEBUG);
 
                        // Are there fewer workers running as possible? Then fork a new one.
                        if (!Config::get("system", "worker_dont_fork", false) && ($queues > ($active + 1)) && ($entries > 1)) {
@@ -923,7 +932,7 @@ class Worker
                                ['id'],
                                ["`pid` = 0 AND `priority` < ? AND NOT `done` AND `next_try` < ?",
                                $highest_priority, DateTimeFormat::utcNow()],
-                               ['limit' => $limit, 'order' => ['priority', 'created']]
+                               ['limit' => 1, 'order' => ['priority', 'created']]
                        );
                        self::$db_duration += (microtime(true) - $stamp);
 
@@ -942,7 +951,7 @@ class Worker
                                        ['id'],
                                        ["`pid` = 0 AND `priority` > ? AND NOT `done` AND `next_try` < ?",
                                        $highest_priority, DateTimeFormat::utcNow()],
-                                       ['limit' => $limit, 'order' => ['priority', 'created']]
+                                       ['limit' => 1, 'order' => ['priority', 'created']]
                                );
                                self::$db_duration += (microtime(true) - $stamp);
 
@@ -956,6 +965,26 @@ class Worker
                        }
                }
 
+               // At first try to fetch a bunch of high or medium tasks
+               if (!$found && ($limit > 1)) {
+                       $stamp = (float)microtime(true);
+                       $result = DBA::select(
+                               'workerqueue',
+                               ['id'],
+                               ["`pid` = 0 AND NOT `done` AND `priority` <= ? AND `next_try` < ? AND `retrial` = 0",
+                               PRIORITY_MEDIUM, DateTimeFormat::utcNow()],
+                               ['limit' => $limit, 'order' => ['created']]
+                       );
+                       self::$db_duration += (microtime(true) - $stamp);
+
+                       while ($id = DBA::fetch($result)) {
+                               $ids[] = $id["id"];
+                       }
+                       DBA::close($result);
+
+                       $found = (count($ids) > 0);
+               }
+
                // If there is no result (or we shouldn't pass lower processes) we check without priority limit
                if (!$found) {
                        $stamp = (float)microtime(true);
@@ -964,7 +993,7 @@ class Worker
                                ['id'],
                                ["`pid` = 0 AND NOT `done` AND `next_try` < ?",
                                DateTimeFormat::utcNow()],
-                               ['limit' => $limit, 'order' => ['priority', 'created']]
+                               ['limit' => 1, 'order' => ['priority', 'created']]
                        );
                        self::$db_duration += (microtime(true) - $stamp);