BackgroundRepair.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. * @author Thomas Müller <thomas.mueller@tmit.eu>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Migration;
  24. use OC\BackgroundJob\JobList;
  25. use OC\BackgroundJob\TimedJob;
  26. use OC\NeedsUpdateException;
  27. use OC\Repair;
  28. use OC_App;
  29. use OCP\BackgroundJob\IJobList;
  30. use OCP\ILogger;
  31. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  32. /**
  33. * Class BackgroundRepair
  34. *
  35. * @package OC\Migration
  36. */
  37. class BackgroundRepair extends TimedJob {
  38. /** @var IJobList */
  39. private $jobList;
  40. /** @var ILogger */
  41. private $logger;
  42. /** @var EventDispatcherInterface */
  43. private $dispatcher;
  44. /**
  45. * @param EventDispatcherInterface $dispatcher
  46. */
  47. public function setDispatcher(EventDispatcherInterface $dispatcher): void {
  48. $this->dispatcher = $dispatcher;
  49. }
  50. /**
  51. * run the job, then remove it from the job list
  52. *
  53. * @param JobList $jobList
  54. * @param ILogger|null $logger
  55. */
  56. public function execute($jobList, ILogger $logger = null) {
  57. // add an interval of 15 mins
  58. $this->setInterval(15*60);
  59. $this->jobList = $jobList;
  60. $this->logger = $logger;
  61. parent::execute($jobList, $logger);
  62. }
  63. /**
  64. * @param array $argument
  65. * @throws \Exception
  66. * @throws \OC\NeedsUpdateException
  67. */
  68. protected function run($argument) {
  69. if (!isset($argument['app']) || !isset($argument['step'])) {
  70. // remove the job - we can never execute it
  71. $this->jobList->remove($this, $this->argument);
  72. return;
  73. }
  74. $app = $argument['app'];
  75. try {
  76. $this->loadApp($app);
  77. } catch (NeedsUpdateException $ex) {
  78. // as long as the app is not yet done with it's offline migration
  79. // we better not start with the live migration
  80. return;
  81. }
  82. $step = $argument['step'];
  83. $repair = new Repair([], $this->dispatcher);
  84. try {
  85. $repair->addStep($step);
  86. } catch (\Exception $ex) {
  87. $this->logger->logException($ex,[
  88. 'app' => 'migration'
  89. ]);
  90. // remove the job - we can never execute it
  91. $this->jobList->remove($this, $this->argument);
  92. return;
  93. }
  94. // execute the repair step
  95. $repair->run();
  96. // remove the job once executed successfully
  97. $this->jobList->remove($this, $this->argument);
  98. }
  99. /**
  100. * @codeCoverageIgnore
  101. * @param $app
  102. * @throws NeedsUpdateException
  103. */
  104. protected function loadApp($app) {
  105. OC_App::loadApp($app);
  106. }
  107. }