periodically query calendar resource / room backends for updated resource / room information

Signed-off-by: Georg Ehrke <developer@georgehrke.com>
pull/9773/head
Georg Ehrke 6 years ago
parent 9aca92c441
commit 8f061f5407
No known key found for this signature in database
GPG Key ID: 9D98FD9380A1CB43

@ -48,6 +48,19 @@ $eventDispatcher->addListener('OCP\Federation\TrustedServerEvent::remove',
}
);
$eventHandler = function() use ($app) {
try {
$job = $app->getContainer()->query(\OCA\DAV\BackgroundJob\UpdateCalendarResourcesRoomsBackgroundJob::class);
$job->run();
$app->getContainer()->getServer()->getJobList()->setLastRun($job);
} catch(\Exception $ex) {
$app->getContainer()->getServer()->getLogger()->logException($ex);
}
};
$eventDispatcher->addListener('\OCP\Calendar\Resource\ForceRefreshEvent', $eventHandler);
$eventDispatcher->addListener('\OCP\Calendar\Room\ForceRefreshEvent', $eventHandler);
$cm = \OC::$server->getContactsManager();
$cm->register(function() use ($cm, $app) {
$user = \OC::$server->getUserSession()->getUser();

@ -21,6 +21,7 @@
<background-jobs>
<job>OCA\DAV\BackgroundJob\CleanupDirectLinksJob</job>
<job>OCA\DAV\BackgroundJob\UpdateCalendarResourcesRoomsBackgroundJob</job>
</background-jobs>
<repair-steps>

@ -0,0 +1,335 @@
<?php
/**
* @copyright 2018, Georg Ehrke <oc.list@georgehrke.com>
*
* @author Georg Ehrke <oc.list@georgehrke.com>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\DAV\BackgroundJob;
use OC\BackgroundJob\TimedJob;
use OCA\DAV\CalDAV\CalDavBackend;
use OCP\Calendar\BackendTemporarilyUnavailableException;
use OCP\Calendar\Resource\IManager as IResourceManager;
use OCP\Calendar\Resource\IResource;
use OCP\Calendar\Room\IManager as IRoomManager;
use OCP\Calendar\Room\IRoom;
use OCP\IDBConnection;
class UpdateCalendarResourcesRoomsBackgroundJob extends TimedJob {
/** @var IResourceManager */
private $resourceManager;
/** @var IRoomManager */
private $roomManager;
/** @var IDBConnection */
private $db;
/** @var CalDavBackend */
private $calDavBackend;
/** @var string */
private $resourceDbTable;
/** @var string */
private $resourcePrincipalUri;
/** @var string */
private $roomDbTable;
/** @var string */
private $roomPrincipalUri;
/**
* UpdateCalendarResourcesRoomsBackgroundJob constructor.
*
* @param IResourceManager $resourceManager
* @param IRoomManager $roomManager
* @param IDBConnection $dbConnection
* @param CalDavBackend $calDavBackend
*/
public function __construct(IResourceManager $resourceManager, IRoomManager $roomManager,
IDBConnection $dbConnection, CalDavBackend $calDavBackend) {
$this->resourceManager = $resourceManager;
$this->roomManager = $roomManager;
$this->db = $dbConnection;
$this->calDavBackend = $calDavBackend;
$this->resourceDbTable = 'calendar_resources_cache';
$this->resourcePrincipalUri = 'principals/calendar-resources';
$this->roomDbTable = 'calendar_rooms_cache';
$this->roomPrincipalUri = 'principals/calendar-rooms';
// run once an hour
$this->setInterval(60 * 60);
}
/**
* @param $argument
*/
public function run($argument) {
$this->runResources();
$this->runRooms();
}
/**
* run timed job for resources
*/
private function runResources() {
$resourceBackends = $this->resourceManager->getBackends();
$cachedResources = $this->getCached($this->resourceDbTable);
$cachedResourceIds = $this->getCachedResourceIds($cachedResources);
$remoteResourceIds = [];
foreach($resourceBackends as $resourceBackend) {
try {
$remoteResourceIds[$resourceBackend->getBackendIdentifier()] =
$resourceBackend->listAllResources();
} catch(BackendTemporarilyUnavailableException $ex) {
// If the backend is temporarily unavailable
// ignore this backend in this execution
unset($cachedResourceIds[$resourceBackend->getBackendIdentifier()]);
}
}
$sortedResources = $this->sortByNewDeletedExisting($cachedResourceIds, $remoteResourceIds);
foreach($sortedResources['new'] as $backendId => $newResources) {
foreach ($newResources as $newResource) {
$resource = $this->resourceManager->getBackend($backendId)
->getResource($newResource);
$this->addToCache($this->resourceDbTable, $resource);
}
}
foreach($sortedResources['deleted'] as $backendId => $deletedResources) {
foreach ($deletedResources as $deletedResource) {
$this->deleteFromCache($this->resourceDbTable,
$this->resourcePrincipalUri, $backendId, $deletedResource);
}
}
foreach($sortedResources['edited'] as $backendId => $editedResources) {
foreach ($editedResources as $editedResource) {
$resource = $this->resourceManager->getBackend($backendId)
->getResource($editedResource);
$this->updateCache($this->resourceDbTable, $resource);
}
}
}
/**
* run timed job for rooms
*/
private function runRooms() {
$roomBackends = $this->roomManager->getBackends();
$cachedRooms = $this->getCached($this->roomDbTable);
$cachedRoomIds = $this->getCachedRoomIds($cachedRooms);
$remoteRoomIds = [];
foreach($roomBackends as $roomBackend) {
try {
$remoteRoomIds[$roomBackend->getBackendIdentifier()] =
$roomBackend->listAllRooms();
} catch(BackendTemporarilyUnavailableException $ex) {
// If the backend is temporarily unavailable
// ignore this backend in this execution
unset($cachedRoomIds[$roomBackend->getBackendIdentifier()]);
}
}
$sortedRooms = $this->sortByNewDeletedExisting($cachedRoomIds, $remoteRoomIds);
foreach($sortedRooms['new'] as $backendId => $newRooms) {
foreach ($newRooms as $newRoom) {
$resource = $this->roomManager->getBackend($backendId)
->getRoom($newRoom);
$this->addToCache($this->roomDbTable, $resource);
}
}
foreach($sortedRooms['deleted'] as $backendId => $deletedRooms) {
foreach ($deletedRooms as $deletedRoom) {
$this->deleteFromCache($this->roomDbTable,
$this->roomPrincipalUri, $backendId, $deletedRoom);
}
}
foreach($sortedRooms['edited'] as $backendId => $editedRooms) {
foreach ($editedRooms as $editedRoom) {
$resource = $this->roomManager->getBackend($backendId)
->getRoom($editedRoom);
$this->updateCache($this->roomDbTable, $resource);
}
}
}
/**
* get cached db rows for resources / rooms
* @param string $tableName
* @return array
*/
private function getCached($tableName):array {
$query = $this->db->getQueryBuilder();
$query->select('*')->from($tableName);
$rows = [];
$stmt = $query->execute();
while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
$rows[] = $row;
}
return $rows;
}
/**
* @param array $cachedResources
* @return array
*/
private function getCachedResourceIds(array $cachedResources):array {
$cachedResourceIds = [];
foreach ($cachedResources as $cachedResource) {
if (!isset($cachedResourceIds[$cachedResource['backend_id']])) {
$cachedResourceIds[$cachedResource['backend_id']] = [];
}
$cachedResourceIds[$cachedResource['backend_id']][] =
$cachedResource['resource_id'];
}
return $cachedResourceIds;
}
/**
* @param array $cachedRooms
* @return array
*/
private function getCachedRoomIds(array $cachedRooms):array {
$cachedRoomIds = [];
foreach ($cachedRooms as $cachedRoom) {
if (!isset($cachedRoomIds[$cachedRoom['backend_id']])) {
$cachedRoomIds[$cachedRoom['backend_id']] = [];
}
$cachedRoomIds[$cachedRoom['backend_id']][] =
$cachedRoom['resource_id'];
}
return $cachedRoomIds;
}
/**
* sort list of ids by whether they appear only in the backend /
* only in the cache / in both
*
* @param array $cached
* @param array $remote
* @return array
*/
private function sortByNewDeletedExisting(array $cached, array $remote):array {
$sorted = [
'new' => [],
'deleted' => [],
'existing' => [],
];
$backendIds = array_merge(array_keys($cached), array_keys($remote));
foreach($backendIds as $backendId) {
if (!isset($cached[$backendId])) {
$sorted['new'][$backendId] = $remote[$backendId];
} elseif (!isset($remote[$backendId])) {
$sorted['deleted'][$backendId] = $remote[$backendId];
} else {
$sorted['new'][$backendId] = array_diff($remote[$backendId], $cached[$backendId]);
$sorted['deleted'][$backendId] = array_diff($cached[$backendId], $remote[$backendId]);
$sorted['existing'][$backendId] = array_intersect($remote[$backendId], $cached[$backendId]);
}
}
return $sorted;
}
/**
* add entry to cache that exists remotely but not yet in cache
*
* @param string $table
* @param IResource|IRoom $remote
*/
private function addToCache($table, $remote) {
$query = $this->db->getQueryBuilder();
$query->insert($table)
->values([
'backend_id' => $query->createNamedParameter($remote->getBackend()),
'resource_id' => $query->createNamedParameter($remote->getId()),
'email' => $query->createNamedParameter($remote->getEMail()),
'displayname' => $query->createNamedParameter($remote->getDisplayName()),
'group_restrictions' => $query->createNamedParameter(
$this->serializeGroupRestrictions(
$remote->getGroupRestrictions()
))
])
->execute();
}
/**
* delete entry from cache that does not exist anymore remotely
*
* @param string $table
* @param string $principalUri
* @param string $backendId
* @param string $resourceId
*/
private function deleteFromCache($table, $principalUri, $backendId, $resourceId) {
$query = $this->db->getQueryBuilder();
$query->delete($table)
->where($query->expr()->eq('backend_id', $query->createNamedParameter($backendId)))
->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($resourceId)))
->execute();
$calendar = $this->calDavBackend->getCalendarByUri($principalUri, implode('-', [$backendId, $resourceId]));
if ($calendar !== null) {
$this->calDavBackend->deleteCalendar($calendar['id']);
}
}
/**
* update an existing entry in cache
*
* @param string $table
* @param IResource|IRoom $remote
*/
private function updateCache($table, $remote) {
$query = $this->db->getQueryBuilder();
$query->update($table)
->set('email', $query->createNamedParameter($remote->getEMail()))
->set('displayname', $query->createNamedParameter($remote->getDisplayName()))
->set('group_restrictions', $query->createNamedParameter(
$this->serializeGroupRestrictions(
$remote->getGroupRestrictions()
)))
->execute();
}
/**
* serialize array of group restrictions to store them in database
*
* @param array $groups
* @return string
*/
private function serializeGroupRestrictions(array $groups):string {
return \json_encode($groups);
}
}

