RegisterBuildReminderIndexBackgroundJob.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\Migration;
  8. use OCA\DAV\BackgroundJob\BuildReminderIndexBackgroundJob;
  9. use OCP\BackgroundJob\IJobList;
  10. use OCP\IConfig;
  11. use OCP\IDBConnection;
  12. use OCP\Migration\IOutput;
  13. use OCP\Migration\IRepairStep;
  14. /**
  15. * Class RegisterBuildReminderIndexBackgroundJob
  16. *
  17. * @package OCA\DAV\Migration
  18. */
  19. class RegisterBuildReminderIndexBackgroundJob implements IRepairStep {
  20. /** @var IDBConnection */
  21. private $db;
  22. /** @var IJobList */
  23. private $jobList;
  24. /** @var IConfig */
  25. private $config;
  26. /** @var string */
  27. private const CONFIG_KEY = 'buildCalendarReminderIndex';
  28. /**
  29. * @param IDBConnection $db
  30. * @param IJobList $jobList
  31. * @param IConfig $config
  32. */
  33. public function __construct(IDBConnection $db,
  34. IJobList $jobList,
  35. IConfig $config) {
  36. $this->db = $db;
  37. $this->jobList = $jobList;
  38. $this->config = $config;
  39. }
  40. /**
  41. * @return string
  42. */
  43. public function getName() {
  44. return 'Registering building of calendar reminder index as background job';
  45. }
  46. /**
  47. * @param IOutput $output
  48. */
  49. public function run(IOutput $output) {
  50. // only run once
  51. if ($this->config->getAppValue('dav', self::CONFIG_KEY) === 'yes') {
  52. $output->info('Repair step already executed');
  53. return;
  54. }
  55. $query = $this->db->getQueryBuilder();
  56. $query->select($query->createFunction('MAX(' . $query->getColumnName('id') . ')'))
  57. ->from('calendarobjects');
  58. $result = $query->execute();
  59. $maxId = (int)$result->fetchOne();
  60. $result->closeCursor();
  61. $output->info('Add background job');
  62. $this->jobList->add(BuildReminderIndexBackgroundJob::class, [
  63. 'offset' => 0,
  64. 'stopAt' => $maxId
  65. ]);
  66. // if all were done, no need to redo the repair during next upgrade
  67. $this->config->setAppValue('dav', self::CONFIG_KEY, 'yes');
  68. }
  69. }