1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Nextcloud - OCR |
5
|
|
|
* |
6
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
7
|
|
|
* later. See the COPYING file. |
8
|
|
|
* |
9
|
|
|
* @author Janis Koehr <[email protected]> |
10
|
|
|
* @copyright Janis Koehr 2017 |
11
|
|
|
*/ |
12
|
|
|
namespace OCA\Ocr\Service; |
13
|
|
|
|
14
|
|
|
use Exception; |
15
|
|
|
use OCA\Ocr\Db\OcrJob; |
16
|
|
|
use OCA\Ocr\Db\OcrJobMapper; |
17
|
|
|
use OCP\IConfig; |
18
|
|
|
use OCP\IL10N; |
19
|
|
|
use OCP\ILogger; |
20
|
|
|
use OCA\Ocr\Constants\OcrConstants; |
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, ILogger $logger) { |
93
|
5 |
|
$this->mapper = $mapper; |
94
|
5 |
|
$this->fileService = $fileService; |
95
|
5 |
|
$this->logger = $logger; |
96
|
5 |
|
$this->l10n = $l10n; |
97
|
5 |
|
$this->config = $config; |
98
|
5 |
|
$this->redisHost = $this->config->getAppValue ( $this->appName, 'redisHost' ); |
99
|
5 |
|
$this->redisPort = intval ( $this->config->getAppValue ( $this->appName, 'redisPort' ) ); |
100
|
5 |
|
$this->redisDb = intval ( $this->config->getAppValue ( $this->appName, 'redisDb' ) ); |
101
|
5 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Inits the client and sends the task to the background worker (async) |
105
|
|
|
* |
106
|
|
|
* @param OcrJob $job |
107
|
|
|
* @param string[] $languages |
108
|
|
|
* @param string $occDir |
109
|
|
|
*/ |
110
|
|
|
public function sendJob($job, $languages, $occDir) { |
|
|
|
|
111
|
|
|
try { |
112
|
|
|
// check for messaging and put everything together |
113
|
|
|
$redis = $this->setupRedisInstance (); |
114
|
|
|
|
115
|
|
|
$job = $this->mapper->insert ( $job ); |
116
|
|
|
$msg = json_encode ( 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
|
|
|
|
144
|
|
|
$result = $redis->multi ()->lRange ( OcrConstants::REDIS_FINISHED_JOBS_QUEUE, 0, - 1 )->delete ( OcrConstants::REDIS_FINISHED_JOBS_QUEUE )->exec (); |
145
|
|
|
$this->logger->debug ( 'Retrieved the following array from redis: {result}', [ |
146
|
|
|
'result' => $result [0] |
147
|
|
|
] ); |
148
|
|
|
return $result [0]; |
149
|
|
|
} catch ( Exception $e ) { |
150
|
|
|
$this->handleException ( $e ); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Setup the Redis instance and return to whom ever it needs. |
156
|
|
|
* |
157
|
|
|
* @throws NotFoundException |
158
|
|
|
* @return \Redis |
159
|
|
|
*/ |
160
|
|
|
private function setupRedisInstance() { |
161
|
|
View Code Duplication |
if (! extension_loaded ( 'redis' )) { |
|
|
|
|
162
|
|
|
$this->logger->debug ( 'It seems that the message queueing capabilities are not available in your local php installation. Please install php-redis.' ); |
163
|
|
|
throw new NotFoundException ( $this->l10n->t ( 'Message queueing capabilities are missing on the server.' ) ); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$redis = new \Redis (); |
167
|
|
|
if (! $redis->connect ( $this->redisHost, $this->redisPort, 2.5, NULL, 100 )) { |
168
|
|
|
$this->logger->debug ( 'Cannot connect to Redis.' ); |
169
|
|
|
throw new NotFoundException ( $this->l10n->t ( 'Cannot connect to Redis.' ) ); |
170
|
|
|
} |
171
|
|
View Code Duplication |
if (! $redis->select ( $this->redisDb )) { |
|
|
|
|
172
|
|
|
$this->logger->debug ( 'Cannot connect to the right Redis database.' ); |
173
|
|
|
throw new NotFoundException ( $this->l10n->t ( 'Cannot connect to the right Redis database.' ) ); |
174
|
|
|
} |
175
|
|
|
$redis->setOption ( \Redis::OPT_PREFIX, OcrConstants::REDIS_KEY_PREFIX ); |
176
|
|
|
|
177
|
|
|
return $redis; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Handle the possible thrown Exceptions from all methods of this class. |
182
|
|
|
* |
183
|
|
|
* @param Exception $e |
184
|
|
|
* @throws Exception |
185
|
|
|
* @throws NotFoundException |
186
|
|
|
*/ |
187
|
|
|
private function handleException($e) { |
188
|
|
|
$this->logger->logException ( $e, [ |
189
|
|
|
'message' => 'Exception during message queue processing' |
190
|
|
|
] ); |
191
|
|
|
throw $e; |
192
|
|
|
} |
193
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.