|
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\ILogger; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class GearmanWorkerService |
|
19
|
|
|
* |
|
20
|
|
|
* @package OCA\Ocr\Service |
|
21
|
|
|
*/ |
|
22
|
|
|
class GearmanWorkerService { |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var ILogger |
|
26
|
|
|
*/ |
|
27
|
|
|
private $logger; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* GearmanWorkerService constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param ILogger $logger |
|
33
|
|
|
*/ |
|
34
|
2 |
|
public function __construct(ILogger $logger) { |
|
35
|
2 |
|
$this->logger = $logger; |
|
36
|
2 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Checks if a worker is active and registered at the Gearman Job Server. |
|
40
|
|
|
* returns false if not. |
|
41
|
|
|
* @return boolean|null |
|
42
|
|
|
*/ |
|
43
|
|
|
public function workerExists() { |
|
44
|
|
|
try { |
|
45
|
|
|
$checkCommand = 'gearadmin -h 127.0.0.1 -p 4730 --workers 2>&1'; |
|
46
|
|
|
exec($checkCommand, $result, $success); |
|
47
|
|
|
if ($success !== 0 && count($result) > 0) { |
|
48
|
|
|
throw new NotFoundException('Gearman worker detection failed.'); |
|
49
|
|
|
} |
|
50
|
|
|
// 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. |
|
51
|
|
|
foreach ($result as $res) { |
|
52
|
|
|
if (strpos($res, 'ocr') !== false) { |
|
53
|
|
|
$this->logger->debug('Worker found.', ['app' => 'ocr']); |
|
54
|
|
|
return true; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
$this->logger->debug('No worker found.', ['app' => 'ocr']); |
|
58
|
|
|
return false; |
|
59
|
|
|
} catch (Exception $e) { |
|
60
|
|
|
$this->handleException($e); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Handle the possible thrown Exceptions from all methods of this class. |
|
66
|
|
|
* |
|
67
|
|
|
* @param Exception $e |
|
68
|
|
|
* @throws Exception |
|
69
|
|
|
* @throws NotFoundException |
|
70
|
|
|
*/ |
|
71
|
|
View Code Duplication |
private function handleException($e) { |
|
72
|
|
|
$this->logger->logException($e, ['app' => 'ocr', 'message' => 'Exception during gearman worker service function processing']); |
|
73
|
|
|
if ($e instanceof NotFoundException) { |
|
74
|
|
|
throw new NotFoundException($e->getMessage()); |
|
75
|
|
|
} else { |
|
76
|
|
|
throw $e; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
} |