Completed
Push — master ( 4a9dd8...1b56cd )
by Matias
04:44 queued 04:40
created

CheckRequirementsTask::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 7
ccs 0
cts 6
cp 0
crap 2
rs 10
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\MemoryLimits;
33
use OCA\FaceRecognition\Helper\Requirements;
34
35
use OCA\FaceRecognition\Model\IModel;
36
use OCA\FaceRecognition\Model\ModelManager;
37
38
use OCA\FaceRecognition\Service\SettingsService;
39
40
/**
41
 * Check all requirements before we start engaging in lengthy background task.
42
 */
43
class CheckRequirementsTask extends FaceRecognitionBackgroundTask {
44
45
	/** @var ModelManager Model Manader */
46
	private $modelManager;
47
48
	/** @var SettingsService Settings service */
49
	private $settingsService;
50
51
	/**
52
	 * @param ModelManager $modelManager Model Manager
53
	 * @param SettingsService $settingsService Settings service
54
	 */
55
	public function __construct(ModelManager    $modelManager,
56
	                            SettingsService $settingsService)
57
	{
58
		parent::__construct();
59
60
		$this->modelManager    = $modelManager;
61
		$this->settingsService = $settingsService;
62
	}
63
64
	/**
65
	 * @inheritdoc
66
	 */
67
	public function description() {
68
		return "Check all requirements";
69
	}
70
71
	/**
72
	 * @inheritdoc
73
	 */
74
	public function execute(FaceRecognitionContext $context) {
75
		$this->setContext($context);
76
77
		if (!Requirements::pdlibLoaded()) {
78
			$error_message =
79
				"The PDlib PHP extension is not loaded. Cannot continue without it." .
80
				"Please read the documentation again about how to install the application: https://github.com/matiasdelellis/facerecognition/wiki/Installation";
81
			$this->logInfo($error_message);
82
			return false;
83
		}
84
85
		if (!Requirements::hasEnoughMemory()) {
86
			$error_message =
87
				"Your system does not meet the minimum of memory requirements.\n" .
88
				"Face recognition application requires at least " . OCP_Util::humanFileSize(SettingsService::MINIMUM_SYSTEM_MEMORY_REQUIREMENTS) . " of system memory.\n" .
89
				"See https://github.com/matiasdelellis/facerecognition/wiki/Performance-analysis-of-DLib%E2%80%99s-CNN-face-detection for more details\n\n" .
90
				"Fill an issue here if that doesn't help: https://github.com/matiasdelellis/facerecognition/issues";
91
			$this->logInfo($error_message);
92
			return false;
93
		}
94
95
		$model = $this->modelManager->getCurrentModel();
96
		if (is_null($model)) {
97
			$error_message =
98
				"Seems there are no installed models.\n" .
99
				"Please read the documentation about this: https://github.com/matiasdelellis/facerecognition/wiki/Models#install-models\n" .
100
				"and install them with the 'occ face:setup --model MODEL_ID' command.\n\n" .
101
				"Fill an issue here if that doesn't help: https://github.com/matiasdelellis/facerecognition/issues";
102
			$this->logInfo($error_message);
103
			return false;
104
		}
105
106
		$model_message = ''
107
		if (!$model->meetDependencies($model_message)) {
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_IF on line 107 at column 2
Loading history...
108
			$error_message =
109
				"Seems that don't meet the dependencies to use the model " . $model->getId() . ": " . $model->getName() . "\n".
110
				"Resume: " . $model_message . "\n" .
111
				"Please read the documentation for this model to continue: " . $model->getDocumentation() . "\n\n" .
112
				"Fill an issue here if that doesn't help: https://github.com/matiasdelellis/facerecognition/issues";
113
			$this->logInfo($error_message);
114
			return false;
115
		}
116
117
		$imageArea = $this->settingsService->getAnalysisImageArea();
118
		if ($imageArea < 0) {
119
			$error_message =
120
				"Seems that still don't configured the image area used for temporary files.\n" .
121
				"Please read the documentation about this: https://github.com/matiasdelellis/facerecognition/wiki/Settings#temporary-files\n" .
122
				"and then configure it in the admin panel to continue\n\n" .
123
				"Fill an issue here if that doesn't help: https://github.com/matiasdelellis/facerecognition/issues";
124
			$this->logInfo($error_message);
125
			return false;
126
		}
127
128
		$maxImageArea = $model->getMaximumArea();
129
		if ($imageArea > $maxImageArea) {
130
			$error_message =
131
				"There are inconsistencies between the configured image area (" . $imageArea. " pixels^2) which is\n" .
132
				"greater that the maximum allowed by the model (". $maxImageArea . " pixels^2).\n" .
133
				"Please check and fix it in the admin panel to continue.\n\n" .
134
				"Fill an issue here if that doesn't help: https://github.com/matiasdelellis/facerecognition/issues";
135
			$this->logInfo($error_message);
136
			return false;
137
		}
138
139
		$systemMemory = MemoryLimits::getSystemMemory();
140
		$this->logDebug("System memory: " . ($systemMemory > 0 ? $systemMemory : "Unknown"));
141
142
		$phpMemory = MemoryLimits::getPhpMemory();
143
		$this->logDebug("PHP Memory Limit: " . ($phpMemory > 0 ? $phpMemory : "Unknown"));
144
145
		return true;
146
	}
147
}