Create entity-related classes for delivery-queue table

This commit is contained in:
Hypolite Petovan 2022-12-31 14:27:42 -05:00
parent 4faf08c064
commit 49654c0d99
7 changed files with 372 additions and 0 deletions

View File

@ -602,6 +602,20 @@ abstract class DI
return self::$dice->create(Navigation\Notifications\Factory\FormattedNavNotification::class);
}
//
// "Federation" namespace instances
//
public static function deliveryQueueItemFactory(): Federation\Factory\DeliveryQueueItem
{
return self::$dice->create(Federation\Factory\DeliveryQueueItem::class);
}
public static function deliveryQueueItemRepo(): Federation\Repository\DeliveryQueueItem
{
return self::$dice->create(Federation\Repository\DeliveryQueueItem::class);
}
//
// "Protocol" namespace instances
//

View File

@ -0,0 +1,44 @@
<?php
/**
* @copyright Copyright (C) 2010-2023, 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\Federation\Collection;
use Friendica\Federation\Entity;
final class DeliveryQueueAggregates extends \Friendica\BaseCollection
{
/**
* @param Entity\DeliveryQueueAggregate[] $entities
* @param int|null $totalCount
*/
public function __construct(array $entities = [], int $totalCount = null)
{
parent::__construct($entities, $totalCount);
}
/**
* @return Entity\DeliveryQueueAggregate
*/
public function current(): Entity\DeliveryQueueAggregate
{
return parent::current();
}
}

View File

@ -0,0 +1,44 @@
<?php
/**
* @copyright Copyright (C) 2010-2023, 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\Federation\Collection;
use Friendica\Federation\Entity;
final class DeliveryQueueItems extends \Friendica\BaseCollection
{
/**
* @param Entity\DeliveryQueueItem[] $entities
* @param int|null $totalCount
*/
public function __construct(array $entities = [], int $totalCount = null)
{
parent::__construct($entities, $totalCount);
}
/**
* @return Entity\DeliveryQueueItem
*/
public function current(): Entity\DeliveryQueueItem
{
return parent::current();
}
}

View File

@ -0,0 +1,40 @@
<?php
/**
* @copyright Copyright (C) 2010-2023, 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\Federation\Entity;
/**
* @property-read int $targetServerId
* @property-read int $failed Maximum number of delivery failures among the delivery queue items targeting the server
*/
final class DeliveryQueueAggregate extends \Friendica\BaseEntity
{
/** @var int */
protected $targetServerId;
/** @var int */
protected $failed;
public function __construct(int $targetServerId, int $failed)
{
$this->targetServerId = $targetServerId;
$this->failed = $failed;
}
}

View File

@ -0,0 +1,62 @@
<?php
/**
* @copyright Copyright (C) 2010-2023, 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\Federation\Entity;
use DateTimeImmutable;
/**
* @property-read int $targetServerId
* @property-read int $postUriId
* @property-read DateTimeImmutable $created
* @property-read string $command One of the Protocol\Delivery command constant values
* @property-read int $targetContactId
* @property-read int $senderUserId
* @property-read int $failed Number of delivery failures for this post and target server
*/
final class DeliveryQueueItem extends \Friendica\BaseEntity
{
/** @var int */
protected $targetServerId;
/** @var int */
protected $postUriId;
/** @var DateTimeImmutable */
protected $created;
/** @var string */
protected $command;
/** @var int */
protected $targetContactId;
/** @var int */
protected $senderUserId;
/** @var int */
protected $failed;
public function __construct(int $targetServerId, int $postUriId, DateTimeImmutable $created, string $command, int $targetContactId, int $senderUserId, int $failed = 0)
{
$this->targetServerId = $targetServerId;
$this->postUriId = $postUriId;
$this->created = $created;
$this->command = $command;
$this->targetContactId = $targetContactId;
$this->senderUserId = $senderUserId;
$this->failed = $failed;
}
}

View File

