1
0

BuildReminderIndexBackgroundJob.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 Psr\Log\LoggerInterface;
  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. private LoggerInterface $logger;
  44. /** @var IJobList */
  45. private $jobList;
  46. /** @var ITimeFactory */
  47. private $timeFactory;
  48. /**
  49. * BuildReminderIndexBackgroundJob constructor.
  50. */
  51. public function __construct(IDBConnection $db,
  52. ReminderService $reminderService,
  53. LoggerInterface $logger,
  54. IJobList $jobList,
  55. ITimeFactory $timeFactory) {
  56. parent::__construct($timeFactory);
  57. $this->db = $db;
  58. $this->reminderService = $reminderService;
  59. $this->logger = $logger;
  60. $this->jobList = $jobList;
  61. $this->timeFactory = $timeFactory;
  62. }
  63. public function run($argument) {
  64. $offset = (int) $argument['offset'];
  65. $stopAt = (int) $argument['stopAt'];
  66. $this->logger->info('Building calendar reminder index (' . $offset .'/' . $stopAt . ')');
  67. $offset = $this->buildIndex($offset, $stopAt);
  68. if ($offset >= $stopAt) {
  69. $this->logger->info('Building calendar reminder index done');
  70. } else {
  71. $this->jobList->add(self::class, [
  72. 'offset' => $offset,
  73. 'stopAt' => $stopAt
  74. ]);
  75. $this->logger->info('Scheduled a new BuildReminderIndexBackgroundJob with offset ' . $offset);
  76. }
  77. }
  78. /**
  79. * @param int $offset
  80. * @param int $stopAt
  81. * @return int
  82. */
  83. private function buildIndex(int $offset, int $stopAt):int {
  84. $startTime = $this->timeFactory->getTime();
  85. $query = $this->db->getQueryBuilder();
  86. $query->select('*')
  87. ->from('calendarobjects')
  88. ->where($query->expr()->lte('id', $query->createNamedParameter($stopAt)))
  89. ->andWhere($query->expr()->gt('id', $query->createNamedParameter($offset)))
  90. ->orderBy('id', 'ASC');
  91. $result = $query->executeQuery();
  92. while ($row = $result->fetch(\PDO::FETCH_ASSOC)) {
  93. $offset = (int) $row['id'];
  94. if (is_resource($row['calendardata'])) {
  95. $row['calendardata'] = stream_get_contents($row['calendardata']);
  96. }
  97. $row['component'] = $row['componenttype'];
  98. try {
  99. $this->reminderService->onCalendarObjectCreate($row);
  100. } catch (\Exception $ex) {
  101. $this->logger->error($ex->getMessage(), ['exception' => $ex]);
  102. }
  103. if (($this->timeFactory->getTime() - $startTime) > 15) {
  104. $result->closeCursor();
  105. return $offset;
  106. }
  107. }
  108. $result->closeCursor();
  109. return $stopAt;
  110. }
  111. }