RegisterBuildReminderIndexBackgroundJob.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\DAV\Migration;
  26. use OCA\DAV\BackgroundJob\BuildReminderIndexBackgroundJob;
  27. use OCP\BackgroundJob\IJobList;
  28. use OCP\IConfig;
  29. use OCP\IDBConnection;
  30. use OCP\Migration\IOutput;
  31. use OCP\Migration\IRepairStep;
  32. /**
  33. * Class RegisterBuildReminderIndexBackgroundJob
  34. *
  35. * @package OCA\DAV\Migration
  36. */
  37. class RegisterBuildReminderIndexBackgroundJob implements IRepairStep {
  38. /** @var IDBConnection */
  39. private $db;
  40. /** @var IJobList */
  41. private $jobList;
  42. /** @var IConfig */
  43. private $config;
  44. /** @var string */
  45. private const CONFIG_KEY = 'buildCalendarReminderIndex';
  46. /**
  47. * @param IDBConnection $db
  48. * @param IJobList $jobList
  49. * @param IConfig $config
  50. */
  51. public function __construct(IDBConnection $db,
  52. IJobList $jobList,
  53. IConfig $config) {
  54. $this->db = $db;
  55. $this->jobList = $jobList;
  56. $this->config = $config;
  57. }
  58. /**
  59. * @return string
  60. */
  61. public function getName() {
  62. return 'Registering building of calendar reminder index as background job';
  63. }
  64. /**
  65. * @param IOutput $output
  66. */
  67. public function run(IOutput $output) {
  68. // only run once
  69. if ($this->config->getAppValue('dav', self::CONFIG_KEY) === 'yes') {
  70. $output->info('Repair step already executed');
  71. return;
  72. }
  73. $query = $this->db->getQueryBuilder();
  74. $query->select($query->createFunction('MAX(' . $query->getColumnName('id') . ')'))
  75. ->from('calendarobjects');
  76. $maxId = (int)$query->execute()->fetchColumn();
  77. $output->info('Add background job');
  78. $this->jobList->add(BuildReminderIndexBackgroundJob::class, [
  79. 'offset' => 0,
  80. 'stopAt' => $maxId
  81. ]);
  82. // if all were done, no need to redo the repair during next upgrade
  83. $this->config->setAppValue('dav', self::CONFIG_KEY, 'yes');
  84. }
  85. }