]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Worker.php
Merge pull request #5920 from MrPetovan/bug/remove-password-from-register
[friendica.git] / src / Core / Worker.php
index 1de4d3b953414319548ac13ffdbad3f1eb906deb..723cd809da44d3230721afd1eb4e06cf5a17ffb7 100644 (file)
@@ -8,6 +8,7 @@ use Friendica\Database\DBA;
 use Friendica\Model\Process;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Network;
+use Friendica\BaseObject;
 
 require_once 'include/dba.php';
 
@@ -62,7 +63,7 @@ class Worker
                }
 
                // Do we have too few memory?
-               if ($a->min_memory_reached()) {
+               if ($a->isMinMemoryReached()) {
                        logger('Pre check: Memory limit reached, quitting.', LOGGER_DEBUG);
                        return;
                }
@@ -117,12 +118,14 @@ class Worker
                                // Count active workers and compare them with a maximum value that depends on the load
                                if (self::tooMuchWorkers()) {
                                        logger('Active worker limit reached, quitting.', LOGGER_DEBUG);
+                                       Lock::release('worker');
                                        return;
                                }
 
                                // Check free memory
-                               if ($a->min_memory_reached()) {
+                               if ($a->isMinMemoryReached()) {
                                        logger('Memory limit reached, quitting.', LOGGER_DEBUG);
+                                       Lock::release('worker');
                                        return;
                                }
                                Lock::release('worker');
@@ -150,7 +153,8 @@ class Worker
         */
        private static function totalEntries()
        {
-               return DBA::count('workerqueue', ["`executed` <= ? AND NOT `done`", NULL_DATE]);
+               return DBA::count('workerqueue', ["`executed` <= ? AND NOT `done` AND `next_try` < ?",
+                       NULL_DATE, DateTimeFormat::utcNow()]);
        }
 
        /**
@@ -160,7 +164,7 @@ class Worker
         */
        private static function highestPriority()
        {
-               $condition = ["`executed` <= ? AND NOT `done`", NULL_DATE];
+               $condition = ["`executed` <= ? AND NOT `done` AND `next_try` < ?", NULL_DATE, DateTimeFormat::utcNow()];
                $workerqueue = DBA::selectFirst('workerqueue', ['priority'], $condition, ['order' => ['priority']]);
                if (DBA::isResult($workerqueue)) {
                        return $workerqueue["priority"];
@@ -178,7 +182,8 @@ class Worker
         */
        private static function processWithPriorityActive($priority)
        {
-               $condition = ["`priority` <= ? AND `executed` > ? AND NOT `done`", $priority, NULL_DATE];
+               $condition = ["`priority` <= ? AND `executed` > ? AND NOT `done` AND `next_try` < ?",
+                       $priority, NULL_DATE, DateTimeFormat::utcNow()];
                return DBA::exists('workerqueue', $condition);
        }
 
@@ -238,7 +243,7 @@ class Worker
                        self::execFunction($queue, $include, $argv, true);
 
                        $stamp = (float)microtime(true);
-                       if (DBA::update('workerqueue', ['done' => true], ['id' => $queue["id"]])) {
+                       if (DBA::update('workerqueue', ['done' => true], ['id' => $queue['id']])) {
                                Config::set('system', 'last_worker_execution', DateTimeFormat::utcNow());
                        }
                        self::$db_duration = (microtime(true) - $stamp);
@@ -616,7 +621,7 @@ class Worker
                $active = self::activeWorkers();
 
                // Decrease the number of workers at higher load
-               $load = current_load();
+               $load = System::currentLoad();
                if ($load) {
                        $maxsysload = intval(Config::get("system", "maxloadavg", 50));
 
@@ -803,7 +808,8 @@ class Worker
                        $result = DBA::select(
                                'workerqueue',
                                ['id'],
-                               ["`executed` <= ? AND `priority` < ? AND NOT `done`", NULL_DATE, $highest_priority],
+                               ["`executed` <= ? AND `priority` < ? AND NOT `done` AND `next_try` < ?",
+                               NULL_DATE, $highest_priority, DateTimeFormat::utcNow()],
                                ['limit' => $limit, 'order' => ['priority', 'created']]
                        );
 
@@ -819,7 +825,8 @@ class Worker
                                $result = DBA::select(
                                        'workerqueue',
                                        ['id'],
-                                       ["`executed` <= ? AND `priority` > ? AND NOT `done`", NULL_DATE, $highest_priority],
+                                       ["`executed` <= ? AND `priority` > ? AND NOT `done` AND `next_try` < ?",
+                                       NULL_DATE, $highest_priority, DateTimeFormat::utcNow()],
                                        ['limit' => $limit, 'order' => ['priority', 'created']]
                                );
 
@@ -838,7 +845,8 @@ class Worker
                        $result = DBA::select(
                                'workerqueue',
                                ['id'],
-                               ["`executed` <= ? AND NOT `done`", NULL_DATE],
+                               ["`executed` <= ? AND NOT `done` AND `next_try` < ?",
+                               NULL_DATE, DateTimeFormat::utcNow()],
                                ['limit' => $limit, 'order' => ['priority', 'created']]
                        );
 
@@ -1011,7 +1019,7 @@ class Worker
        {
                $command = 'bin/worker.php';
 
-               $args = [ 'cron' => $do_cron ];
+               $args = ['no_cron' => !$do_cron];
 
                get_app()->proc_run($command, $args);
 
@@ -1114,6 +1122,35 @@ class Worker
                return true;
        }
 
+       /**
+        * Defers the current worker entry
+        */
+       public static function defer()
+       {
+               if (empty(BaseObject::getApp()->queue)) {
+                       return;
+               }
+
+               $queue = BaseObject::getApp()->queue;
+
+               $retrial = $queue['retrial'];
+               $id = $queue['id'];
+
+               if ($retrial > 14) {
+                       logger('Id ' . $id . ' had been tried 14 times, it will be deleted now.', LOGGER_DEBUG);
+                       DBA::delete('workerqueue', ['id' => $id]);
+               }
+
+               // Calculate the delay until the next trial
+               $delay = (($retrial + 3) ** 4) + (rand(1, 30) * ($retrial + 1));
+               $next = DateTimeFormat::utc('now + ' . $delay . ' seconds');
+
+               logger('Defer execution ' . $retrial . ' of id ' . $id . ' to ' . $next, LOGGER_DEBUG);
+
+               $fields = ['retrial' => $retrial + 1, 'next_try' => $next, 'executed' => NULL_DATE, 'pid' => 0];
+               DBA::update('workerqueue', $fields, ['id' => $id]);
+       }
+
        /**
         * Log active processes into the "process" table
         *