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