BuildReminderIndexBackgroundJob.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2019 Georg Ehrke <oc.list@georgehrke.com>
  5. *
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  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. */
  24. namespace OCA\DAV\BackgroundJob;
  25. use OCP\BackgroundJob\QueuedJob;
  26. use OCA\DAV\CalDAV\Reminder\ReminderService;
  27. use OCP\AppFramework\Utility\ITimeFactory;
  28. use OCP\BackgroundJob\IJobList;
  29. use OCP\IDBConnection;
  30. use OCP\ILogger;
  31. /**
  32. * Class BuildReminderIndexBackgroundJob
  33. *
  34. * @package OCA\DAV\BackgroundJob
  35. */
  36. class BuildReminderIndexBackgroundJob extends QueuedJob {
  37. /** @var IDBConnection */
  38. private $db;
  39. /** @var ReminderService */
  40. private $reminderService;
  41. /** @var ILogger */
  42. private $logger;
  43. /** @var IJobList */
  44. private $jobList;
  45. /** @var ITimeFactory */
  46. private $timeFactory;
  47. /**
  48. * BuildReminderIndexBackgroundJob constructor.
  49. *
  50. * @param IDBConnection $db
  51. * @param ReminderService $reminderService
  52. * @param ILogger $logger
  53. * @param IJobList $jobList
  54. * @param ITimeFactory $timeFactory
  55. */
  56. public function __construct(IDBConnection $db,
  57. ReminderService $reminderService,
  58. ILogger $logger,
  59. IJobList $jobList,
  60. ITimeFactory $timeFactory) {
  61. $this->db = $db;
  62. $this->reminderService = $reminderService;
  63. $this->logger = $logger;
  64. $this->jobList = $jobList;
  65. $this->timeFactory = $timeFactory;
  66. }
  67. /**
  68. * @param $arguments
  69. */
  70. public function run($arguments) {
  71. $offset = (int) $arguments['offset'];
  72. $stopAt = (int) $arguments['stopAt'];
  73. $this->logger->info('Building calendar reminder index (' . $offset .'/' . $stopAt . ')');
  74. $offset = $this->buildIndex($offset, $stopAt);
  75. if ($offset >= $stopAt) {
  76. $this->logger->info('Building calendar reminder index done');
  77. } else {
  78. $this->jobList->add(self::class, [
  79. 'offset' => $offset,
  80. 'stopAt' => $stopAt
  81. ]);
  82. $this->logger->info('Scheduled a new BuildReminderIndexBackgroundJob with offset ' . $offset);
  83. }
  84. }
  85. /**
  86. * @param int $offset
  87. * @param int $stopAt
  88. * @return int
  89. */
  90. private function buildIndex(int $offset, int $stopAt):int {
  91. $startTime = $this->timeFactory->getTime();
  92. $query = $this->db->getQueryBuilder();
  93. $query->select('*')
  94. ->from('calendarobjects')
  95. ->where($query->expr()->lte('id', $query->createNamedParameter($stopAt)))
  96. ->andWhere($query->expr()->gt('id', $query->createNamedParameter($offset)))
  97. ->orderBy('id', 'ASC');
  98. $stmt = $query->execute();
  99. while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
  100. $offset = $row['id'];
  101. if (is_resource($row['calendardata'])) {
  102. $row['calendardata'] = stream_get_contents($row['calendardata']);
  103. }
  104. $row['component'] = $row['componenttype'];
  105. try {
  106. $this->reminderService->onTouchCalendarObject('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', $row);
  107. } catch(\Exception $ex) {
  108. $this->logger->logException($ex);
  109. }
  110. if (($this->timeFactory->getTime() - $startTime) > 15) {
  111. return $offset;
  112. }
  113. }
  114. return $stopAt;
  115. }
  116. }