scanfiles.php 3.0 KB

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