Completed
Push — master ( 0622ca...d10f64 )
by Branko
13s queued 12s
created

CheckRequirementsTask::execute()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 52
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 34
nc 8
nop 1
dl 0
loc 52
ccs 0
cts 40
cp 0
crap 42
rs 8.7537
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2017, Matias De lellis <[email protected]>
4
 * @copyright Copyright (c) 2018, Branko Kokanovic <[email protected]>
5
 *
6
 * @author Branko Kokanovic <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
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
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
namespace OCA\FaceRecognition\BackgroundJob\Tasks;
25
26
use OCP\IConfig;
27
28
use OCA\FaceRecognition\BackgroundJob\FaceRecognitionBackgroundTask;
29
use OCA\FaceRecognition\BackgroundJob\FaceRecognitionContext;
30
use OCA\FaceRecognition\Helper\MemoryLimits;
31
use OCA\FaceRecognition\Helper\Requirements;
32
use OCA\FaceRecognition\Migration\AddDefaultFaceModel;
33
34
/**
35
 * Check all requirements before we start engaging in lengthy background task.
36
 */
37
class CheckRequirementsTask extends FaceRecognitionBackgroundTask {
38
	/** @var IConfig Config */
39
	private $config;
40
41
	/**
42
	 * @param IConfig $config Config
43
	 */
44
	public function __construct(IConfig $config) {
45
		parent::__construct();
46
		$this->config = $config;
47
	}
48
49
	/**
50
	 * @inheritdoc
51
	 */
52
	public function description() {
53
		return "Check all requirements";
54
	}
55
56
	/**
57
	 * @inheritdoc
58
	 */
59
	public function execute(FaceRecognitionContext $context) {
60
		$this->setContext($context);
61
		$model = intval($this->config->getAppValue('facerecognition', 'model', AddDefaultFaceModel::DEFAULT_FACE_MODEL_ID));
62
63
		$req = new Requirements($context->appManager, $model);
64
65
		if (!$req->pdlibLoaded()) {
66
			$error_message = "PDLib is not loaded. Cannot continue";
67
			$this->logInfo($error_message);
68
			return false;
69
		}
70
71
		if (!$req->modelFilesPresent()) {
72
			$error_message =
73
				"Files of model with ID ' . $model . ' are not present in models/ directory.\n" .
74
				"Please contact administrator to change models you are using for face recognition\n" .
75
				"or reinstall application. File an issue here if that doesn\'t help: https://github.com/matiasdelellis/facerecognition/issues";
76
			$this->logInfo($error_message);
77
			return false;
78
		}
79
80
		$memory = MemoryLimits::getAvailableMemory();
81
		if ($memory <= 0) {
82
			// We cannot determine amount of memory to give to face recognition CNN.
83
			// We will hardcode it here to 1GB, but plan is to expose this to user in future.
84
			// TODO: allow user to choose "recognition quality", which will map to given memory.
85
			// If user explicitely set something, we ignore getting memory from system.
86
			$this->logDebug("Cannot detect amount of memory given to PHP process. Using 1GB for image processing");
87
			$memory = 1024 * 1024 * 1024;
88
		} else {
89
			$this->logDebug(sprintf('Found %d MB available to PHP.', $memory / (1024 * 1024)));
90
			// No point going above 4GB ("4GB should be enough for everyone")
91
			if ($memory > 4 * 1024 * 1024 * 1024) {
92
				$this->logDebug('Capping used memory to maximum of 4GB');
93
				$memory = 4 * 1024 * 1024 * 1024;
94
			}
95
		}
96
97
		$context->propertyBag['memory'] = $memory;
98
99
		if ($memory < 1024 * 1024 * 1024) {
100
			$error_message =
101
				"\n" .
102
				"Seems that you have only " . intval($memory / (1024 * 1024)). "MB of memory given to PHP.\n" .
103
				"Face recognition application requires at least 1 GB. You need to change your memory_limit in php.ini.\n" .
104
				"Check https://secure.php.net/manual/en/ini.core.php#ini.memory-limit for details.\n" .
105
				"If you already set this to unlimited, it seems your system is not having enough RAM memory.";
106
			$this->logInfo($error_message);
107
			return false;
108
		}
109
110
		return true;
111
	}
112
}