ScanFiles.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OCA\Files\BackgroundJob;
  23. use OC\Files\Utils\Scanner;
  24. use OCP\IConfig;
  25. use OCP\IDBConnection;
  26. use OCP\ILogger;
  27. use OCP\IUser;
  28. use OCP\IUserManager;
  29. /**
  30. * Class ScanFiles is a background job used to run the file scanner over the user
  31. * accounts to ensure integrity of the file cache.
  32. *
  33. * @package OCA\Files\BackgroundJob
  34. */
  35. class ScanFiles extends \OC\BackgroundJob\TimedJob {
  36. /** @var IConfig */
  37. private $config;
  38. /** @var IUserManager */
  39. private $userManager;
  40. /** @var IDBConnection */
  41. private $dbConnection;
  42. /** @var ILogger */
  43. private $logger;
  44. /** Amount of users that should get scanned per execution */
  45. const USERS_PER_SESSION = 500;
  46. /**
  47. * @param IConfig|null $config
  48. * @param IUserManager|null $userManager
  49. * @param IDBConnection|null $dbConnection
  50. * @param ILogger|null $logger
  51. */
  52. public function __construct(IConfig $config = null,
  53. IUserManager $userManager = null,
  54. IDBConnection $dbConnection = null,
  55. ILogger $logger = null) {
  56. // Run once per 10 minutes
  57. $this->setInterval(60 * 10);
  58. if (is_null($userManager) || is_null($config)) {
  59. $this->fixDIForJobs();
  60. } else {
  61. $this->config = $config;
  62. $this->userManager = $userManager;
  63. $this->logger = $logger;
  64. }
  65. }
  66. protected function fixDIForJobs() {
  67. $this->config = \OC::$server->getConfig();
  68. $this->userManager = \OC::$server->getUserManager();
  69. $this->logger = \OC::$server->getLogger();
  70. }
  71. /**
  72. * @param IUser $user
  73. */
  74. protected function runScanner(IUser $user) {
  75. try {
  76. $scanner = new Scanner(
  77. $user->getUID(),
  78. $this->dbConnection,
  79. $this->logger
  80. );
  81. $scanner->backgroundScan('');
  82. } catch (\Exception $e) {
  83. $this->logger->logException($e, ['app' => 'files']);
  84. }
  85. \OC_Util::tearDownFS();
  86. }
  87. /**
  88. * @param $argument
  89. * @throws \Exception
  90. */
  91. protected function run($argument) {
  92. $offset = $this->config->getAppValue('files', 'cronjob_scan_files', 0);
  93. $users = $this->userManager->search('', self::USERS_PER_SESSION, $offset);
  94. if (!count($users)) {
  95. // No users found, reset offset and retry
  96. $offset = 0;
  97. $users = $this->userManager->search('', self::USERS_PER_SESSION);
  98. }
  99. $offset += self::USERS_PER_SESSION;
  100. $this->config->setAppValue('files', 'cronjob_scan_files', $offset);
  101. foreach ($users as $user) {
  102. $this->runScanner($user);
  103. }
  104. }
  105. }