BackgroundRepair.php 2.8 KB

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