BuildReminderIndexBackgroundJob.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 IDBConnection */
  21. private $db;
  22. /** @var ReminderService */
  23. private $reminderService;
  24. private LoggerInterface $logger;
  25. /** @var IJobList */
  26. private $jobList;
  27. /** @var ITimeFactory */
  28. private $timeFactory;
  29. /**
  30. * BuildReminderIndexBackgroundJob constructor.
  31. */
  32. public function __construct(IDBConnection $db,
  33. ReminderService $reminderService,
  34. LoggerInterface $logger,
  35. IJobList $jobList,
  36. ITimeFactory $timeFactory) {
  37. parent::__construct($timeFactory);
  38. $this->db = $db;
  39. $this->reminderService = $reminderService;
  40. $this->logger = $logger;
  41. $this->jobList = $jobList;
  42. $this->timeFactory = $timeFactory;
  43. }
  44. public function run($argument) {
  45. $offset = (int) $argument['offset'];
  46. $stopAt = (int) $argument['stopAt'];
  47. $this->logger->info('Building calendar reminder index (' . $offset .'/' . $stopAt . ')');
  48. $offset = $this->buildIndex($offset, $stopAt);
  49. if ($offset >= $stopAt) {
  50. $this->logger->info('Building calendar reminder index done');
  51. } else {
  52. $this->jobList->add(self::class, [
  53. 'offset' => $offset,
  54. 'stopAt' => $stopAt
  55. ]);
  56. $this->logger->info('Scheduled a new BuildReminderIndexBackgroundJob with offset ' . $offset);
  57. }
  58. }
  59. /**
  60. * @param int $offset
  61. * @param int $stopAt
  62. * @return int
  63. */
  64. private function buildIndex(int $offset, int $stopAt):int {
  65. $startTime = $this->timeFactory->getTime();
  66. $query = $this->db->getQueryBuilder();
  67. $query->select('*')
  68. ->from('calendarobjects')
  69. ->where($query->expr()->lte('id', $query->createNamedParameter($stopAt)))
  70. ->andWhere($query->expr()->gt('id', $query->createNamedParameter($offset)))
  71. ->orderBy('id', 'ASC');
  72. $result = $query->executeQuery();
  73. while ($row = $result->fetch(\PDO::FETCH_ASSOC)) {
  74. $offset = (int) $row['id'];
  75. if (is_resource($row['calendardata'])) {
  76. $row['calendardata'] = stream_get_contents($row['calendardata']);
  77. }
  78. $row['component'] = $row['componenttype'];
  79. try {
  80. $this->reminderService->onCalendarObjectCreate($row);
  81. } catch (\Exception $ex) {
  82. $this->logger->error($ex->getMessage(), ['exception' => $ex]);
  83. }
  84. if (($this->timeFactory->getTime() - $startTime) > 15) {
  85. $result->closeCursor();
  86. return $offset;
  87. }
  88. }
  89. $result->closeCursor();
  90. return $stopAt;
  91. }
  92. }