Passed
Push — master ( 59402b...899021 )
by John
11:15 queued 10s
created

ScanFiles::fixDIForJobs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Lukas Reschke <[email protected]>
6
 * @author Robin Appelman <[email protected]>
7
 *
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program. If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OCA\Files\BackgroundJob;
25
26
use OC\Files\Utils\Scanner;
27
use OCP\EventDispatcher\IEventDispatcher;
28
use OCP\IConfig;
29
use OCP\ILogger;
30
use OCP\IUser;
31
use OCP\IUserManager;
32
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 \OC\BackgroundJob\TimedJob {
40
	/** @var IConfig */
41
	private $config;
42
	/** @var IUserManager */
43
	private $userManager;
44
	/** @var IEventDispatcher */
45
	private $dispatcher;
46
	/** @var ILogger */
47
	private $logger;
48
49
	/** Amount of users that should get scanned per execution */
50
	const USERS_PER_SESSION = 500;
51
52
	/**
53
	 * @param IConfig|null $config
54
	 * @param IUserManager|null $userManager
55
	 * @param IEventDispatcher|null $dispatcher
56
	 * @param ILogger|null $logger
57
	 */
58
	public function __construct(IConfig $config = null,
59
								IUserManager $userManager = null,
60
								IEventDispatcher $dispatcher = null,
61
								ILogger $logger = null) {
62
		// Run once per 10 minutes
63
		$this->setInterval(60 * 10);
64
65
		$this->config = $config ?? \OC::$server->getConfig();
66
		$this->userManager = $userManager ?? \OC::$server->getUserManager();
67
		$this->dispatcher = $dispatcher ?? \OC::$server->query(IEventDispatcher::class);
68
		$this->logger = $logger ?? \OC::$server->getLogger();
69
	}
70
71
	/**
72
	 * @param IUser $user
73
	 */
74
	protected function runScanner(IUser $user) {
75
		try {
76
			$scanner = new Scanner(
77
					$user->getUID(),
78
					null,
79
					$this->dispatcher,
80
					$this->logger
81
			);
82
			$scanner->backgroundScan('');
83
		} catch (\Exception $e) {
84
			$this->logger->logException($e, ['app' => 'files']);
85
		}
86
		\OC_Util::tearDownFS();
87
	}
88
89
	/**
90
	 * @param $argument
91
	 * @throws \Exception
92
	 */
93
	protected function run($argument) {
94
		if ($this->config->getSystemValueBool('files_no_background_scan', false)) {
95
			return;
96
		}
97
98
		$offset = $this->config->getAppValue('files', 'cronjob_scan_files', 0);
99
		$users = $this->userManager->search('', self::USERS_PER_SESSION, $offset);
0 ignored issues
show
Bug introduced by
$offset of type string is incompatible with the type integer expected by parameter $offset of OCP\IUserManager::search(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

99
		$users = $this->userManager->search('', self::USERS_PER_SESSION, /** @scrutinizer ignore-type */ $offset);
Loading history...
100
		if (!count($users)) {
101
			// No users found, reset offset and retry
102
			$offset = 0;
103
			$users = $this->userManager->search('', self::USERS_PER_SESSION);
104
		}
105
106
		$offset += self::USERS_PER_SESSION;
107
		$this->config->setAppValue('files', 'cronjob_scan_files', $offset);
108
109
		foreach ($users as $user) {
110
			$this->runScanner($user);
111
		}
112
	}
113
}
114