You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.0 KiB

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