TaskMapper.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\TaskProcessing\Db;
  8. use OCP\AppFramework\Db\DoesNotExistException;
  9. use OCP\AppFramework\Db\Entity;
  10. use OCP\AppFramework\Db\MultipleObjectsReturnedException;
  11. use OCP\AppFramework\Db\QBMapper;
  12. use OCP\AppFramework\Utility\ITimeFactory;
  13. use OCP\DB\Exception;
  14. use OCP\DB\QueryBuilder\IQueryBuilder;
  15. use OCP\IDBConnection;
  16. /**
  17. * @extends QBMapper<Task>
  18. */
  19. class TaskMapper extends QBMapper {
  20. public function __construct(
  21. IDBConnection $db,
  22. private ITimeFactory $timeFactory,
  23. ) {
  24. parent::__construct($db, 'taskprocessing_tasks', Task::class);
  25. }
  26. /**
  27. * @param int $id
  28. * @return Task
  29. * @throws Exception
  30. * @throws DoesNotExistException
  31. * @throws MultipleObjectsReturnedException
  32. */
  33. public function find(int $id): Task {
  34. $qb = $this->db->getQueryBuilder();
  35. $qb->select(Task::$columns)
  36. ->from($this->tableName)
  37. ->where($qb->expr()->eq('id', $qb->createPositionalParameter($id)));
  38. return $this->findEntity($qb);
  39. }
  40. /**
  41. * @param list<string> $taskTypes
  42. * @param list<int> $taskIdsToIgnore
  43. * @return Task
  44. * @throws DoesNotExistException
  45. * @throws Exception
  46. */
  47. public function findOldestScheduledByType(array $taskTypes, array $taskIdsToIgnore): Task {
  48. $qb = $this->db->getQueryBuilder();
  49. $qb->select(Task::$columns)
  50. ->from($this->tableName)
  51. ->where($qb->expr()->eq('status', $qb->createPositionalParameter(\OCP\TaskProcessing\Task::STATUS_SCHEDULED, IQueryBuilder::PARAM_INT)))
  52. ->setMaxResults(1)
  53. ->orderBy('last_updated', 'ASC');
  54. if (!empty($taskTypes)) {
  55. $filter = [];
  56. foreach ($taskTypes as $taskType) {
  57. $filter[] = $qb->expr()->eq('type', $qb->createPositionalParameter($taskType));
  58. }
  59. $qb->andWhere($qb->expr()->orX(...$filter));
  60. }
  61. if (!empty($taskIdsToIgnore)) {
  62. $qb->andWhere($qb->expr()->notIn('id', $qb->createNamedParameter($taskIdsToIgnore, IQueryBuilder::PARAM_INT_ARRAY)));
  63. }
  64. return $this->findEntity($qb);
  65. }
  66. /**
  67. * @param int $id
  68. * @param string|null $userId
  69. * @return Task
  70. * @throws DoesNotExistException
  71. * @throws Exception
  72. * @throws MultipleObjectsReturnedException
  73. */
  74. public function findByIdAndUser(int $id, ?string $userId): Task {
  75. $qb = $this->db->getQueryBuilder();
  76. $qb->select(Task::$columns)
  77. ->from($this->tableName)
  78. ->where($qb->expr()->eq('id', $qb->createPositionalParameter($id)));
  79. if ($userId === null) {
  80. $qb->andWhere($qb->expr()->isNull('user_id'));
  81. } else {
  82. $qb->andWhere($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId)));
  83. }
  84. return $this->findEntity($qb);
  85. }
  86. /**
  87. * @param string|null $userId
  88. * @param string|null $taskType
  89. * @param string|null $customId
  90. * @return list<Task>
  91. * @throws Exception
  92. */
  93. public function findByUserAndTaskType(?string $userId, ?string $taskType = null, ?string $customId = null): array {
  94. $qb = $this->db->getQueryBuilder();
  95. $qb->select(Task::$columns)
  96. ->from($this->tableName)
  97. ->where($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId)));
  98. if ($taskType !== null) {
  99. $qb->andWhere($qb->expr()->eq('type', $qb->createPositionalParameter($taskType)));
  100. }
  101. if ($customId !== null) {
  102. $qb->andWhere($qb->expr()->eq('custom_id', $qb->createPositionalParameter($customId)));
  103. }
  104. return $this->findEntities($qb);
  105. }
  106. /**
  107. * @param string $userId
  108. * @param string $appId
  109. * @param string|null $customId
  110. * @return list<Task>
  111. * @throws Exception
  112. */
  113. public function findUserTasksByApp(?string $userId, string $appId, ?string $customId = null): array {
  114. $qb = $this->db->getQueryBuilder();
  115. $qb->select(Task::$columns)
  116. ->from($this->tableName)
  117. ->where($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId)))
  118. ->andWhere($qb->expr()->eq('app_id', $qb->createPositionalParameter($appId)));
  119. if ($customId !== null) {
  120. $qb->andWhere($qb->expr()->eq('custom_id', $qb->createPositionalParameter($customId)));
  121. }
  122. return $this->findEntities($qb);
  123. }
  124. /**
  125. * @param string|null $userId
  126. * @param string|null $taskType
  127. * @param string|null $appId
  128. * @param string|null $customId
  129. * @param int|null $status
  130. * @param int|null $scheduleAfter
  131. * @param int|null $endedBefore
  132. * @return list<Task>
  133. * @throws Exception
  134. */
  135. public function findTasks(
  136. ?string $userId, ?string $taskType = null, ?string $appId = null, ?string $customId = null,
  137. ?int $status = null, ?int $scheduleAfter = null, ?int $endedBefore = null): array {
  138. $qb = $this->db->getQueryBuilder();
  139. $qb->select(Task::$columns)
  140. ->from($this->tableName);
  141. // empty string: no userId filter
  142. if ($userId !== '') {
  143. $qb->where($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId)));
  144. }
  145. if ($taskType !== null) {
  146. $qb->andWhere($qb->expr()->eq('type', $qb->createPositionalParameter($taskType)));
  147. }
  148. if ($appId !== null) {
  149. $qb->andWhere($qb->expr()->eq('app_id', $qb->createPositionalParameter($appId)));
  150. }
  151. if ($customId !== null) {
  152. $qb->andWhere($qb->expr()->eq('custom_id', $qb->createPositionalParameter($customId)));
  153. }
  154. if ($status !== null) {
  155. $qb->andWhere($qb->expr()->eq('status', $qb->createPositionalParameter($status, IQueryBuilder::PARAM_INT)));
  156. }
  157. if ($scheduleAfter !== null) {
  158. $qb->andWhere($qb->expr()->isNotNull('scheduled_at'));
  159. $qb->andWhere($qb->expr()->gt('scheduled_at', $qb->createPositionalParameter($scheduleAfter, IQueryBuilder::PARAM_INT)));
  160. }
  161. if ($endedBefore !== null) {
  162. $qb->andWhere($qb->expr()->isNotNull('ended_at'));
  163. $qb->andWhere($qb->expr()->lt('ended_at', $qb->createPositionalParameter($endedBefore, IQueryBuilder::PARAM_INT)));
  164. }
  165. return $this->findEntities($qb);
  166. }
  167. /**
  168. * @param int $timeout
  169. * @return int the number of deleted tasks
  170. * @throws Exception
  171. */
  172. public function deleteOlderThan(int $timeout): int {
  173. $qb = $this->db->getQueryBuilder();
  174. $qb->delete($this->tableName)
  175. ->where($qb->expr()->lt('last_updated', $qb->createPositionalParameter($this->timeFactory->getDateTime()->getTimestamp() - $timeout)));
  176. return $qb->executeStatement();
  177. }
  178. public function update(Entity $entity): Entity {
  179. $entity->setLastUpdated($this->timeFactory->now()->getTimestamp());
  180. return parent::update($entity);
  181. }
  182. public function lockTask(Entity $entity): int {
  183. $qb = $this->db->getQueryBuilder();
  184. $qb->update($this->tableName)
  185. ->set('status', $qb->createPositionalParameter(\OCP\TaskProcessing\Task::STATUS_RUNNING, IQueryBuilder::PARAM_INT))
  186. ->where($qb->expr()->eq('id', $qb->createPositionalParameter($entity->getId(), IQueryBuilder::PARAM_INT)))
  187. ->andWhere($qb->expr()->neq('status', $qb->createPositionalParameter(2, IQueryBuilder::PARAM_INT)));
  188. try {
  189. return $qb->executeStatement();
  190. } catch (Exception) {
  191. return 0;
  192. }
  193. }
  194. }