Passed
Push — master ( 270287...88eebf )
by Julius
11:22 queued 11s
created

ScanFiles::run()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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