@ -0,0 +1,55 @@
<?php
/**
* @copyright Copyright (C) 2010-2023, 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\Federation\Factory;
use Friendica\Federation\Entity;
final class DeliveryQueueItem extends \Friendica\BaseFactory implements \Friendica\Capabilities\ICanCreateFromTableRow
{
/**
* @inheritDoc
*/
public function createFromTableRow(array $row): Entity\DeliveryQueueItem
{
return new Entity\DeliveryQueueItem(
$row['gsid'],
$row['uri-id'],
new \DateTimeImmutable($row['created']),
$row['command'],
$row['cid'],
$row['uid'],
$row['failed']
);
}
public function createFromDelivery(string $cmd, int $uri_id, \DateTimeImmutable $created, int $cid, int $gsid, int $uid): Entity\DeliveryQueueItem
{
return new Entity\DeliveryQueueItem(
$gsid,
$uri_id,
$created,
$cmd,
$cid,
$uid
);
}
}

View File

@ -0,0 +1,113 @@
<?php
/**
* @copyright Copyright (C) 2010-2023, 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\Federation\Repository;
use Friendica\Database\Database;
use Friendica\Database\DBA;
use Friendica\Federation\Collection;
use Friendica\Federation\Entity;
use Friendica\Federation\Factory;
use Friendica\Util\DateTimeFormat;
use Psr\Log\LoggerInterface;
final class DeliveryQueueItem extends \Friendica\BaseRepository
{
protected static $table_name = 'delivery-queue';
public function __construct(Database $database, LoggerInterface $logger, Factory\DeliveryQueueItem $factory)
{
parent::__construct($database, $logger, $factory);
}
public function selectByServerId(int $gsid, int $maxFailedCount): Collection\DeliveryQueueItems
{
$Entities = new Collection\DeliveryQueueItems();
$deliveryQueueItems = $this->db->select(
self::$table_name,
[],
["`gsid` = ? AND `failed` < ?", $gsid, $maxFailedCount],
['order' => ['created']]
);
while ($deliveryQueueItem = $this->db->fetch($deliveryQueueItems)) {
$Entities[] = $this->factory->createFromTableRow($deliveryQueueItem);
}
$this->db->close($deliveryQueueItems);
return $Entities;
}
public function selectAggregateByServerId(): Collection\DeliveryQueueAggregates
{
$Entities = new Collection\DeliveryQueueAggregates();
$deliveryQueueAggregates = $this->db->p("SELECT `gsid`, MAX(`failed`) AS `failed` FROM " . DBA::buildTableString([self::$table_name]) . " GROUP BY `gsid` ORDER BY RAND()");
while ($deliveryQueueAggregate = $this->db->fetch($deliveryQueueAggregates)) {
$Entities[] = new Entity\DeliveryQueueAggregate($deliveryQueueAggregate['gsid'], $deliveryQueueAggregate['failed']);
}
$this->db->close($deliveryQueueAggregates);
return $Entities;
}
public function save(Entity\DeliveryQueueItem $deliveryQueueItem)
{
$fields = [
'gsid' => $deliveryQueueItem->targetServerId,
'uri-id' => $deliveryQueueItem->postUriId,
'created' => $deliveryQueueItem->created->format(DateTimeFormat::MYSQL),
'command' => $deliveryQueueItem->command,
'cid' => $deliveryQueueItem->targetContactId,
'uid' => $deliveryQueueItem->senderUserId,
'failed' => $deliveryQueueItem->failed,
];
$this->db->insert(self::$table_name, $fields, Database::INSERT_UPDATE);
}
public function remove(Entity\DeliveryQueueItem $deliveryQueueItem): bool
{
return $this->db->delete(self::$table_name, ['uri-id' => $deliveryQueueItem->postUriId, 'gsid' => $deliveryQueueItem->targetServerId]);
}
public function removeFailedByServerId(int $gsid, int $failedThreshold): bool
{
return $this->db->delete(self::$table_name, ["`gsid` = ? AND `failed` >= ?", $gsid, $failedThreshold]);
}
public function incrementFailed(Entity\DeliveryQueueItem $deliveryQueueItem): bool
{
return $this->db->e("
UPDATE " . DBA::buildTableString([self::$table_name]) . "
SET `failed` = `failed` + 1
WHERE `uri-id` = ? AND `gsid` = ?",
$deliveryQueueItem->postUriId, $deliveryQueueItem->targetServerId
);
}
public function optimizeStorage(): bool
{
return $this->db->e("OPTIMIZE TABLE " . DBA::buildTableString([self::$table_name]));
}
}