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 OCA\Ocr\Db\OcrStatus; |
16
|
|
|
use OCA\Ocr\Db\OcrStatusMapper; |
17
|
|
|
use OCP\IL10N; |
18
|
|
|
use OCP\ILogger; |
19
|
|
|
|
20
|
|
|
class QueueService { |
21
|
|
|
|
22
|
|
|
private $mapper; |
23
|
|
|
|
24
|
|
|
private $logger; |
25
|
|
|
|
26
|
|
|
private $l10n; |
27
|
|
|
|
28
|
4 |
|
public function __construct(OcrStatusMapper $mapper, IL10N $l10n, ILogger $logger) { |
29
|
4 |
|
$this->mapper = $mapper; |
30
|
4 |
|
$this->logger = $logger; |
31
|
4 |
|
$this->l10n = $l10n; |
32
|
4 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Inits the client and sends the task to the background worker (async) |
36
|
|
|
* |
37
|
|
|
* @param OcrStatus $status |
38
|
|
|
* @param string $datadirectory |
39
|
|
|
* @param string $path |
40
|
|
|
* @param string $language |
41
|
|
|
* @param string $occDir |
42
|
|
|
*/ |
43
|
1 |
|
public function clientSend($status, $datadirectory, $path, $language, $occDir) { |
44
|
|
|
try { |
45
|
1 |
|
$this->mapper->insert($status); |
46
|
1 |
|
$queue = msg_get_queue(21671); |
47
|
1 |
|
$msg = json_encode(array( |
48
|
1 |
|
'type' => $status->getType(), |
49
|
1 |
|
'datadirectory' => $datadirectory, |
50
|
1 |
|
'path' => $path, |
51
|
1 |
|
'tempfile' => $status->getTempFile(), |
52
|
1 |
|
'language' => $language, |
53
|
1 |
|
'statusid' => $status->getId(), |
54
|
|
|
'occdir' => $occDir |
55
|
1 |
|
)); |
56
|
1 |
|
if (msg_send($queue, 1, $msg)) { |
57
|
1 |
|
$this->logger->debug('Client message: ' . json_encode($msg), ['app' => 'ocr']); |
58
|
1 |
|
} else { |
59
|
|
|
$this->mapper->delete($status); |
60
|
|
|
throw new NotFoundException($this->l10n->t('Could not add files to the ocr processing queue.')); |
61
|
|
|
} |
62
|
1 |
|
} catch (Exception $e) { |
63
|
|
|
exec('rm ' . $status->getTempFile()); |
64
|
|
|
$this->handleException($e); |
65
|
|
|
} |
66
|
1 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Handle the possible thrown Exceptions from all methods of this class. |
70
|
|
|
* |
71
|
|
|
* @param Exception $e |
72
|
|
|
* @throws Exception |
73
|
|
|
* @throws NotFoundException |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
private function handleException($e) { |
|
|
|
|
76
|
|
|
$this->logger->logException($e, ['app' => 'ocr', 'message' => 'Exception during message queue processing']); |
77
|
|
|
if ($e instanceof NotFoundException) { |
78
|
|
|
throw new NotFoundException($e->getMessage()); |
79
|
|
|
} else { |
80
|
|
|
throw $e; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.