|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Nextcloud - OCR |
|
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 2017 |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace OCA\Ocr\Service; |
|
12
|
|
|
|
|
13
|
|
|
use Exception; |
|
14
|
|
|
use OCA\Ocr\Db\OcrJob; |
|
15
|
|
|
use OCA\Ocr\Db\OcrJobMapper; |
|
16
|
|
|
use OCP\IConfig; |
|
17
|
|
|
use OCP\IL10N; |
|
18
|
|
|
use OCP\ILogger; |
|
19
|
|
|
use OCA\Ocr\Constants\OcrConstants; |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class RedisService |
|
24
|
|
|
* |
|
25
|
|
|
* @package OCA\Ocr\Service |
|
26
|
|
|
*/ |
|
27
|
|
|
class RedisService { |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* |
|
31
|
|
|
* @var IConfig |
|
32
|
|
|
*/ |
|
33
|
|
|
private $config; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* |
|
37
|
|
|
* @var OcrJobMapper |
|
38
|
|
|
*/ |
|
39
|
|
|
private $mapper; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* |
|
43
|
|
|
* @var FileService |
|
44
|
|
|
*/ |
|
45
|
|
|
private $fileService; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* |
|
49
|
|
|
* @var ILogger |
|
50
|
|
|
*/ |
|
51
|
|
|
private $logger; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* |
|
55
|
|
|
* @var IL10N |
|
56
|
|
|
*/ |
|
57
|
|
|
private $l10n; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* |
|
61
|
|
|
* @var string |
|
62
|
|
|
*/ |
|
63
|
|
|
private $redisHost; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* |
|
67
|
|
|
* @var integer |
|
68
|
|
|
*/ |
|
69
|
|
|
private $redisPort; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* |
|
73
|
|
|
* @var integer |
|
74
|
|
|
*/ |
|
75
|
|
|
private $redisDb; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* |
|
79
|
|
|
* @var string |
|
80
|
|
|
*/ |
|
81
|
|
|
private $appName = 'ocr'; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* QueueService constructor. |
|
85
|
|
|
* |
|
86
|
|
|
* @param OcrJobMapper $mapper |
|
87
|
|
|
* @param FileService $fileService |
|
88
|
|
|
* @param IConfig $config |
|
89
|
|
|
* @param IL10N $l10n |
|
90
|
|
|
* @param ILogger $logger |
|
91
|
|
|
*/ |
|
92
|
5 |
|
public function __construct(OcrJobMapper $mapper, FileService $fileService, IConfig $config, IL10N $l10n, |
|
93
|
|
|
ILogger $logger) { |
|
94
|
5 |
|
$this->mapper = $mapper; |
|
95
|
5 |
|
$this->fileService = $fileService; |
|
96
|
5 |
|
$this->logger = $logger; |
|
97
|
5 |
|
$this->l10n = $l10n; |
|
98
|
5 |
|
$this->config = $config; |
|
99
|
5 |
|
$this->redisHost = $this->config->getAppValue($this->appName, 'redisHost'); |
|
100
|
5 |
|
$this->redisPort = intval($this->config->getAppValue($this->appName, 'redisPort')); |
|
101
|
5 |
|
$this->redisDb = intval($this->config->getAppValue($this->appName, 'redisDb')); |
|
102
|
5 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Inits the client and sends the task to the background worker (async) |
|
106
|
|
|
* |
|
107
|
|
|
* @param OcrJob $job |
|
108
|
|
|
* @param string[] $languages |
|
109
|
|
|
*/ |
|
110
|
|
|
public function sendJob($job, $languages) { |
|
111
|
|
|
try { |
|
112
|
|
|
// check for messaging and put everything together |
|
113
|
|
|
$redis = $this->setupRedisInstance(); |
|
114
|
|
|
$job = $this->mapper->insert($job); |
|
115
|
|
|
$msg = json_encode( |
|
116
|
|
|
array( |
|
117
|
|
|
'id' => $job->getId(), |
|
118
|
|
|
'type' => $job->getType(), |
|
119
|
|
|
'source' => $job->getSource(), |
|
120
|
|
|
'tempFile' => $job->getTempFile(), |
|
121
|
|
|
'languages' => $languages |
|
122
|
|
|
)); |
|
123
|
|
|
if (!$redis->lPush(OcrConstants::REDIS_NEW_JOBS_QUEUE, $msg)) { |
|
124
|
|
|
throw new NotFoundException($this->l10n->t('Could not add files to the redis OCR processing queue.')); |
|
125
|
|
|
} |
|
126
|
|
|
} catch (Exception $e) { |
|
127
|
|
|
$this->fileService->execRemove($job->getTempFile()); |
|
128
|
|
|
$job->setStatus('FAILED'); |
|
129
|
|
|
$job->setErrorLog($this->l10n->t('Could not add files to the redis OCR processing queue.')); |
|
130
|
|
|
$this->mapper->update($job); |
|
131
|
|
|
$this->handleException($e); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Retrieves all finished jobs from redis and returns them. |
|
137
|
|
|
* |
|
138
|
|
|
* @return string[] |
|
139
|
|
|
*/ |
|
140
|
|
|
public function readingFinishedJobs() { |
|
141
|
|
|
try { |
|
142
|
|
|
$redis = $this->setupRedisInstance(); |
|
143
|
|
|
$result = $redis->multi() |
|
144
|
|
|
->lRange(OcrConstants::REDIS_FINISHED_JOBS_QUEUE, 0, -1) |
|
145
|
|
|
->delete(OcrConstants::REDIS_FINISHED_JOBS_QUEUE) |
|
146
|
|
|
->exec(); |
|
147
|
|
|
$this->logger->debug('Retrieved the following array from redis: {result}', |
|
148
|
|
|
[ |
|
149
|
|
|
'result' => $result[0] |
|
150
|
|
|
]); |
|
151
|
|
|
return $result[0]; |
|
152
|
|
|
} catch (Exception $e) { |
|
153
|
|
|
$this->handleException($e); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Setup the Redis instance and return to whom ever it needs. |
|
159
|
|
|
* |
|
160
|
|
|
* @throws NotFoundException |
|
161
|
|
|
* @return \Redis |
|
162
|
|
|
*/ |
|
163
|
|
|
private function setupRedisInstance() { |
|
164
|
|
View Code Duplication |
if (!extension_loaded('redis')) { |
|
|
|
|
|
|
165
|
|
|
$this->logger->debug( |
|
166
|
|
|
'It seems that the message queueing capabilities are not available in your local php installation. Please install php-redis.'); |
|
167
|
|
|
throw new NotFoundException($this->l10n->t('Message queueing capabilities are missing on the server.')); |
|
168
|
|
|
} |
|
169
|
|
|
$redis = new \Redis(); |
|
170
|
|
|
if (!$redis->connect($this->redisHost, $this->redisPort, 2.5, NULL, 100)) { |
|
171
|
|
|
$this->logger->debug('Cannot connect to Redis.'); |
|
172
|
|
|
throw new NotFoundException($this->l10n->t('Cannot connect to Redis.')); |
|
173
|
|
|
} |
|
174
|
|
View Code Duplication |
if (!$redis->select($this->redisDb)) { |
|
|
|
|
|
|
175
|
|
|
$this->logger->debug('Cannot connect to the right Redis database.'); |
|
176
|
|
|
throw new NotFoundException($this->l10n->t('Cannot connect to the right Redis database.')); |
|
177
|
|
|
} |
|
178
|
|
|
$redis->setOption(\Redis::OPT_PREFIX, OcrConstants::REDIS_KEY_PREFIX); |
|
179
|
|
|
return $redis; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Handle the possible thrown Exceptions from all methods of this class. |
|
184
|
|
|
* |
|
185
|
|
|
* @param Exception $e |
|
186
|
|
|
* @throws Exception |
|
187
|
|
|
* @throws NotFoundException |
|
188
|
|
|
*/ |
|
189
|
|
|
private function handleException($e) { |
|
190
|
|
|
$this->logger->logException($e, [ |
|
191
|
|
|
'message' => 'Exception during message queue processing' |
|
192
|
|
|
]); |
|
193
|
|
|
throw $e; |
|
194
|
|
|
} |
|
195
|
|
|
} |
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.