BackgroundRepair.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\EventDispatcher;
  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 EventDispatcher */
  43. private $dispatcher;
  44. public function setDispatcher(EventDispatcher $dispatcher) {
  45. $this->dispatcher = $dispatcher;
  46. }
  47. /**
  48. * run the job, then remove it from the job list
  49. *
  50. * @param JobList $jobList
  51. * @param ILogger|null $logger
  52. */
  53. public function execute($jobList, ILogger $logger = null) {
  54. // add an interval of 15 mins
  55. $this->setInterval(15*60);
  56. $this->jobList = $jobList;
  57. $this->logger = $logger;
  58. parent::execute($jobList, $logger);
  59. }
  60. /**
  61. * @param array $argument
  62. * @throws \Exception
  63. * @throws \OC\NeedsUpdateException
  64. */
  65. protected function run($argument) {
  66. if (!isset($argument['app']) || !isset($argument['step'])) {
  67. // remove the job - we can never execute it
  68. $this->jobList->remove($this, $this->argument);
  69. return;
  70. }
  71. $app = $argument['app'];
  72. try {
  73. $this->loadApp($app);
  74. } catch (NeedsUpdateException $ex) {
  75. // as long as the app is not yet done with it's offline migration
  76. // we better not start with the live migration
  77. return;
  78. }
  79. $step = $argument['step'];
  80. $repair = new Repair([], $this->dispatcher);
  81. try {
  82. $repair->addStep($step);
  83. } catch (\Exception $ex) {
  84. $this->logger->logException($ex,[
  85. 'app' => 'migration'
  86. ]);
  87. // remove the job - we can never execute it
  88. $this->jobList->remove($this, $this->argument);
  89. return;
  90. }
  91. // execute the repair step
  92. $repair->run();
  93. // remove the job once executed successfully
  94. $this->jobList->remove($this, $this->argument);
  95. }
  96. /**
  97. * @codeCoverageIgnore
  98. * @param $app
  99. * @throws NeedsUpdateException
  100. */
  101. protected function loadApp($app) {
  102. OC_App::loadApp($app);
  103. }
  104. }