1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Nextcloud - OCR |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. |
7
|
|
|
* 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\IL10N; |
18
|
|
|
use OCP\ITempManager; |
19
|
|
|
use OCP\ILogger; |
20
|
|
|
use OCA\Ocr\Constants\OcrConstants; |
21
|
|
|
use OCA\Ocr\Util\FileUtil; |
22
|
|
|
use OCA\Ocr\Util\RedisUtil; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class RedisService |
27
|
|
|
* |
28
|
|
|
* @package OCA\Ocr\Service |
29
|
|
|
*/ |
30
|
|
|
class RedisService { |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
* @var RedisUtil |
35
|
|
|
*/ |
36
|
|
|
private $redisUtil; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* |
40
|
|
|
* @var OcrJobMapper |
41
|
|
|
*/ |
42
|
|
|
private $mapper; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* |
46
|
|
|
* @var FileUtil |
47
|
|
|
*/ |
48
|
|
|
private $fileUtil; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* |
52
|
|
|
* @var ILogger |
53
|
|
|
*/ |
54
|
|
|
private $logger; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* |
58
|
|
|
* @var IL10N |
59
|
|
|
*/ |
60
|
|
|
private $l10n; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* |
64
|
|
|
* @var ITempManager |
65
|
|
|
*/ |
66
|
|
|
private $tempM; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* QueueService constructor. |
70
|
|
|
* |
71
|
|
|
* @param OcrJobMapper $mapper |
72
|
|
|
* @param FileUtil $fileUtil |
73
|
|
|
* @param RedisUtil $redisUtil |
74
|
|
|
* @param IL10N $l10n |
75
|
|
|
* @param ILogger $logger |
76
|
|
|
*/ |
77
|
8 |
|
public function __construct(OcrJobMapper $mapper, FileUtil $fileUtil, RedisUtil $redisUtil, IL10N $l10n, |
78
|
|
|
ILogger $logger, ITempManager $tempManager) { |
79
|
8 |
|
$this->mapper = $mapper; |
80
|
8 |
|
$this->fileUtil = $fileUtil; |
81
|
8 |
|
$this->logger = $logger; |
82
|
8 |
|
$this->l10n = $l10n; |
83
|
8 |
|
$this->redisUtil = $redisUtil; |
84
|
8 |
|
$this->tempM = $tempManager; |
85
|
8 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Inits the client and sends the task to the background worker (async) |
89
|
|
|
* |
90
|
|
|
* @param OcrJob $job |
91
|
|
|
* @param string[] $languages |
92
|
|
|
*/ |
93
|
2 |
|
public function sendJob($job, $languages) { |
94
|
|
|
try { |
95
|
|
|
// check for messaging and put everything together |
96
|
2 |
|
$redis = $this->redisUtil->setupRedisInstance(); |
97
|
2 |
|
$job = $this->mapper->insert($job); |
98
|
2 |
|
$msg = json_encode( |
99
|
|
|
[ |
100
|
2 |
|
'id' => $job->getId(), |
101
|
2 |
|
'type' => $job->getType(), |
102
|
2 |
|
'source' => $job->getSource(), |
103
|
2 |
|
'tempFile' => $job->getTempFile(), |
104
|
2 |
|
'languages' => $languages |
105
|
|
|
]); |
106
|
2 |
|
if (!$redis->lPush(OcrConstants::REDIS_NEW_JOBS_QUEUE, $msg)) { |
107
|
2 |
|
throw new NotFoundException($this->l10n->t('Could not add files to the Redis OCR processing queue.')); |
108
|
|
|
} |
109
|
1 |
|
} catch (Exception $e) { |
110
|
1 |
|
$this->fileUtil->execRemove($this->tempM->getTempBaseDir().'/'.$job->getTempFile().'.pdf'); |
111
|
1 |
|
$job->setStatus(OcrConstants::STATUS_FAILED); |
112
|
1 |
|
$job->setErrorLog($e->getMessage()); |
113
|
1 |
|
$this->mapper->update($job); |
114
|
1 |
|
$this->handleException($e); |
115
|
|
|
} |
116
|
1 |
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Retrieves all finished jobs from redis and returns them. |
120
|
|
|
* |
121
|
|
|
* @return string[] |
122
|
|
|
*/ |
123
|
1 |
|
public function readingFinishedJobs() { |
124
|
|
|
try { |
125
|
1 |
|
$redis = $this->redisUtil->setupRedisInstance(); |
126
|
1 |
|
$result = $redis->multi() |
127
|
1 |
|
->lRange(OcrConstants::REDIS_FINISHED_JOBS_QUEUE, 0, -1) |
128
|
1 |
|
->delete(OcrConstants::REDIS_FINISHED_JOBS_QUEUE) |
129
|
1 |
|
->exec(); |
130
|
1 |
|
$this->logger->debug('Retrieved the following array from Redis: {result}', |
131
|
|
|
[ |
132
|
1 |
|
'result' => $result[0], |
133
|
|
|
'app' => OcrConstants::APP_NAME |
134
|
|
|
]); |
135
|
1 |
|
return $result[0]; |
136
|
|
|
} catch (Exception $e) { |
137
|
|
|
$this->handleException($e); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Handle the possible thrown Exceptions from all methods of this class. |
143
|
|
|
* |
144
|
|
|
* @param Exception $e |
145
|
|
|
* @throws Exception |
146
|
|
|
* @throws NotFoundException |
147
|
|
|
*/ |
148
|
1 |
|
private function handleException($e) { |
149
|
1 |
|
$this->logger->logException($e, |
150
|
|
|
[ |
151
|
1 |
|
'message' => 'Exception during message queue processing', |
152
|
|
|
'app' => OcrConstants::APP_NAME |
153
|
|
|
]); |
154
|
1 |
|
throw $e; |
155
|
|
|
} |
156
|
|
|
} |