ScanFiles.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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\EventDispatcher\IEventDispatcher;
  27. use OCP\IConfig;
  28. use OCP\ILogger;
  29. use OCP\IUser;
  30. use OCP\IUserManager;
  31. /**
  32. * Class ScanFiles is a background job used to run the file scanner over the user
  33. * accounts to ensure integrity of the file cache.
  34. *
  35. * @package OCA\Files\BackgroundJob
  36. */
  37. class ScanFiles extends \OC\BackgroundJob\TimedJob {
  38. /** @var IConfig */
  39. private $config;
  40. /** @var IUserManager */
  41. private $userManager;
  42. /** @var IEventDispatcher */
  43. private $dispatcher;
  44. /** @var ILogger */
  45. private $logger;
  46. /** Amount of users that should get scanned per execution */
  47. const USERS_PER_SESSION = 500;
  48. /**
  49. * @param IConfig|null $config
  50. * @param IUserManager|null $userManager
  51. * @param IEventDispatcher|null $dispatcher
  52. * @param ILogger|null $logger
  53. */
  54. public function __construct(IConfig $config = null,
  55. IUserManager $userManager = null,
  56. IEventDispatcher $dispatcher = null,
  57. ILogger $logger = null) {
  58. // Run once per 10 minutes
  59. $this->setInterval(60 * 10);
  60. $this->config = $config ?? \OC::$server->getConfig();
  61. $this->userManager = $userManager ?? \OC::$server->getUserManager();
  62. $this->dispatcher = $dispatcher ?? \OC::$server->query(IEventDispatcher::class);
  63. $this->logger = $logger ?? \OC::$server->getLogger();
  64. }
  65. /**
  66. * @param IUser $user
  67. */
  68. protected function runScanner(IUser $user) {
  69. try {
  70. $scanner = new Scanner(
  71. $user->getUID(),
  72. null,
  73. $this->dispatcher,
  74. $this->logger
  75. );
  76. $scanner->backgroundScan('');
  77. } catch (\Exception $e) {
  78. $this->logger->logException($e, ['app' => 'files']);
  79. }
  80. \OC_Util::tearDownFS();
  81. }
  82. /**
  83. * @param $argument
  84. * @throws \Exception
  85. */
  86. protected function run($argument) {
  87. if ($this->config->getSystemValueBool('files_no_background_scan', false)) {
  88. return;
  89. }
  90. $offset = $this->config->getAppValue('files', 'cronjob_scan_files', 0);
  91. $users = $this->userManager->search('', self::USERS_PER_SESSION, $offset);
  92. if (!count($users)) {
  93. // No users found, reset offset and retry
  94. $offset = 0;
  95. $users = $this->userManager->search('', self::USERS_PER_SESSION);
  96. }
  97. $offset += self::USERS_PER_SESSION;
  98. $this->config->setAppValue('files', 'cronjob_scan_files', $offset);
  99. foreach ($users as $user) {
  100. $this->runScanner($user);
  101. }
  102. }
  103. }