RegisterBuildReminderIndexBackgroundJob.php 2.7 KB

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