]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Worker.php
Merge pull request #8044 from annando/contact-adding
[friendica.git] / src / Core / Worker.php
index 709424c5ad318afefefa3bc8cdcdf7a29453936c..02c8f65cbb6760ca384f25eb276761f7a53f8e39 100644 (file)
@@ -4,8 +4,9 @@
  */
 namespace Friendica\Core;
 
-use Friendica\BaseObject;
+use Friendica\Core;
 use Friendica\Database\DBA;
+use Friendica\DI;
 use Friendica\Model\Process;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Network;
@@ -55,7 +56,7 @@ class Worker
                self::$up_start = microtime(true);
 
                // At first check the maximum load. We shouldn't continue with a high load
-               if ($a->isMaxLoadReached()) {
+               if (DI::process()->isMaxLoadReached()) {
                        Logger::log('Pre check: maximum load reached, quitting.', Logger::DEBUG);
                        return;
                }
@@ -77,7 +78,7 @@ class Worker
                }
 
                // Do we have too few memory?
-               if ($a->isMinMemoryReached()) {
+               if (DI::process()->isMinMemoryReached()) {
                        Logger::log('Pre check: Memory limit reached, quitting.', Logger::DEBUG);
                        return;
                }
@@ -89,7 +90,7 @@ class Worker
                }
 
                // Possibly there are too much database processes that block the system
-               if ($a->isMaxProcessesReached()) {
+               if (DI::process()->isMaxProcessesReached()) {
                        Logger::log('Pre check: maximum processes reached, quitting.', Logger::DEBUG);
                        return;
                }
@@ -139,7 +140,7 @@ class Worker
                                        }
 
                                        // Check free memory
-                                       if ($a->isMinMemoryReached()) {
+                                       if (DI::process()->isMinMemoryReached()) {
                                                Logger::log('Memory limit reached, quitting.', Logger::DEBUG);
                                                Lock::release('worker');
                                                return;
@@ -250,8 +251,6 @@ class Worker
         */
        public static function execute($queue)
        {
-               $a = \get_app();
-
                $mypid = getmypid();
 
                // Quit when in maintenance
@@ -261,7 +260,7 @@ class Worker
                }
 
                // Constantly check the number of parallel database processes
-               if ($a->isMaxProcessesReached()) {
+               if (DI::process()->isMaxProcessesReached()) {
                        Logger::log("Max processes reached for process ".$mypid, Logger::DEBUG);
                        return false;
                }
@@ -385,7 +384,7 @@ class Worker
 
                // We use the callstack here to analyze the performance of executed worker entries.
                // For this reason the variables have to be initialized.
-               $a->getProfiler()->reset();
+               DI::profiler()->reset();
 
                $a->queue = $queue;
 
@@ -442,7 +441,7 @@ class Worker
 
                Logger::info('Process done.', ['priority' => $queue["priority"], 'id' => $queue["id"], 'duration' => round($duration, 3)]);
 
-               $a->getProfiler()->saveLog($a->getLogger(), "ID " . $queue["id"] . ": " . $funcname);
+               DI::profiler()->saveLog(DI::logger(), "ID " . $queue["id"] . ": " . $funcname);
 
                $cooldown = Config::get("system", "worker_cooldown", 0);
 
@@ -670,7 +669,7 @@ class Worker
                                $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());
+                               $jobs = DBA::p("SELECT COUNT(*) AS `entries`, `priority` FROM `workerqueue` WHERE NOT `done` GROUP BY `priority`");
                                self::$db_duration += (microtime(true) - $stamp);
                                self::$db_duration_stat += (microtime(true) - $stamp);
                                while ($entry = DBA::fetch($jobs)) {
@@ -700,6 +699,8 @@ class Worker
                                DBA::close($jobs);
                        }
 
+                       $waiting_processes -= $deferred;
+
                        $listitem[0] = "0:" . max(0, $idle_workers);
 
                        $processlist .= ' ('.implode(', ', $listitem).')';
@@ -978,7 +979,7 @@ class Worker
                        return;
                }
 
-               $url = System::baseUrl()."/worker";
+               $url = DI::baseUrl() . '/worker';
                Network::fetchUrl($url, false, 1);
        }
 
@@ -1082,7 +1083,9 @@ class Worker
 
                $args = ['no_cron' => !$do_cron];
 
