1
0

BackgroundRepair.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Migration;
  8. use OC\NeedsUpdateException;
  9. use OC\Repair;
  10. use OC_App;
  11. use OCP\AppFramework\Utility\ITimeFactory;
  12. use OCP\BackgroundJob\IJobList;
  13. use OCP\BackgroundJob\TimedJob;
  14. use Psr\Log\LoggerInterface;
  15. /**
  16. * Class BackgroundRepair
  17. *
  18. * @package OC\Migration
  19. */
  20. class BackgroundRepair extends TimedJob {
  21. public function __construct(
  22. private Repair $repair,
  23. ITimeFactory $time,
  24. private LoggerInterface $logger,
  25. private IJobList $jobList,
  26. ) {
  27. parent::__construct($time);
  28. $this->setInterval(15 * 60);
  29. }
  30. /**
  31. * @param array $argument
  32. * @throws \Exception
  33. * @throws \OC\NeedsUpdateException
  34. */
  35. protected function run($argument): void {
  36. if (!isset($argument['app']) || !isset($argument['step'])) {
  37. // remove the job - we can never execute it
  38. $this->jobList->remove($this, $this->argument);
  39. return;
  40. }
  41. $app = $argument['app'];
  42. try {
  43. $this->loadApp($app);
  44. } catch (NeedsUpdateException $ex) {
  45. // as long as the app is not yet done with it's offline migration
  46. // we better not start with the live migration
  47. return;
  48. }
  49. $step = $argument['step'];
  50. $this->repair->setRepairSteps([]);
  51. try {
  52. $this->repair->addStep($step);
  53. } catch (\Exception $ex) {
  54. $this->logger->error($ex->getMessage(), [
  55. 'app' => 'migration',
  56. 'exception' => $ex,
  57. ]);
  58. // remove the job - we can never execute it
  59. $this->jobList->remove($this, $this->argument);
  60. return;
  61. }
  62. // execute the repair step
  63. $this->repair->run();
  64. // remove the job once executed successfully
  65. $this->jobList->remove($this, $this->argument);
  66. }
  67. /**
  68. * @codeCoverageIgnore
  69. * @param $app
  70. * @throws NeedsUpdateException
  71. */
  72. protected function loadApp($app): void {
  73. OC_App::loadApp($app);
  74. }
  75. }