TaskMapper.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 string|null $taskType
  42. * @return Task
  43. * @throws DoesNotExistException
  44. * @throws Exception
  45. */
  46. public function findOldestScheduledByType(?string $taskType): Task {
  47. $qb = $this->db->getQueryBuilder();
  48. $qb->select(Task::$columns)
  49. ->from($this->tableName)
  50. ->where($qb->expr()->eq('status', $qb->createPositionalParameter(\OCP\TaskProcessing\Task::STATUS_SCHEDULED, IQueryBuilder::PARAM_INT)))
  51. ->setMaxResults(1)
  52. ->orderBy('last_updated', 'ASC');
  53. if ($taskType !== null) {
  54. $qb->andWhere($qb->expr()->eq('type', $qb->createPositionalParameter($taskType)));
  55. }
  56. return $this->findEntity($qb);
  57. }
  58. /**
  59. * @param int $id
  60. * @param string|null $userId
  61. * @return Task
  62. * @throws DoesNotExistException
  63. * @throws Exception
  64. * @throws MultipleObjectsReturnedException
  65. */
  66. public function findByIdAndUser(int $id, ?string $userId): Task {
  67. $qb = $this->db->getQueryBuilder();
  68. $qb->select(Task::$columns)
  69. ->from($this->tableName)
  70. ->where($qb->expr()->eq('id', $qb->createPositionalParameter($id)));
  71. if ($userId === null) {
  72. $qb->andWhere($qb->expr()->isNull('user_id'));
  73. } else {
  74. $qb->andWhere($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId)));
  75. }
  76. return $this->findEntity($qb);
  77. }
  78. /**
  79. * @param string|null $userId
  80. * @param string|null $taskType
  81. * @param string|null $customId
  82. * @return list<Task>
  83. * @throws Exception
  84. */
  85. public function findByUserAndTaskType(?string $userId, ?string $taskType = null, ?string $customId = null): array {
  86. $qb = $this->db->getQueryBuilder();
  87. $qb->select(Task::$columns)
  88. ->from($this->tableName)
  89. ->where($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId)));
  90. if ($taskType !== null) {
  91. $qb->andWhere($qb->expr()->eq('type', $qb->createPositionalParameter($taskType)));
  92. }
  93. if ($customId !== null) {
  94. $qb->andWhere($qb->expr()->eq('custom_id', $qb->createPositionalParameter($customId)));
  95. }
  96. return array_values($this->findEntities($qb));
  97. }
  98. /**
  99. * @param string $userId
  100. * @param string $appId
  101. * @param string|null $customId
  102. * @return list<Task>
  103. * @throws Exception
  104. */
  105. public function findUserTasksByApp(?string $userId, string $appId, ?string $customId = null): array {
  106. $qb = $this->db->getQueryBuilder();
  107. $qb->select(Task::$columns)
  108. ->from($this->tableName)
  109. ->where($qb->expr()->eq('user_id', $qb->createPositionalParameter($userId)))
  110. ->andWhere($qb->expr()->eq('app_id', $qb->createPositionalParameter($appId)));
  111. if ($customId !== null) {
  112. $qb->andWhere($qb->expr()->eq('custom_id', $qb->createPositionalParameter($customId)));
  113. }
  114. return array_values($this->findEntities($qb));
  115. }
  116. /**
  117. * @param int $timeout
  118. * @return int the number of deleted tasks
  119. * @throws Exception
  120. */
  121. public function deleteOlderThan(int $timeout): int {
  122. $qb = $this->db->getQueryBuilder();
  123. $qb->delete($this->tableName)
  124. ->where($qb->expr()->lt('last_updated', $qb->createPositionalParameter($this->timeFactory->getDateTime()->getTimestamp() - $timeout)));
  125. return $qb->executeStatement();
  126. }
  127. public function update(Entity $entity): Entity {
  128. $entity->setLastUpdated($this->timeFactory->now()->getTimestamp());
  129. return parent::update($entity);
  130. }
  131. }