@ -60,6 +60,18 @@ class Manager implements \OCP\Calendar\Resource\IManager {
return array_values($this->backends);
}
/**
* @param string $backendId
* @return IBackend|null
*/
public function getBackend($backendId):IBackend {
if (!isset($this->backends[$backendId])) {
return null;
}
return $this->backends[$backendId];
}
/**
* removes all registered backend instances
* @return void

@ -60,6 +60,18 @@ class Manager implements \OCP\Calendar\Room\IManager {
return array_values($this->backends);
}
/**
* @param string $backendId
* @return IBackend|null
*/
public function getBackend($backendId):IBackend {
if (!isset($this->backends[$backendId])) {
return null;
}
return $this->backends[$backendId];
}
/**
* removes all registered backend instances
* @return void

@ -0,0 +1,26 @@
<?php
/**
* @copyright 2018, Georg Ehrke <oc.list@georgehrke.com>
*
* @author Georg Ehrke <oc.list@georgehrke.com>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/
namespace OCP\Calendar;
class BackendTemporarilyUnavailableException extends \Exception {}

@ -22,6 +22,7 @@
*/
namespace OCP\Calendar\Resource;
use OCP\Calendar\BackendTemporarilyUnavailableException;
/**
* Interface IBackend
@ -34,6 +35,7 @@ interface IBackend {
/**
* get a list of all resources in this backend
*
* @throws BackendTemporarilyUnavailableException
* @return IResource[]
*/
public function getAllResources():array;
@ -41,6 +43,7 @@ interface IBackend {
/**
* get a list of all resource identifiers in this backend
*
* @throws BackendTemporarilyUnavailableException
* @return string[]
*/
public function listAllResources():array;
@ -49,6 +52,7 @@ interface IBackend {
* get a resource by it's id
*
* @param string $id
* @throws BackendTemporarilyUnavailableException
* @return IResource|null
*/
public function getResource($id);

@ -49,6 +49,12 @@ interface IManager {
*/
public function getBackends():array;
/**
* @param string $backendId
* @return IBackend
*/
public function getBackend($backendId):IBackend;
/**
* removes all registered backend instances
* @return void

@ -22,6 +22,7 @@
*/
namespace OCP\Calendar\Room;
use OCP\Calendar\BackendTemporarilyUnavailableException;
/**
* Interface IBackend
@ -34,6 +35,7 @@ interface IBackend {
/**
* get a list of all rooms in this backend
*
* @throws BackendTemporarilyUnavailableException
* @return IRoom[]
*/
public function getAllRooms():array;
@ -41,6 +43,7 @@ interface IBackend {
/**
* get a list of all room identifiers in this backend
*
* @throws BackendTemporarilyUnavailableException
* @return string[]
*/
public function listAllRooms():array;
@ -49,6 +52,7 @@ interface IBackend {
* get a room by it's id
*
* @param string $id
* @throws BackendTemporarilyUnavailableException
* @return IRoom|null
*/
public function getRoom($id);

@ -49,6 +49,12 @@ interface IManager {
*/
public function getBackends():array;
/**
* @param string $backendId
* @return IBackend
*/
public function getBackend($backendId):IBackend;
/**
* removes all registered backend instances
* @return void

Loading…
Cancel
Save