BuildReminderIndexBackgroundJob.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2019 Georg Ehrke <oc.list@georgehrke.com>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\DAV\BackgroundJob;
  27. use OCA\DAV\CalDAV\Reminder\ReminderService;
  28. use OCP\AppFramework\Utility\ITimeFactory;
  29. use OCP\BackgroundJob\IJobList;
  30. use OCP\BackgroundJob\QueuedJob;
  31. use OCP\IDBConnection;
  32. use OCP\ILogger;
  33. /**
  34. * Class BuildReminderIndexBackgroundJob
  35. *
  36. * @package OCA\DAV\BackgroundJob
  37. */
  38. class BuildReminderIndexBackgroundJob extends QueuedJob {
  39. /** @var IDBConnection */
  40. private $db;
  41. /** @var ReminderService */
  42. private $reminderService;
  43. /** @var ILogger */
  44. private $logger;
  45. /** @var IJobList */
  46. private $jobList;
  47. /** @var ITimeFactory */
  48. private $timeFactory;
  49. /**
  50. * BuildReminderIndexBackgroundJob constructor.
  51. *
  52. * @param IDBConnection $db
  53. * @param ReminderService $reminderService
  54. * @param ILogger $logger
  55. * @param IJobList $jobList
  56. * @param ITimeFactory $timeFactory
  57. */
  58. public function __construct(IDBConnection $db,
  59. ReminderService $reminderService,
  60. ILogger $logger,
  61. IJobList $jobList,
  62. ITimeFactory $timeFactory) {
  63. parent::__construct($timeFactory);
  64. $this->db = $db;
  65. $this->reminderService = $reminderService;
  66. $this->logger = $logger;
  67. $this->jobList = $jobList;
  68. $this->timeFactory = $timeFactory;
  69. }
  70. /**
  71. * @param $arguments
  72. */
  73. public function run($arguments) {
  74. $offset = (int) $arguments['offset'];
  75. $stopAt = (int) $arguments['stopAt'];
  76. $this->logger->info('Building calendar reminder index (' . $offset .'/' . $stopAt . ')');
  77. $offset = $this->buildIndex($offset, $stopAt);
  78. if ($offset >= $stopAt) {
  79. $this->logger->info('Building calendar reminder index done');
  80. } else {
  81. $this->jobList->add(self::class, [
  82. 'offset' => $offset,
  83. 'stopAt' => $stopAt
  84. ]);
  85. $this->logger->info('Scheduled a new BuildReminderIndexBackgroundJob with offset ' . $offset);
  86. }
  87. }
  88. /**
  89. * @param int $offset
  90. * @param int $stopAt
  91. * @return int
  92. */
  93. private function buildIndex(int $offset, int $stopAt):int {
  94. $startTime = $this->timeFactory->getTime();
  95. $query = $this->db->getQueryBuilder();
  96. $query->select('*')
  97. ->from('calendarobjects')
  98. ->where($query->expr()->lte('id', $query->createNamedParameter($stopAt)))
  99. ->andWhere($query->expr()->gt('id', $query->createNamedParameter($offset)))
  100. ->orderBy('id', 'ASC');
  101. $stmt = $query->execute();
  102. while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
  103. $offset = $row['id'];
  104. if (is_resource($row['calendardata'])) {
  105. $row['calendardata'] = stream_get_contents($row['calendardata']);
  106. }
  107. $row['component'] = $row['componenttype'];
  108. try {
  109. $this->reminderService->onTouchCalendarObject('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', $row);
  110. } catch (\Exception $ex) {
  111. $this->logger->logException($ex);
  112. }
  113. if (($this->timeFactory->getTime() - $startTime) > 15) {
  114. return $offset;
  115. }
  116. }
  117. return $stopAt;
  118. }
  119. }