1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* nextCloud - ocr |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the COPYING file. |
7
|
|
|
* |
8
|
|
|
* @author Janis Koehr <[email protected]> |
9
|
|
|
* @copyright Janis Koehr 2016 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace OCA\Ocr\Service; |
13
|
|
|
|
14
|
|
|
use Exception; |
15
|
|
|
use OCP\IL10N; |
16
|
|
|
use OCP\ILogger; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class GearmanWorkerService |
20
|
|
|
* |
21
|
|
|
* @package OCA\Ocr\Service |
22
|
|
|
*/ |
23
|
|
|
class GearmanWorkerService { |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ILogger |
27
|
|
|
*/ |
28
|
|
|
private $logger; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var IL10N |
32
|
|
|
*/ |
33
|
|
|
private $l10n; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* GearmanWorkerService constructor. |
37
|
|
|
* |
38
|
|
|
* @param IL10N $l10n |
39
|
|
|
* @param ILogger $logger |
40
|
|
|
*/ |
41
|
2 |
|
public function __construct(IL10N $l10n, ILogger $logger) { |
42
|
2 |
|
$this->logger = $logger; |
43
|
2 |
|
$this->l10n = $l10n; |
44
|
2 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Checks if a worker is active and registered at the Gearman Job Server. |
48
|
|
|
* returns false if not. |
49
|
|
|
* @return boolean|null |
50
|
|
|
*/ |
51
|
|
|
public function workerExists() { |
52
|
|
|
try { |
53
|
|
|
$checkCommand = 'gearadmin -h 127.0.0.1 -p 4730 --workers 2>&1'; |
54
|
|
|
exec($checkCommand, $result, $success); |
55
|
|
|
if ($success !== 0 && count($result) > 0) { |
56
|
|
|
throw new NotFoundException($this->l10n->t('Gearman worker detection failed.')); |
57
|
|
|
} |
58
|
|
|
// look into the resulting array. 3 because first row is the ps checking command, second row is the grep command separated from the ps and 3rd or more has to be the GearmanOCRWorker.php. |
59
|
|
|
foreach ($result as $res) { |
60
|
|
|
if (strpos($res, 'ocr') !== false) { |
61
|
|
|
$this->logger->debug('Worker found.', ['app' => 'ocr']); |
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
$this->logger->debug('No worker found.', ['app' => 'ocr']); |
66
|
|
|
return false; |
67
|
|
|
} catch (Exception $e) { |
68
|
|
|
$this->handleException($e); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Handle the possible thrown Exceptions from all methods of this class. |
74
|
|
|
* |
75
|
|
|
* @param Exception $e |
76
|
|
|
* @throws Exception |
77
|
|
|
* @throws NotFoundException |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
private function handleException($e) { |
80
|
|
|
$this->logger->logException($e, ['app' => 'ocr', 'message' => 'Exception during gearman worker service function processing']); |
81
|
|
|
if ($e instanceof NotFoundException) { |
82
|
|
|
throw new NotFoundException($e->getMessage()); |
83
|
|
|
} else { |
84
|
|
|
throw $e; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} |