BackgroundRepair.php 2.9 KB

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