]> git.mxchange.org Git - friendica.git/blobdiff - src/Worker/UpdateGServers.php
Detection of local requests
[friendica.git] / src / Worker / UpdateGServers.php
index a245c34fca260b1b866d2a54897b5b6b1d3660fa..38ab6e5d3079f0e55cc3dc4d7cfe9671eaa7c85c 100644 (file)
@@ -1,40 +1,78 @@
 <?php
 /**
- * @file src/Worker/UpdateGServers.php
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
  */
+
 namespace Friendica\Worker;
 
 use Friendica\Core\Logger;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
-use Friendica\Model\GServer;
+use Friendica\DI;
+use Friendica\Util\Strings;
 
 class UpdateGServers
 {
        /**
-        * @brief Updates the first 250 servers
-        *
+        * Updates a defined number of servers
         */
        public static function execute()
        {
-               $gservers = DBA::p("SELECT `url`, `created`, `last_failure`, `last_contact` FROM `gserver` ORDER BY rand()");
-               if (!DBA::isResult($gservers)) {
+               $update_limit = DI::config()->get('system', 'gserver_update_limit');
+               if (empty($update_limit)) {
                        return;
                }
 
-               $updated = 0;
+               $updating = Worker::countWorkersByCommand('UpdateGServer');
+               $limit = $update_limit - $updating;
+               if ($limit <= 0) {
+                       Logger::info('The number of currently running jobs exceed the limit');
+                       return;
+               }
 
-               while ($gserver = DBA::fetch($gservers)) {
-                       if (!GServer::updateNeeded($gserver['created'], '', $gserver['last_failure'], $gserver['last_contact'])) {
-                               continue;
-                       }
-                       Logger::info('Update server status', ['server' => $gserver['url']]);
+               $total = DBA::count('gserver');
+               $condition = ["`next_contact` < UTC_TIMESTAMP() AND (`nurl` != ? OR `url` != ?)", '', ''];
+               $outdated = DBA::count('gserver', $condition);
+               Logger::info('Server status', ['total' => $total, 'outdated' => $outdated, 'updating' => $limit]);
 
-                       Worker::add(PRIORITY_LOW, 'UpdateGServer', $gserver['url']);
+               $gservers = DBA::select('gserver', ['url', 'nurl'], $condition, ['limit' => $limit]);
+               if (!DBA::isResult($gservers)) {
+                       return;
+               }
 
-                       if (++$updated > 250) {
-                               return;
+               $count = 0;
+               while ($gserver = DBA::fetch($gservers)) {
+                       // Sometimes the "nurl" and "url" doesn't seem to fit, see https://forum.friendi.ca/display/ec054ce7-155f-c94d-6159-f50372664245
+                       // There are duplicated "url" but not "nurl". So we check both addresses instead of just overwriting them,
+                       // since that would mean loosing data.
+                       if (!empty($gserver['url'])) {
+                               if (Worker::add(PRIORITY_LOW, 'UpdateGServer', $gserver['url'])) {
+                                       $count++;
+                               }
+                       }
+                       if (!empty($gserver['nurl']) && ($gserver['nurl'] != Strings::normaliseLink($gserver['url']))) {
+                               if (Worker::add(PRIORITY_LOW, 'UpdateGServer', $gserver['nurl'])) {
+                                       $count++;
+                               }
                        }
                }
+               DBA::close($gservers);
+               Logger::info('Updated servers', ['count' => $count]);
        }
 }