ScanFiles.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  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, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Files\BackgroundJob;
  25. use OC\Files\Utils\Scanner;
  26. use OCP\AppFramework\Utility\ITimeFactory;
  27. use OCP\BackgroundJob\TimedJob;
  28. use OCP\DB\QueryBuilder\IQueryBuilder;
  29. use OCP\EventDispatcher\IEventDispatcher;
  30. use OCP\IConfig;
  31. use OCP\IDBConnection;
  32. use Psr\Log\LoggerInterface;
  33. /**
  34. * Class ScanFiles is a background job used to run the file scanner over the user
  35. * accounts to ensure integrity of the file cache.
  36. *
  37. * @package OCA\Files\BackgroundJob
  38. */
  39. class ScanFiles extends TimedJob {
  40. private IConfig $config;
  41. private IEventDispatcher $dispatcher;
  42. private LoggerInterface $logger;
  43. private IDBConnection $connection;
  44. /** Amount of users that should get scanned per execution */
  45. public const USERS_PER_SESSION = 500;
  46. public function __construct(
  47. IConfig $config,
  48. IEventDispatcher $dispatcher,
  49. LoggerInterface $logger,
  50. IDBConnection $connection,
  51. ITimeFactory $time
  52. ) {
  53. parent::__construct($time);
  54. // Run once per 10 minutes
  55. $this->setInterval(60 * 10);
  56. $this->config = $config;
  57. $this->dispatcher = $dispatcher;
  58. $this->logger = $logger;
  59. $this->connection = $connection;
  60. }
  61. protected function runScanner(string $user): void {
  62. try {
  63. $scanner = new Scanner(
  64. $user,
  65. null,
  66. $this->dispatcher,
  67. $this->logger
  68. );
  69. $scanner->backgroundScan('');
  70. } catch (\Exception $e) {
  71. $this->logger->error($e->getMessage(), ['exception' => $e, 'app' => 'files']);
  72. }
  73. \OC_Util::tearDownFS();
  74. }
  75. /**
  76. * Find a storage which have unindexed files and return a user with access to the storage
  77. *
  78. * @return string|false
  79. */
  80. private function getUserToScan() {
  81. $query = $this->connection->getQueryBuilder();
  82. $query->select('user_id')
  83. ->from('filecache', 'f')
  84. ->innerJoin('f', 'mounts', 'm', $query->expr()->eq('storage_id', 'storage'))
  85. ->where($query->expr()->lt('size', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
  86. ->andWhere($query->expr()->gt('parent', $query->createNamedParameter(-1, IQueryBuilder::PARAM_INT)))
  87. ->setMaxResults(1);
  88. return $query->executeQuery()->fetchOne();
  89. }
  90. /**
  91. * @param $argument
  92. * @throws \Exception
  93. */
  94. protected function run($argument) {
  95. if ($this->config->getSystemValueBool('files_no_background_scan', false)) {
  96. return;
  97. }
  98. $usersScanned = 0;
  99. $lastUser = '';
  100. $user = $this->getUserToScan();
  101. while ($user && $usersScanned < self::USERS_PER_SESSION && $lastUser !== $user) {
  102. $this->runScanner($user);
  103. $lastUser = $user;
  104. $user = $this->getUserToScan();
  105. $usersScanned += 1;
  106. }
  107. if ($lastUser === $user) {
  108. $this->logger->warning("User $user still has unscanned files after running background scan, background scan might be stopped prematurely");
  109. }
  110. }
  111. }