1
0

GenerateMetadataJob.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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\IConfig;
  17. use OCP\IUserManager;
  18. use Psr\Log\LoggerInterface;
  19. class GenerateMetadataJob extends TimedJob {
  20. // Default file size limit for metadata generation (MBytes).
  21. protected const DEFAULT_MAX_FILESIZE = 256;
  22. public function __construct(
  23. ITimeFactory $time,
  24. private IConfig $config,
  25. private IAppConfig $appConfig,
  26. private IRootFolder $rootFolder,
  27. private IUserManager $userManager,
  28. private IFilesMetadataManager $filesMetadataManager,
  29. private IJobList $jobList,
  30. private LoggerInterface $logger,
  31. ) {
  32. parent::__construct($time);
  33. $this->setTimeSensitivity(self::TIME_INSENSITIVE);
  34. $this->setInterval(24 * 60 * 60);
  35. }
  36. protected function run(mixed $argument): void {
  37. if ($this->appConfig->getValueBool('core', 'metadataGenerationDone', false)) {
  38. return;
  39. }
  40. $lastHandledUser = $this->appConfig->getValueString('core', 'metadataGenerationLastHandledUser', '');
  41. $users = $this->userManager->search('');
  42. // we'll only start timer once we have found a valid user to handle
  43. // meaning NOW if we have not handled any user from a previous run
  44. $startTime = ($lastHandledUser === '') ? time() : null;
  45. foreach ($users as $user) {
  46. $userId = $user->getUID();
  47. // if we already handled a previous run, we start timer only when we face the last handled user
  48. if ($startTime === null) {
  49. if ($userId === $lastHandledUser) {
  50. $startTime = time();
  51. }
  52. continue;
  53. }
  54. $this->appConfig->setValueString('core', 'metadataGenerationLastHandledUser', $userId);
  55. $this->scanFilesForUser($user->getUID());
  56. // Stop if execution time is more than one hour.
  57. if (time() - $startTime > 3600) {
  58. return;
  59. }
  60. }
  61. $this->appConfig->deleteKey('core', 'metadataGenerationLastHandledUser');
  62. $this->appConfig->setValueBool('core', 'metadataGenerationDone', true);
  63. }
  64. private function scanFilesForUser(string $userId): void {
  65. $userFolder = $this->rootFolder->getUserFolder($userId);
  66. $this->scanFolder($userFolder);
  67. }
  68. private function scanFolder(Folder $folder): void {
  69. // Do not scan share and other moveable mounts.
  70. if ($folder->getMountPoint() instanceof \OC\Files\Mount\MoveableMount) {
  71. return;
  72. }
  73. foreach ($folder->getDirectoryListing() as $node) {
  74. if ($node instanceof Folder) {
  75. $this->scanFolder($node);
  76. continue;
  77. }
  78. // Don't generate metadata for files bigger than configured metadata_max_filesize
  79. // Files are loaded in memory so very big files can lead to an OOM on the server
  80. $nodeSize = $node->getSize();
  81. $nodeLimit = $this->config->getSystemValueInt('metadata_max_filesize', self::DEFAULT_MAX_FILESIZE);
  82. if ($nodeSize > $nodeLimit * 1000000) {
  83. $this->logger->debug('Skipping generating metadata for fileid ' . $node->getId() . " as its size exceeds configured 'metadata_max_filesize'.");
  84. continue;
  85. }
  86. try {
  87. $this->filesMetadataManager->getMetadata($node->getId(), false);
  88. } catch (FilesMetadataNotFoundException) {
  89. try {
  90. $this->filesMetadataManager->refreshMetadata(
  91. $node,
  92. IFilesMetadataManager::PROCESS_LIVE | IFilesMetadataManager::PROCESS_BACKGROUND
  93. );
  94. } catch (\Throwable $ex) {
  95. $this->logger->warning('Error while generating metadata for fileid ' . $node->getId(), ['exception' => $ex]);
  96. }
  97. }
  98. }
  99. }
  100. }