BuildReminderIndexBackgroundJob.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\BackgroundJob;
  8. use OCA\DAV\CalDAV\Reminder\ReminderService;
  9. use OCP\AppFramework\Utility\ITimeFactory;
  10. use OCP\BackgroundJob\IJobList;
  11. use OCP\BackgroundJob\QueuedJob;
  12. use OCP\IDBConnection;
  13. use Psr\Log\LoggerInterface;
  14. /**
  15. * Class BuildReminderIndexBackgroundJob
  16. *
  17. * @package OCA\DAV\BackgroundJob
  18. */
  19. class BuildReminderIndexBackgroundJob extends QueuedJob {
  20. /** @var ITimeFactory */
  21. private $timeFactory;
  22. /**
  23. * BuildReminderIndexBackgroundJob constructor.
  24. */
  25. public function __construct(
  26. private IDBConnection $db,
  27. private ReminderService $reminderService,
  28. private LoggerInterface $logger,
  29. private IJobList $jobList,
  30. ITimeFactory $timeFactory,
  31. ) {
  32. parent::__construct($timeFactory);
  33. $this->timeFactory = $timeFactory;
  34. }
  35. public function run($argument) {
  36. $offset = (int)$argument['offset'];
  37. $stopAt = (int)$argument['stopAt'];
  38. $this->logger->info('Building calendar reminder index (' . $offset . '/' . $stopAt . ')');
  39. $offset = $this->buildIndex($offset, $stopAt);
  40. if ($offset >= $stopAt) {
  41. $this->logger->info('Building calendar reminder index done');
  42. } else {
  43. $this->jobList->add(self::class, [
  44. 'offset' => $offset,
  45. 'stopAt' => $stopAt
  46. ]);
  47. $this->logger->info('Scheduled a new BuildReminderIndexBackgroundJob with offset ' . $offset);
  48. }
  49. }
  50. /**
  51. * @param int $offset
  52. * @param int $stopAt
  53. * @return int
  54. */
  55. private function buildIndex(int $offset, int $stopAt):int {
  56. $startTime = $this->timeFactory->getTime();
  57. $query = $this->db->getQueryBuilder();
  58. $query->select('*')
  59. ->from('calendarobjects')
  60. ->where($query->expr()->lte('id', $query->createNamedParameter($stopAt)))
  61. ->andWhere($query->expr()->gt('id', $query->createNamedParameter($offset)))
  62. ->orderBy('id', 'ASC');
  63. $result = $query->executeQuery();
  64. while ($row = $result->fetch(\PDO::FETCH_ASSOC)) {
  65. $offset = (int)$row['id'];
  66. if (is_resource($row['calendardata'])) {
  67. $row['calendardata'] = stream_get_contents($row['calendardata']);
  68. }
  69. $row['component'] = $row['componenttype'];
  70. try {
  71. $this->reminderService->onCalendarObjectCreate($row);
  72. } catch (\Exception $ex) {
  73. $this->logger->error($ex->getMessage(), ['exception' => $ex]);
  74. }
  75. if (($this->timeFactory->getTime() - $startTime) > 15) {
  76. $result->closeCursor();
  77. return $offset;
  78. }
  79. }
  80. $result->closeCursor();
  81. return $stopAt;
  82. }
  83. }