GenerateMetadataJob.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\BackgroundJobs;
  8. use OCP\AppFramework\Utility\ITimeFactory;
  9. use OCP\BackgroundJob\IJobList;
  10. use OCP\BackgroundJob\TimedJob;
  11. use OCP\Files\Folder;
  12. use OCP\Files\IRootFolder;
  13. use OCP\FilesMetadata\Exceptions\FilesMetadataNotFoundException;
  14. use OCP\FilesMetadata\IFilesMetadataManager;
  15. use OCP\IAppConfig;
  16. use OCP\IUserManager;
  17. use Psr\Log\LoggerInterface;
  18. class GenerateMetadataJob extends TimedJob {
  19. public function __construct(
  20. ITimeFactory $time,
  21. private IAppConfig $appConfig,
  22. private IRootFolder $rootFolder,
  23. private IUserManager $userManager,
  24. private IFilesMetadataManager $filesMetadataManager,
  25. private IJobList $jobList,
  26. private LoggerInterface $logger,
  27. ) {
  28. parent::__construct($time);
  29. $this->setTimeSensitivity(\OCP\BackgroundJob\IJob::TIME_INSENSITIVE);
  30. $this->setInterval(24 * 3600);
  31. }
  32. protected function run(mixed $argument): void {
  33. if ($this->appConfig->getValueBool('core', 'metadataGenerationDone', false)) {
  34. return;
  35. }
  36. $lastHandledUser = $this->appConfig->getValueString('core', 'metadataGenerationLastHandledUser', '');
  37. $users = $this->userManager->search('');
  38. // we'll only start timer once we have found a valid user to handle
  39. // meaning NOW if we have not handled any user from a previous run
  40. $startTime = ($lastHandledUser === '') ? time() : null;
  41. foreach ($users as $user) {
  42. $userId = $user->getUID();
  43. // if we already handled a previous run, we start timer only when we face the last handled user
  44. if ($startTime === null) {
  45. if ($userId === $lastHandledUser) {
  46. $startTime = time();
  47. }
  48. continue;
  49. }
  50. $this->appConfig->setValueString('core', 'metadataGenerationLastHandledUser', $userId);
  51. $this->scanFilesForUser($user->getUID());
  52. // Stop if execution time is more than one hour.
  53. if (time() - $startTime > 3600) {
  54. return;
  55. }
  56. }
  57. $this->appConfig->deleteKey('core', 'metadataGenerationLastHandledUser');
  58. $this->appConfig->setValueBool('core', 'metadataGenerationDone', true);
  59. }
  60. private function scanFilesForUser(string $userId): void {
  61. $userFolder = $this->rootFolder->getUserFolder($userId);
  62. $this->scanFolder($userFolder);
  63. }
  64. private function scanFolder(Folder $folder): void {
  65. // Do not scan share and other moveable mounts.
  66. if ($folder->getMountPoint() instanceof \OC\Files\Mount\MoveableMount) {
  67. return;
  68. }
  69. foreach ($folder->getDirectoryListing() as $node) {
  70. if ($node instanceof Folder) {
  71. $this->scanFolder($node);
  72. continue;
  73. }
  74. try {
  75. $this->filesMetadataManager->getMetadata($node->getId(), false);
  76. } catch (FilesMetadataNotFoundException) {
  77. try {
  78. $this->filesMetadataManager->refreshMetadata(
  79. $node,
  80. IFilesMetadataManager::PROCESS_LIVE | IFilesMetadataManager::PROCESS_BACKGROUND
  81. );
  82. } catch (\Throwable $ex) {
  83. $this->logger->warning("Error while generating metadata for fileid " . $node->getId(), ['exception' => $ex]);
  84. }
  85. }
  86. }
  87. }
  88. }