diff options
author | Maxence Lange <maxence@artificial-owl.com> | 2018-11-27 15:59:19 -0100 |
---|---|---|
committer | Maxence Lange <maxence@artificial-owl.com> | 2018-11-27 15:59:19 -0100 |
commit | b2a045cb696d85c5e21a7c25ad72619a5ca607b1 (patch) | |
tree | 33604b6e23220a65c19ed9e2268f6bb9d60493c1 /lib/Db | |
parent | 6210a6ec96f9bf6d92c5023724237028e88c5802 (diff) |
queue and async
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
Diffstat (limited to 'lib/Db')
-rw-r--r-- | lib/Db/CoreRequestBuilder.php | 19 | ||||
-rw-r--r-- | lib/Db/NotesRequestBuilder.php | 4 | ||||
-rw-r--r-- | lib/Db/RequestQueueRequest.php | 199 | ||||
-rw-r--r-- | lib/Db/RequestQueueRequestBuilder.php | 116 |
4 files changed, 334 insertions, 4 deletions
diff --git a/lib/Db/CoreRequestBuilder.php b/lib/Db/CoreRequestBuilder.php index 9224600d..c22dde13 100644 --- a/lib/Db/CoreRequestBuilder.php +++ b/lib/Db/CoreRequestBuilder.php @@ -53,6 +53,8 @@ use OCP\IDBConnection; class CoreRequestBuilder { + const TABLE_REQUEST_QUEUE = 'social_request_queue'; + const TABLE_SERVER_ACTORS = 'social_server_actors'; const TABLE_SERVER_NOTES = 'social_server_notes'; const TABLE_SERVER_FOLLOWS = 'social_server_follows'; @@ -159,6 +161,17 @@ class CoreRequestBuilder { /** + * Limit the request to the token + * + * @param IQueryBuilder $qb + * @param string $token + */ + protected function limitToToken(IQueryBuilder &$qb, string $token) { + $this->limitToDBField($qb, 'token', $token); + } + + + /** * Limit the request to the ActorId * * @param IQueryBuilder $qb @@ -262,10 +275,10 @@ class CoreRequestBuilder { * Limit the request to the status * * @param IQueryBuilder $qb - * @param string $status + * @param int $status */ - protected function limitToStatus(IQueryBuilder &$qb, $status) { - $this->limitToDBField($qb, 'status', $status); + protected function limitToStatus(IQueryBuilder &$qb, int $status) { + $this->limitToDBFieldInt($qb, 'status', $status); } diff --git a/lib/Db/NotesRequestBuilder.php b/lib/Db/NotesRequestBuilder.php index 16c5999d..90ff24e1 100644 --- a/lib/Db/NotesRequestBuilder.php +++ b/lib/Db/NotesRequestBuilder.php @@ -177,7 +177,9 @@ class NotesRequestBuilder extends CoreRequestBuilder { $instances = json_decode($data['instances'], true); if (is_array($instances)) { foreach ($instances as $instance) { - $note->addInstancePath(new InstancePath($instance['uri'], $instance['type'])); + $instancePath = new InstancePath(); + $instancePath->import($instance); + $note->addInstancePath($instancePath); } } diff --git a/lib/Db/RequestQueueRequest.php b/lib/Db/RequestQueueRequest.php new file mode 100644 index 00000000..33ca298c --- /dev/null +++ b/lib/Db/RequestQueueRequest.php @@ -0,0 +1,199 @@ +<?php +declare(strict_types=1); + + +/** + * Nextcloud - Social Support + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Maxence Lange <maxence@artificial-owl.com> + * @copyright 2018, Maxence Lange <maxence@artificial-owl.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\Social\Db; + + +use DateTime; +use Exception; +use OCA\Social\Exceptions\QueueStatusException; +use OCA\Social\Model\RequestQueue; +use OCA\Social\Service\QueueService; +use OCP\DB\QueryBuilder\IQueryBuilder; + + +/** + * Class RequestQueueRequest + * + * @package OCA\Social\Db + */ +class RequestQueueRequest extends RequestQueueRequestBuilder { + + + /** + * create a new Queue in the database. + * + * @param RequestQueue[] $queues + * + * @throws Exception + */ + public function multiple(array $queues) { + foreach ($queues as $queue) { + $this->create($queue); +// $qb->values( +// [ +// 'source' => $qb->createNamedParameter($queue->getSource()), +// 'activity' => $qb->createNamedParameter($queue->getActivity()), +// 'instance' => $qb->createNamedParameter( +// json_encode($queue->getInstance(), JSON_UNESCAPED_SLASHES) +// ), +// 'status' => $qb->createNamedParameter($queue->getStatus()), +// 'tries' => $qb->createNamedParameter($queue->getTries()), +// 'last' => $qb->createNamedParameter($queue->getLast()) +// ] +// ); + } + } + + + /** + * create a new Queue in the database. + * + * @param RequestQueue $queue + * + * @throws Exception + */ + public function create(RequestQueue $queue) { + $qb = $this->getQueueInsertSql(); + $qb->setValue('token', $qb->createNamedParameter($queue->getToken())) + ->setValue('author', $qb->createNamedParameter($queue->getAuthor())) + ->setValue('activity', $qb->createNamedParameter($queue->getActivity())) + ->setValue( + 'instance', $qb->createNamedParameter( + json_encode($queue->getInstance(), JSON_UNESCAPED_SLASHES) + ) + ) + ->setValue('priority', $qb->createNamedParameter($queue->getPriority())) + ->setValue('status', $qb->createNamedParameter($queue->getStatus())) + ->setValue('tries', $qb->createNamedParameter($queue->getTries())) + ->setValue('last', $qb->createNamedParameter($queue->getLast())); + $qb->execute(); + } + + + /** + * return Actor from database based on the username + * + * @param string $token + * @param int $status + * + * @return RequestQueue[] + */ + public function getFromToken(string $token, int $status = -1): array { + $qb = $this->getQueueSelectSql(); + $this->limitToToken($qb, $token); + + if ($status > -1) { + $this->limitToStatus($qb, $status); + } + + $this->orderByPriority($qb); + + $requests = []; + $cursor = $qb->execute(); + while ($data = $cursor->fetch()) { + $requests[] = $this->parseQueueSelectSql($data); + } + $cursor->closeCursor(); + + return $requests; + } + + + /** + * @param RequestQueue $queue + * + * @throws QueueStatusException + */ + public function setAsRunning(RequestQueue &$queue) { + $qb = $this->getQueueUpdateSql(); + $qb->set('status', $qb->createNamedParameter(RequestQueue::STATUS_RUNNING)) + ->set( + 'last', + $qb->createNamedParameter(new DateTime('now'), IQueryBuilder::PARAM_DATE) + ); + $this->limitToId($qb, $queue->getId()); + $this->limitToStatus($qb, RequestQueue::STATUS_STANDBY); + + $count = $qb->execute(); + + if ($count === 0) { + throw new QueueStatusException(); + } + + $queue->setStatus(RequestQueue::STATUS_RUNNING); + } + + + /** + * @param RequestQueue $queue + * + * @throws QueueStatusException + */ + public function setAsSuccess(RequestQueue &$queue) { + $qb = $this->getQueueUpdateSql(); + $qb->set('status', $qb->createNamedParameter(RequestQueue::STATUS_SUCCESS)); + $this->limitToId($qb, $queue->getId()); + $this->limitToStatus($qb, RequestQueue::STATUS_RUNNING); + + $count = $qb->execute(); + + if ($count === 0) { + throw new QueueStatusException(); + } + + $queue->setStatus(RequestQueue::STATUS_SUCCESS); + } + + + /** + * @param RequestQueue $queue >ll + * + * @throws QueueStatusException + */ + public function setAsFailure(RequestQueue &$queue) { + $qb = $this->getQueueUpdateSql(); + $qb->set('status', $qb->createNamedParameter(RequestQueue::STATUS_STANDBY)); + // TODO - increment tries++ +// ->set('tries', 'tries+1'); + $this->limitToId($qb, $queue->getId()); + $this->limitToStatus($qb, RequestQueue::STATUS_RUNNING); + + $count = $qb->execute(); + + if ($count === 0) { + throw new QueueStatusException(); + } + + $queue->setStatus(RequestQueue::STATUS_SUCCESS); + } + +} + diff --git a/lib/Db/RequestQueueRequestBuilder.php b/lib/Db/RequestQueueRequestBuilder.php new file mode 100644 index 00000000..41060f80 --- /dev/null +++ b/lib/Db/RequestQueueRequestBuilder.php @@ -0,0 +1,116 @@ +<?php +declare(strict_types=1); + + +/** + * Nextcloud - Social Support + * + * This file is licensed under the Affero General Public License version 3 or + * later. See the COPYING file. + * + * @author Maxence Lange <maxence@artificial-owl.com> + * @copyright 2018, Maxence Lange <maxence@artificial-owl.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\Social\Db; + + +use daita\MySmallPhpTools\Traits\TArrayTools; +use OCA\Social\Model\RequestQueue; +use OCP\DB\QueryBuilder\IQueryBuilder; + +class RequestQueueRequestBuilder extends CoreRequestBuilder { + + + use TArrayTools; + + + /** + * Base of the Sql Insert request + * + * @return IQueryBuilder + */ + protected function getQueueInsertSql(): IQueryBuilder { + $qb = $this->dbConnection->getQueryBuilder(); + $qb->insert(self::TABLE_REQUEST_QUEUE); + + return $qb; + } + + + /** + * Base of the Sql Update request + * + * @return IQueryBuilder + */ + protected function getQueueUpdateSql(): IQueryBuilder { + $qb = $this->dbConnection->getQueryBuilder(); + $qb->update(self::TABLE_REQUEST_QUEUE); + + return $qb; + } + + + /** + * Base of the Sql Select request for Shares + * + * @return IQueryBuilder + */ + protected function getQueueSelectSql(): IQueryBuilder { + $qb = $this->dbConnection->getQueryBuilder(); + + /** @noinspection PhpMethodParametersCountMismatchInspection */ + $qb->select( + 'rq.id', 'rq.token', 'rq.author', 'rq.activity', 'rq.instance', 'rq.priority', + 'rq.status', 'rq.tries', 'rq.last' + ) + ->from(self::TABLE_REQUEST_QUEUE, 'rq'); + + $this->defaultSelectAlias = 'rq'; + + return $qb; + } + + + /** + * Base of the Sql Delete request + * + * @return IQueryBuilder + */ + protected function getQueueDeleteSql(): IQueryBuilder { + $qb = $this->dbConnection->getQueryBuilder(); + $qb->delete(self::TABLE_REQUEST_QUEUE); + + return $qb; + } + + + /** + * @param array $data + * + * @return RequestQueue + */ + protected function parseQueueSelectSql($data): RequestQueue { + $queue = new RequestQueue(); + $queue->importFromDatabase($data); + + return $queue; + } + +} + |