CheckRequirementsTask::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017-2020 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
25
namespace OCA\FaceRecognition\BackgroundJob\Tasks;
26
27
use OCP\Util as OCP_Util;
28
29
use OCA\FaceRecognition\BackgroundJob\FaceRecognitionBackgroundTask;
30
use OCA\FaceRecognition\BackgroundJob\FaceRecognitionContext;
31
32
use OCA\FaceRecognition\Helper\Imaginary;
33
use OCA\FaceRecognition\Helper\MemoryLimits;
34
use OCA\FaceRecognition\Helper\Requirements;
35
36
use OCA\FaceRecognition\Model\IModel;
37
use OCA\FaceRecognition\Model\ModelManager;
38
39
use OCA\FaceRecognition\Model\Exceptions\UnavailableException;
40
41
use OCA\FaceRecognition\Service\SettingsService;
42
43
/**
44
 * Check all requirements before we start engaging in lengthy background task.
45
 */
46
class CheckRequirementsTask extends FaceRecognitionBackgroundTask {
47
48
	/** @var ModelManager Model Manader */
49
	private $modelManager;
50
51
	/** @var SettingsService Settings service */
52
	private $settingsService;
53
54
	/** @var Imaginary imaginary helper */
55
	private $imaginaryHelper;
56
57
	/**
58
	 * @param ModelManager $modelManager Model Manager
59
	 * @param SettingsService $settingsService Settings service
60
	 * @param Imaginary $imaginaryHelper imaginary helper
61
	 */
62
	public function __construct(ModelManager    $modelManager,
63
	                            SettingsService $settingsService,
64
	                            Imaginary       $imaginaryHelper)
65
	{
66
		parent::__construct();
67
68
		$this->modelManager    = $modelManager;
69
		$this->settingsService = $settingsService;
70
		$this->imaginaryHelper = $imaginaryHelper;
71
	}
72
73
	/**
74
	 * @inheritdoc
75
	 */
76
	public function description() {
77
		return "Check all requirements";
78
	}
79
80
	/**
81
	 * @inheritdoc
82
	 */
83
	public function execute(FaceRecognitionContext $context) {
84
		$this->setContext($context);
85
86
		$system = php_uname("s");
87
		$this->logDebug("System: " . $system);
88
89
		$systemMemory = MemoryLimits::getSystemMemory();
90
		$this->logDebug("System memory: " . ($systemMemory > 0 ? $systemMemory : "Unknown"));
91
92
		$phpMemory = MemoryLimits::getPhpMemory();
93
		$this->logDebug("PHP Memory Limit: " . ($phpMemory > 0 ? $phpMemory : "Unknown"));
94
95
		$this->logDebug("Clustering backend: " . (Requirements::pdlibLoaded() ? "pdlib" : "PHP (Not recommended.)"));
96
97
		if ($this->imaginaryHelper->isEnabled()) {
98
			$this->logDebug("Image Backend: Imaginary");
99
			$version = $this->imaginaryHelper->getVersion();
100
			if ($version) {
101
				$this->logDebug("Imaginary version: " . $version);
102
			} else {
103
				$imaginaryUrl = $this->imaginaryHelper->getUrl();
104
				$error_message =
105
					"An Imaginary service (" . $imaginaryUrl . ") was configured to manage temporary images, but it is inaccessible." .
106
					"Check out the service, or set the 'preview_imaginary_url' key appropriately.";
107
				$this->logInfo($error_message);
108
				return false;
109
			}
110
		} else {
111
			$this->logDebug("Image Backend: Imagick");
112
		}
113
114
		if (!Requirements::hasEnoughMemory()) {
115
			$error_message =
116
				"Your system does not meet the minimum of memory requirements.\n" .
117
				"Face recognition application requires at least " . OCP_Util::humanFileSize(SettingsService::MINIMUM_SYSTEM_MEMORY_REQUIREMENTS) . " of system memory.\n" .
118
				"See https://github.com/matiasdelellis/facerecognition/wiki/Performance-analysis-of-DLib%E2%80%99s-CNN-face-detection for more details\n\n" .
119
				"Fill an issue here if that doesn't help: https://github.com/matiasdelellis/facerecognition/issues";
120
			$this->logInfo($error_message);
121
			return false;
122
		}
123
124
		$model = $this->modelManager->getCurrentModel();
125
		if (is_null($model)) {
126
			$error_message =
127
				"Seems there are no installed models.\n" .
128
				"Please read the documentation about this: https://github.com/matiasdelellis/facerecognition/wiki/Models#install-models\n" .
129
				"and install them with the 'occ face:setup --model MODEL_ID' command.\n\n" .
130
				"Fill an issue here if that doesn't help: https://github.com/matiasdelellis/facerecognition/issues";
131
			$this->logInfo($error_message);
132
			return false;
133
		}
134
135
		$model_message = '';
136
		if (!$model->meetDependencies($model_message)) {
137
			$error_message =
138
				"Seems that don't meet the dependencies to use the model " . $model->getId() . ": " . $model->getName() . "\n".
139
				"Resume: " . $model_message . "\n" .
140
				"Please read the documentation for this model to continue: " . $model->getDocumentation() . "\n\n" .
141
				"Fill an issue here if that doesn't help: https://github.com/matiasdelellis/facerecognition/issues";
142
			$this->logInfo($error_message);
143
			return false;
144
		}
145
146
		$imageArea = $this->settingsService->getAnalysisImageArea();
147
		if ($imageArea < 0) {
148
			$error_message =
149
				"Seems that still don't configured the image area used for temporary files.\n" .
150
				"Please read the documentation about this: https://github.com/matiasdelellis/facerecognition/wiki/Settings#temporary-files\n" .
151
				"and then configure it in the admin panel to continue\n\n" .
152
				"Fill an issue here if that doesn't help: https://github.com/matiasdelellis/facerecognition/issues";
153
			$this->logInfo($error_message);
154
			return false;
155
		}
156
157
		try {
158
			$model->open();
159
		} catch (UnavailableException $e) {
160
			$this->logInfo("Error accessing the model to get required information: " . $e->getMessage());
161
			return false;
162
		}
163
164
		$maxImageArea = $model->getMaximumArea();
165
		if ($imageArea > $maxImageArea) {
166
			$error_message =
167
				"There are inconsistencies between the configured image area (" . $imageArea. " pixels^2) which is\n" .
168
				"greater that the maximum allowed by the model (". $maxImageArea . " pixels^2).\n" .
169
				"Please check and fix it in the admin panel to continue.\n\n" .
170
				"Fill an issue here if that doesn't help: https://github.com/matiasdelellis/facerecognition/issues";
171
			$this->logInfo($error_message);
172
			return false;
173
		}
174
175
		return true;
176
	}
177
}