]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Worker.php
Avoid memory issue in exception
[friendica.git] / src / Core / Worker.php
index 5deb33ab644f403f307d532a1d85048db96d82c5..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")) {
@@ -650,8 +655,7 @@ class Worker
                                $argv[0] = basename($argv[0]);
 
                                // How long is the process already running?
-                               // For some weird reasons we cannot use "time()" here. It doesn't seem to be in UTC.
-                               $duration = (strtotime(DateTimeFormat::utcNow()) - strtotime($entry["executed"])) / 60;
+                               $duration = (time() - strtotime($entry["executed"])) / 60;
                                if ($duration > $max_duration) {
                                        Logger::log("Worker process ".$entry["pid"]." (".substr(json_encode($argv), 0, 50).") took more than ".$max_duration." minutes. It will be killed now.");
                                        posix_kill($entry["pid"], SIGTERM);
@@ -928,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);
 
@@ -947,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);
 
@@ -961,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);
@@ -969,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);