You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ScanFiles.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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\DB\QueryBuilder\IQueryBuilder;
  27. use OCP\EventDispatcher\IEventDispatcher;
  28. use OCP\IConfig;
  29. use OCP\IDBConnection;
  30. use OCP\ILogger;
  31. use OCP\IUserManager;
  32. /**
  33. * Class ScanFiles is a background job used to run the file scanner over the user
  34. * accounts to ensure integrity of the file cache.
  35. *
  36. * @package OCA\Files\BackgroundJob
  37. */
  38. class ScanFiles extends \OC\BackgroundJob\TimedJob {
  39. /** @var IConfig */
  40. private $config;
  41. /** @var IUserManager */
  42. private $userManager;
  43. /** @var IEventDispatcher */
  44. private $dispatcher;
  45. /** @var ILogger */
  46. private $logger;
  47. private $connection;
  48. /** Amount of users that should get scanned per execution */
  49. public const USERS_PER_SESSION = 500;
  50. /**
  51. * @param IConfig $config
  52. * @param IUserManager $userManager
  53. * @param IEventDispatcher $dispatcher
  54. * @param ILogger $logger
  55. * @param IDBConnection $connection
  56. */
  57. public function __construct(
  58. IConfig $config,
  59. IUserManager $userManager,
  60. IEventDispatcher $dispatcher,
  61. ILogger $logger,
  62. IDBConnection $connection
  63. ) {
  64. // Run once per 10 minutes
  65. $this->setInterval(60 * 10);
  66. $this->config = $config;
  67. $this->userManager = $userManager;
  68. $this->dispatcher = $dispatcher;
  69. $this->logger = $logger;
  70. $this->connection = $connection;
  71. }
  72. /**
  73. * @param string $user
  74. */
  75. protected function runScanner(string $user) {
  76. try {
  77. $scanner = new Scanner(
  78. $user,
  79. null,
  80. $this->dispatcher,
  81. $this->logger
  82. );
  83. $scanner->backgroundScan('');
  84. } catch (\Exception $e) {
  85. $this->logger->logException($e, ['app' => 'files']);
  86. }
  87. \OC_Util::tearDownFS();
  88. }
  89. /**
  90. * Find all storages which have unindexed files and return a user for each
  91. *
  92. * @return string[]
  93. */
  94. private function getUsersToScan(): array {
  95. $query = $this->connection->getQueryBuilder();
  96. $query->select($query->func()->max('user_id'))
  97. ->from('filecache', 'f')
  98. ->innerJoin('f', 'mounts', 'm', $query->expr()->eq('storage_id', 'storage'))
  99. ->where($query->expr()->lt('size', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
  100. ->groupBy('storage_id')
  101. ->setMaxResults(self::USERS_PER_SESSION);
  102. return $query->execute()->fetchAll(\PDO::FETCH_COLUMN);
  103. }
  104. /**
  105. * @param $argument
  106. * @throws \Exception
  107. */
  108. protected function run($argument) {
  109. if ($this->config->getSystemValueBool('files_no_background_scan', false)) {
  110. return;
  111. }
  112. $users = $this->getUsersToScan();
  113. foreach ($users as $user) {
  114. $this->runScanner($user);
  115. }
  116. }
  117. }