]> git.mxchange.org Git - friendica.git/commitdiff
Replace direct database calls and anonymous arrays by repository calls and entity...
authorHypolite Petovan <hypolite@mrpetovan.com>
Sat, 31 Dec 2022 19:36:47 +0000 (14:36 -0500)
committerHypolite Petovan <hypolite@mrpetovan.com>
Sun, 1 Jan 2023 23:50:21 +0000 (18:50 -0500)
src/Core/Worker/Cron.php
src/Database/DBA.php
src/Worker/BulkDelivery.php
src/Worker/Notifier.php

index 21f67c030bab8a96333707e216a9efa98e2adacd..de6a808acaad1db1682ba3653aaee0806f611fb1 100644 (file)
@@ -204,32 +204,31 @@ class Cron
         */
        private static function deliverPosts()
        {
-               $deliveries = DBA::p("SELECT `gsid`, MAX(`failed`) AS `failed` FROM `delivery-queue` GROUP BY `gsid` ORDER BY RAND()");
-               while ($delivery = DBA::fetch($deliveries)) {
-                       if ($delivery['failed'] > 0) {
-                               Logger::info('Removing failed deliveries', ['gsid' => $delivery['gsid'], 'failed' => $delivery['failed']]);
-                               Delivery::removeFailedQueue($delivery['gsid']);
+               foreach(DI::deliveryQueueItemRepo()->selectAggregateByServerId() as $delivery) {
+                       if ($delivery->failed > 0) {
+                               Logger::info('Removing failed deliveries', ['gsid' => $delivery->targetServerId, 'failed' => $delivery->failed]);
+                               DI::deliveryQueueItemRepo()->removeFailedByServerId($delivery->targetServerId, DI::config()->get('system', 'worker_defer_limit'));
                        }
 
-                       if (($delivery['failed'] < 3) || GServer::isReachableById($delivery['gsid'])) {
+                       if (($delivery->failed < 3) || GServer::isReachableById($delivery->targetServerId)) {
                                $priority = Worker::PRIORITY_HIGH;
-                       } elseif ($delivery['failed'] < 6) {
+                       } elseif ($delivery->failed < 6) {
                                $priority = Worker::PRIORITY_MEDIUM;
-                       } elseif ($delivery['failed'] < 8) {
+                       } elseif ($delivery->failed < 8) {
                                $priority = Worker::PRIORITY_LOW;
                        } else {
                                $priority = Worker::PRIORITY_NEGLIGIBLE;
                        }
 
-                       if (Worker::add(['priority' => $priority, 'force_priority' => true], 'BulkDelivery', $delivery['gsid'])) {
-                               Logger::info('Priority for BulkDelivery worker adjusted', ['gsid' => $delivery['gsid'], 'failed' => $delivery['failed'], 'priority' => $priority]);
+                       if (Worker::add(['priority' => $priority, 'force_priority' => true], 'BulkDelivery', $delivery->targetServerId)) {
+                               Logger::info('Priority for BulkDelivery worker adjusted', ['gsid' => $delivery->targetServerId, 'failed' => $delivery->failed, 'priority' => $priority]);
                        }
                }
 
                // Optimizing this table only last seconds
                if (DI::config()->get('system', 'optimize_tables')) {
                        Logger::info('Optimize start');
-                       DBA::e("OPTIMIZE TABLE `delivery-queue`");
+                       DI::deliveryQueueItemRepo()->optimizeStorage();
                        Logger::info('Optimize end');
                }
        }
index 2e4fb376712e381bc45b338b61689d3e428547c3..ffeecfaa711615e607e6fdb83462f66ff1215656 100644 (file)
@@ -517,7 +517,7 @@ class DBA
         * Build the table query substring from one or more tables, with or without a schema.
         *
         * Expected formats:
-        * - table
+        * - [table]
         * - [table1, table2, ...]
         * - [schema1 => table1, schema2 => table2, table3, ...]
         *
index 5e335f720572b541aa4906fcde34dfe790a1a1ab..4ac77b21376fc1c09c809a767ed37645fae3b094 100644 (file)
@@ -23,6 +23,7 @@ namespace Friendica\Worker;
 
 use Friendica\Core\Logger;
 use Friendica\Core\Worker;
+use Friendica\DI;
 use Friendica\Model\GServer;
 use Friendica\Protocol\Delivery as ProtocolDelivery;
 
@@ -33,19 +34,19 @@ class BulkDelivery
                $server_failure   = false;
                $delivery_failure = false;
 
-               $posts = ProtocolDelivery::selectQueueForServer($gsid);
-               foreach ($posts as $post) {
-                       if (!$server_failure && ProtocolDelivery::deliver($post['command'], $post['uri-id'], $post['cid'], $post['uid'])) {
-                               ProtocolDelivery::removeQueue($post['uri-id'], $post['gsid']);
-                               Logger::debug('Delivery successful', $post);
+               $deliveryQueueItems = DI::deliveryQueueItemRepo()->selectByServerId($gsid, DI::config()->get('system', 'worker_defer_limit'));
+               foreach ($deliveryQueueItems as $deliveryQueueItem) {
+                       if (!$server_failure && ProtocolDelivery::deliver($deliveryQueueItem->command, $deliveryQueueItem->postUriId, $deliveryQueueItem->targetContactId, $deliveryQueueItem->senderUserId)) {
+                               DI::deliveryQueueItemRepo()->remove($deliveryQueueItem);
+                               Logger::debug('Delivery successful', $deliveryQueueItem->toArray());
                        } else {
-                               ProtocolDelivery::incrementFailedQueue($post['uri-id'], $post['gsid']);
+                               DI::deliveryQueueItemRepo()->incrementFailed($deliveryQueueItem);
                                $delivery_failure = true;
 
                                if (!$server_failure) {
                                        $server_failure = !GServer::isReachableById($gsid);
                                }
-                               Logger::debug('Delivery failed', ['server_failure' => $server_failure, 'post' => $post]);
+                               Logger::debug('Delivery failed', ['server_failure' => $server_failure, 'post' => $deliveryQueueItem]);
                        }
                }
 
@@ -54,7 +55,7 @@ class BulkDelivery
                }
 
                if ($delivery_failure) {
-                       ProtocolDelivery::removeFailedQueue($gsid);
+                       DI::deliveryQueueItemRepo()->removeFailedByServerId($gsid, DI::config()->get('system', 'worker_defer_limit'));
                }
        }
 }
index 0b7460c53dd3206c647cebb05c80866ffab7c158..2d21a0937c748fab73102d3cf646e9010aea72a9 100644 (file)
@@ -592,7 +592,8 @@ class Notifier
 
                        if (!empty($contact['gsid']) && DI::config()->get('system', 'bulk_delivery')) {
                                $delivery_queue_count++;
-                               Delivery::addQueue($cmd, $post_uriid, $target_item['created'], $contact['id'], $contact['gsid'], $sender_uid);
+                               $deliveryQueueItem = DI::deliveryQueueItemFactory()->createFromDelivery($cmd, $post_uriid, new \DateTimeImmutable($target_item['created']), $contact['id'], $contact['gsid'], $sender_uid);
+                               DI::deliveryQueueItemRepo()->save($deliveryQueueItem);
                                Worker::add(['priority' => Worker::PRIORITY_HIGH, 'dont_fork' => true], 'BulkDelivery', $contact['gsid']);
                        } else {
                                if (Worker::add($deliver_options, 'Delivery', $cmd, $post_uriid, (int)$contact['id'], $sender_uid)) {