-               get_app()->proc_run($command, $args);
+               $a = get_app();
+               $process = new Core\Process(DI::logger(), DI::mode(), DI::config(), $a->getBasePath());
+               $process->run($command, $args);
 
                // after spawning we have to remove the flag.
                if (Config::get('system', 'worker_daemon_mode', false)) {
@@ -1099,7 +1102,7 @@ class Worker
         * or: Worker::add(PRIORITY_HIGH, "Notifier", Delivery::DELETION, $drop_id);
         * or: Worker::add(array('priority' => PRIORITY_HIGH, 'dont_fork' => true), "CreateShadowEntry", $post_id);
         *
-        * @return boolean "false" if proc_run couldn't be executed
+        * @return boolean "false" if worker queue entry already existed or there had been an error
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         * @note $cmd and string args are surrounded with ""
         *
@@ -1124,7 +1127,7 @@ class Worker
 
                $priority = PRIORITY_MEDIUM;
                // Don't fork from frontend tasks by default
-               $dont_fork = Config::get("system", "worker_dont_fork", false) || !\get_app()->isBackend();
+               $dont_fork = Config::get("system", "worker_dont_fork", false) || !DI::mode()->isBackend();
                $created = DateTimeFormat::utcNow();
                $force_priority = false;
 
@@ -1149,6 +1152,7 @@ class Worker
 
                $parameters = json_encode($args);
                $found = DBA::exists('workerqueue', ['parameter' => $parameters, 'done' => false]);
+               $added = false;
 
                // Quit if there was a database error - a precaution for the update process to 3.5.3
                if (DBA::errorNo() != 0) {
@@ -1156,19 +1160,22 @@ class Worker
                }
 
                if (!$found) {
-                       DBA::insert('workerqueue', ['parameter' => $parameters, 'created' => $created, 'priority' => $priority]);
+                       $added = DBA::insert('workerqueue', ['parameter' => $parameters, 'created' => $created, 'priority' => $priority]);
+                       if (!$added) {
+                               return false;
+                       }
                } elseif ($force_priority) {
                        DBA::update('workerqueue', ['priority' => $priority], ['parameter' => $parameters, 'done' => false, 'pid' => 0]);
                }
 
                // Should we quit and wait for the worker to be called as a cronjob?
                if ($dont_fork) {
-                       return true;
+                       return $added;
                }
 
                // If there is a lock then we don't have to check for too much worker
                if (!Lock::acquire('worker', 0)) {
-                       return true;
+                       return $added;
                }
 
                // If there are already enough workers running, don't fork another one
@@ -1176,19 +1183,19 @@ class Worker
                Lock::release('worker');
 
                if ($quit) {
-                       return true;
+                       return $added;
                }
 
                // We tell the daemon that a new job entry exists
                if (Config::get('system', 'worker_daemon_mode', false)) {
                        // We don't have to set the IPC flag - this is done in "tooMuchWorkers"
-                       return true;
+                       return $added;
                }
 
                // Now call the worker to execute the jobs that we just added to the queue
                self::spawnWorker();
 
-               return true;
+               return $added;
        }
 
        /**
@@ -1219,14 +1226,15 @@ class Worker
 
        /**
         * Defers the current worker entry
+        * @return boolean had the entry been deferred?
         */
        public static function defer()
        {
-               if (empty(BaseObject::getApp()->queue)) {
-                       return;
+               if (empty(DI::app()->queue)) {
+                       return false;
                }
 
-               $queue = BaseObject::getApp()->queue;
+               $queue = DI::app()->queue;
 
                $retrial = $queue['retrial'];
                $id = $queue['id'];
@@ -1237,8 +1245,8 @@ class Worker
                $new_retrial = self::getNextRetrial($queue, $max_level);
 
                if ($new_retrial > $max_level) {
-                       Logger::info('The task exceeded the maximum retry count', ['id' => $id, 'max_level' => $max_level, 'retrial' => $new_retrial]);
-                       return;
+                       Logger::info('The task exceeded the maximum retry count', ['id' => $id, 'created' => $queue['created'], 'old_prio' => $queue['priority'], 'old_retrial' => $queue['retrial'], 'max_level' => $max_level, 'retrial' => $new_retrial]);
+                       return false;
                }
 
                // Calculate the delay until the next trial
@@ -1253,13 +1261,15 @@ class Worker
                        $priority = PRIORITY_NEGLIGIBLE;
                }
 
-               Logger::info('Deferred task', ['id' => $id, 'retrial' => $new_retrial, 'next_execution' => $next, 'old_prio' => $queue['priority'], 'new_prio' => $priority]);
+               Logger::info('Deferred task', ['id' => $id, 'retrial' => $new_retrial, 'created' => $queue['created'], 'next_execution' => $next, 'old_prio' => $queue['priority'], 'new_prio' => $priority]);
 
                $stamp = (float)microtime(true);
                $fields = ['retrial' => $new_retrial, 'next_try' => $next, 'executed' => DBA::NULL_DATETIME, 'pid' => 0, 'priority' => $priority];
                DBA::update('workerqueue', $fields, ['id' => $id]);
                self::$db_duration += (microtime(true) - $stamp);
                self::$db_duration_write += (microtime(true) - $stamp);
+
+               return true;
        }
 
        /**