TaskMapper.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Marcel Klehr <mklehr@gmx.net>
  5. *
  6. * @author Marcel Klehr <mklehr@gmx.net>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. namespace OC\TaskProcessing\Db;
  24. use OCP\AppFramework\Db\DoesNotExistException;
  25. use OCP\AppFramework\Db\Entity;
  26. use OCP\AppFramework\Db\MultipleObjectsReturnedException;
  27. use OCP\AppFramework\Db\QBMapper;
  28. use OCP\AppFramework\Utility\ITimeFactory;
  29. use OCP\DB\Exception;
  30. use OCP\DB\QueryBuilder\IQueryBuilder;
  31. use OCP\IDBConnection;
  32. /**
  33. * @extends QBMapper<Task>
  34. */
  35. class TaskMapper extends QBMapper {
  36. public function __construct(
  37. IDBConnection $db,
  38. private ITimeFactory $timeFactory,
  39. ) {
  40. parent::__construct($db, 'taskprocessing_tasks', Task::class);
  41. }
  42. /**
  43. * @param int $id
  44. * @return Task
  45. * @throws Exception
  46. * @throws DoesNotExistException
  47. * @throws MultipleObjectsReturnedException
  48. */
  49. public function find(int $id): Task {
  50. $qb = $this->db->getQueryBuilder();
  51. $qb->select(Task::$columns)
  52. ->from($this->tableName)
  53. ->where($qb->expr()->eq('id', $qb->createPositionalParameter($id)));
  54. return $this->findEntity($qb);
  55. }
  56. /**
  57. * @param string|null $taskType
  58. * @return Task
  59. * @throws DoesNotExistException
  60. * @throws Exception
  61. */
  62. public function findOldestScheduledByType(?string $taskType): Task {
  63. $qb = $this->db->getQueryBuilder();
  64. $qb->select(Task::$columns)
  65. ->from($this->tableName)
  66. ->where($qb->expr()->eq('status', $qb->createPositionalParameter(\OCP\TaskProcessing\Task::STATUS_SCHEDULED, IQueryBuilder::PARAM_INT)))
  67. ->setMaxResults(1)
  68. ->orderBy('last_updated', 'ASC');
  69. if ($taskType !== null) {
  70. $qb->andWhere($qb->expr()->eq('type', $qb->createPositionalParameter($taskType)));
  71. }
  72. return $this->findEntity($qb);
  73. }
  74. /**
  75. * @param int $id
  76. * @param string|null $userId
  77. * @return Task
  78. * @throws DoesNotExistException
  79. * @throws Exception
  80. * @throws MultipleObjectsReturnedException
  81. */
  82. public function findByIdAndUser(int $id, ?string $userId): Task {
  83. $qb = $this->db->getQueryBuilder();
  84. $qb->select(Task::$columns)
  85. ->from($this->tableName)
  86. ->where($qb->expr()->eq('id', $qb->createPositionalParameter($id)));
  87. if ($userId === null) {
  88. $qb->andWhere($qb->expr()->isNull('user_id'));
  89. } else {
  90. $qb->andWhere($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId)));
  91. }
  92. return $this->findEntity($qb);
  93. }
  94. /**
  95. * @param string|null $userId
  96. * @param string|null $taskType
  97. * @param string|null $customId
  98. * @return list<Task>
  99. * @throws Exception
  100. */
  101. public function findByUserAndTaskType(?string $userId, ?string $taskType = null, ?string $customId = null): array {
  102. $qb = $this->db->getQueryBuilder();
  103. $qb->select(Task::$columns)
  104. ->from($this->tableName)
  105. ->where($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId)));
  106. if ($taskType !== null) {
  107. $qb->andWhere($qb->expr()->eq('type', $qb->createPositionalParameter($taskType)));
  108. }
  109. if ($customId !== null) {
  110. $qb->andWhere($qb->expr()->eq('custom_id', $qb->createPositionalParameter($customId)));
  111. }
  112. return array_values($this->findEntities($qb));
  113. }
  114. /**
  115. * @param string $userId
  116. * @param string $appId
  117. * @param string|null $customId
  118. * @return list<Task>
  119. * @throws Exception
  120. */
  121. public function findUserTasksByApp(?string $userId, string $appId, ?string $customId = null): array {
  122. $qb = $this->db->getQueryBuilder();
  123. $qb->select(Task::$columns)
  124. ->from($this->tableName)
  125. ->where($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId)))
  126. ->andWhere($qb->expr()->eq('app_id', $qb->createPositionalParameter($appId)));
  127. if ($customId !== null) {
  128. $qb->andWhere($qb->expr()->eq('custom_id', $qb->createPositionalParameter($customId)));
  129. }
  130. return array_values($this->findEntities($qb));
  131. }
  132. /**
  133. * @param int $timeout
  134. * @return int the number of deleted tasks
  135. * @throws Exception
  136. */
  137. public function deleteOlderThan(int $timeout): int {
  138. $qb = $this->db->getQueryBuilder();
  139. $qb->delete($this->tableName)
  140. ->where($qb->expr()->lt('last_updated', $qb->createPositionalParameter($this->timeFactory->getDateTime()->getTimestamp() - $timeout)));
  141. return $qb->executeStatement();
  142. }
  143. public function update(Entity $entity): Entity {
  144. $entity->setLastUpdated($this->timeFactory->now()->getTimestamp());
  145. return parent::update($entity);
  146. }
  147. }