From 650427e01af6f22590206253d2ab1a7542f42b10 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 15 Apr 2020 17:45:00 +0200 Subject: [PATCH] Reset the assigned signaling server after some time Signed-off-by: Joas Schilling --- appinfo/info.xml | 3 +- .../ResetAssignedSignalingServer.php | 49 +++++++++++++++++++ lib/Manager.php | 18 +++++++ lib/Room.php | 17 +++++++ 4 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 lib/BackgroundJob/ResetAssignedSignalingServer.php diff --git a/appinfo/info.xml b/appinfo/info.xml index b35566c574..d3b7a8ec38 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -16,7 +16,7 @@ And in the works for the [coming versions](https://github.com/nextcloud/spreed/m ]]> - 9.0.0-dev.6 + 9.0.0-dev.7 agpl Daniel Calviño Sánchez @@ -52,6 +52,7 @@ And in the works for the [coming versions](https://github.com/nextcloud/spreed/m OCA\Talk\BackgroundJob\ExpireSignalingMessage OCA\Talk\BackgroundJob\RemoveEmptyRooms + OCA\Talk\BackgroundJob\ResetAssignedSignalingServer diff --git a/lib/BackgroundJob/ResetAssignedSignalingServer.php b/lib/BackgroundJob/ResetAssignedSignalingServer.php new file mode 100644 index 0000000000..f62882c63c --- /dev/null +++ b/lib/BackgroundJob/ResetAssignedSignalingServer.php @@ -0,0 +1,49 @@ + + * + * @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 . + * + */ + +namespace OCA\Talk\BackgroundJob; + +use OC\BackgroundJob\TimedJob; +use OCA\Talk\Manager; +use OCP\ICache; +use OCP\ICacheFactory; + +class ResetAssignedSignalingServer extends TimedJob { + + /** @var Manager */ + protected $manager; + /** @var ICache */ + protected $cache; + + public function __construct(Manager $manager, + ICacheFactory $cacheFactory) { + // Every 5 minutes + $this->setInterval(60 * 5); + + $this->manager = $manager; + $this->cache = $cacheFactory->createDistributed('hpb_servers'); + } + + protected function run($argument): void { + $this->manager->resetAssignedSignalingServers($this->cache); + } +} diff --git a/lib/Manager.php b/lib/Manager.php index f4c8bfde74..e55e3f6f48 100644 --- a/lib/Manager.php +++ b/lib/Manager.php @@ -34,6 +34,7 @@ use OCP\Comments\IComment; use OCP\Comments\NotFoundException; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\EventDispatcher\IEventDispatcher; +use OCP\ICache; use OCP\IConfig; use OCP\IDBConnection; use OCP\IL10N; @@ -207,6 +208,23 @@ class Manager { } } + public function resetAssignedSignalingServers(ICache $cache): void { + $query = $this->db->getQueryBuilder(); + $query->select('*') + ->from('talk_rooms') + ->where($query->expr()->isNotNull('assigned_hpb')); + + $result = $query->execute(); + while ($row = $result->fetch()) { + $room = $this->createRoomObject($row); + if (!$room->hasActiveSessions()) { + $room->setAssignedSignalingServer(null); + $cache->remove($room->getToken()); + } + } + $result->closeCursor(); + } + /** * @param string $participant * @param bool $includeLastMessage diff --git a/lib/Room.php b/lib/Room.php index 9ab23c51e5..0791b6c67d 100644 --- a/lib/Room.php +++ b/lib/Room.php @@ -1127,6 +1127,23 @@ class Room { return $participants; } + /** + * @return bool + */ + public function hasActiveSessions(): bool { + $query = $this->db->getQueryBuilder(); + $query->select('room_id') + ->from('talk_participants') + ->where($query->expr()->eq('room_id', $query->createNamedParameter($this->getId(), IQueryBuilder::PARAM_INT))) + ->andWhere($query->expr()->neq('session_id', $query->createNamedParameter('0'))) + ->setMaxResults(1); + $result = $query->execute(); + $row = $result->fetch(); + $result->closeCursor(); + + return (bool) $row; + } + /** * @return string[] */