GenerateMetadataJob.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\IConfig;
  16. use OCP\IUserManager;
  17. use Psr\Log\LoggerInterface;
  18. class GenerateMetadataJob extends TimedJob {
  19. public function __construct(
  20. ITimeFactory $time,
  21. private IConfig $config,
  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. $users = $this->userManager->search('');
  34. $lastHandledUser = $this->config->getAppValue('core', 'metadataGenerationLastHandledUser', '');
  35. // we'll only start timer once we have found a valid user to handle
  36. // meaning NOW if we have not handled any user from a previous run
  37. $startTime = ($lastHandledUser === '') ? time() : null;
  38. foreach ($users as $user) {
  39. $userId = $user->getUID();
  40. // if we already handled a previous run, we start timer only when we face the last handled user
  41. if ($startTime === null) {
  42. if ($userId === $lastHandledUser) {
  43. $startTime = time();
  44. }
  45. continue;
  46. }
  47. $this->config->setAppValue('core', 'metadataGenerationLastHandledUser', $userId);
  48. $this->scanFilesForUser($user->getUID());
  49. // Stop if execution time is more than one hour.
  50. if (time() - $startTime > 3600) {
  51. return;
  52. }
  53. }
  54. $this->jobList->remove(GenerateMetadataJob::class);
  55. $this->config->deleteAppValue('core', 'metadataGenerationLastHandledUser');
  56. }
  57. private function scanFilesForUser(string $userId): void {
  58. $userFolder = $this->rootFolder->getUserFolder($userId);
  59. $this->scanFolder($userFolder);
  60. }
  61. private function scanFolder(Folder $folder): void {
  62. // Do not scan share and other moveable mounts.
  63. if ($folder->getMountPoint() instanceof \OC\Files\Mount\MoveableMount) {
  64. return;
  65. }
  66. foreach ($folder->getDirectoryListing() as $node) {
  67. if ($node instanceof Folder) {
  68. $this->scanFolder($node);
  69. continue;
  70. }
  71. try {
  72. $this->filesMetadataManager->getMetadata($node->getId(), false);
  73. } catch (FilesMetadataNotFoundException) {
  74. try {
  75. $this->filesMetadataManager->refreshMetadata(
  76. $node,
  77. IFilesMetadataManager::PROCESS_LIVE | IFilesMetadataManager::PROCESS_BACKGROUND
  78. );
  79. } catch (\Throwable $ex) {
  80. $this->logger->warning("Error while generating metadata for fileid " . $node->getId(), ['exception' => $ex]);
  81. }
  82. }
  83. }
  84. }
  85. }