1
0

GenerateMetadataJob.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Louis Chemineau <louis@chmn.me>
  5. *
  6. * @author Louis Chemineau <louis@chmn.me>
  7. *
  8. * @license AGPL-3.0-or-later
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Core\BackgroundJobs;
  25. use OCP\AppFramework\Utility\ITimeFactory;
  26. use OCP\BackgroundJob\IJobList;
  27. use OCP\BackgroundJob\TimedJob;
  28. use OCP\Files\Folder;
  29. use OCP\Files\IRootFolder;
  30. use OCP\FilesMetadata\Exceptions\FilesMetadataNotFoundException;
  31. use OCP\FilesMetadata\IFilesMetadataManager;
  32. use OCP\IConfig;
  33. use OCP\IUserManager;
  34. use Psr\Log\LoggerInterface;
  35. class GenerateMetadataJob extends TimedJob {
  36. public function __construct(
  37. ITimeFactory $time,
  38. private IConfig $config,
  39. private IRootFolder $rootFolder,
  40. private IUserManager $userManager,
  41. private IFilesMetadataManager $filesMetadataManager,
  42. private IJobList $jobList,
  43. private LoggerInterface $logger,
  44. ) {
  45. parent::__construct($time);
  46. $this->setTimeSensitivity(\OCP\BackgroundJob\IJob::TIME_INSENSITIVE);
  47. $this->setInterval(24 * 3600);
  48. }
  49. protected function run(mixed $argument): void {
  50. $users = $this->userManager->search('');
  51. $lastHandledUser = $this->config->getAppValue('core', 'metadataGenerationLastHandledUser', '');
  52. // we'll only start timer once we have found a valid user to handle
  53. // meaning NOW if we have not handled any user from a previous run
  54. $startTime = ($lastHandledUser === '') ? time() : null;
  55. foreach ($users as $user) {
  56. $userId = $user->getUID();
  57. // if we already handled a previous run, we start timer only when we face the last handled user
  58. if ($startTime === null) {
  59. if ($userId === $lastHandledUser) {
  60. $startTime = time();
  61. }
  62. continue;
  63. }
  64. $this->config->setAppValue('core', 'metadataGenerationLastHandledUser', $userId);
  65. $this->scanFilesForUser($user->getUID());
  66. // Stop if execution time is more than one hour.
  67. if (time() - $startTime > 3600) {
  68. return;
  69. }
  70. }
  71. $this->jobList->remove(GenerateMetadataJob::class);
  72. $this->config->deleteAppValue('core', 'metadataGenerationLastHandledUser');
  73. }
  74. private function scanFilesForUser(string $userId): void {
  75. $userFolder = $this->rootFolder->getUserFolder($userId);
  76. $this->scanFolder($userFolder);
  77. }
  78. private function scanFolder(Folder $folder): void {
  79. // Do not scan share and other moveable mounts.
  80. if ($folder->getMountPoint() instanceof \OC\Files\Mount\MoveableMount) {
  81. return;
  82. }
  83. foreach ($folder->getDirectoryListing() as $node) {
  84. if ($node instanceof Folder) {
  85. $this->scanFolder($node);
  86. continue;
  87. }
  88. try {
  89. $this->filesMetadataManager->getMetadata($node->getId(), false);
  90. } catch (FilesMetadataNotFoundException) {
  91. try {
  92. $this->filesMetadataManager->refreshMetadata(
  93. $node,
  94. IFilesMetadataManager::PROCESS_LIVE | IFilesMetadataManager::PROCESS_BACKGROUND
  95. );
  96. } catch (\Throwable $ex) {
  97. $this->logger->warning("Error while generating metadata for fileid " . $node->getId(), ['exception' => $ex]);
  98. }
  99. }
  100. }
  101. }
  102. }