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
|
|
|
|
13
|
|
|
namespace OCA\Ocr\Service; |
14
|
|
|
|
15
|
|
|
use Exception; |
16
|
|
|
use OCA\Ocr\Db\OcrJob; |
17
|
|
|
use OCA\Ocr\Db\OcrJobMapper; |
18
|
|
|
use OCP\IConfig; |
19
|
|
|
use OCP\IL10N; |
20
|
|
|
use OCP\ILogger; |
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 ILogger |
44
|
|
|
*/ |
45
|
|
|
private $logger; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* |
49
|
|
|
* @var IL10N |
50
|
|
|
*/ |
51
|
|
|
private $l10n; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* QueueService constructor. |
55
|
|
|
* |
56
|
|
|
* @param OcrJobMapper $mapper |
57
|
|
|
* @param IConfig $config |
58
|
|
|
* @param IL10N $l10n |
59
|
|
|
* @param ILogger $logger |
60
|
|
|
*/ |
61
|
|
|
public function __construct(OcrJobMapper $mapper, IConfig $config, IL10N $l10n, ILogger $logger) { |
62
|
|
|
$this->mapper = $mapper; |
63
|
|
|
$this->logger = $logger; |
64
|
|
|
$this->l10n = $l10n; |
65
|
|
|
$this->config = $config; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Inits the client and sends the task to the background worker (async) |
70
|
|
|
* |
71
|
|
|
* @param OcrJob $job |
72
|
|
|
* @param string[] $languages |
73
|
|
|
* @param string $occDir |
74
|
|
|
*/ |
75
|
|
|
public function sendJob($job, $languages, $occDir) { |
|
|
|
|
76
|
|
|
try { |
77
|
|
|
// check for messaging and put everything together |
78
|
|
|
$redis = $this->setupRedisInstance (); |
79
|
|
|
|
80
|
|
|
$job = $this->mapper->insert ( $job ); |
81
|
|
|
$msg = json_encode ( array ( |
82
|
|
|
'id' => $job->getId (), |
83
|
|
|
'type' => $job->getType (), |
84
|
|
|
'source' => $job->getSource (), |
85
|
|
|
'tempFile' => $job->getTempFile (), |
86
|
|
|
'languages' => $languages |
87
|
|
|
) ); |
88
|
|
|
if (! $redis->lPush ( 'incoming', $msg )) { |
89
|
|
|
throw new NotFoundException ( $this->l10n->t ( 'Could not add files to the redis OCR processing queue.' ) ); |
90
|
|
|
} |
91
|
|
|
} catch ( Exception $e ) { |
92
|
|
|
exec ( 'rm ' . $job->getTempFile () ); |
93
|
|
|
$job->setStatus ( 'FAILED' ); |
94
|
|
|
$this->mapper->update ( $job ); |
95
|
|
|
$this->handleException ( $e ); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// TODO: implement reading finished jobs. |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Setup the Redis instance and return to whom ever it needs. |
103
|
|
|
* |
104
|
|
|
* @throws NotFoundException |
105
|
|
|
* @return \Redis |
106
|
|
|
*/ |
107
|
|
|
private function setupRedisInstance() { |
108
|
|
View Code Duplication |
if (! extension_loaded ( 'redis' )) { |
|
|
|
|
109
|
|
|
$this->logger->debug ( 'It seems that the message queueing capabilities are not available in your local php installation. Please install php-redis.', [ |
110
|
|
|
'app' => 'ocr' |
111
|
|
|
] ); |
112
|
|
|
throw new NotFoundException ( $this->l10n->t ( 'Message queueing capabilities are missing on the server.' ) ); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$redis = new \Redis (); |
116
|
|
|
// TODO: get from config |
117
|
|
|
if (! $redis->connect ( '127.0.0.1', 6379, 2.5, NULL, 100 )) { |
118
|
|
|
$this->logger->debug ( 'Cannot connect to Redis.', [ |
119
|
|
|
'app' => 'ocr' |
120
|
|
|
] ); |
121
|
|
|
throw new NotFoundException ( $this->l10n->t ( 'Cannot connect to Redis.' ) ); |
122
|
|
|
} |
123
|
|
View Code Duplication |
if (! $redis->select ( 0 )) { |
|
|
|
|
124
|
|
|
$this->logger->debug ( 'Cannot connect to the right Redis database.', [ |
125
|
|
|
'app' => 'ocr' |
126
|
|
|
] ); |
127
|
|
|
throw new NotFoundException ( $this->l10n->t ( 'Cannot connect to the right Redis database.' ) ); |
128
|
|
|
} |
129
|
|
|
$redis->setOption ( \Redis::OPT_PREFIX, 'ocr:' ); |
130
|
|
|
|
131
|
|
|
return $redis; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Handle the possible thrown Exceptions from all methods of this class. |
136
|
|
|
* |
137
|
|
|
* @param Exception $e |
138
|
|
|
* @throws Exception |
139
|
|
|
* @throws NotFoundException |
140
|
|
|
*/ |
141
|
|
View Code Duplication |
private function handleException($e) { |
142
|
|
|
$this->logger->logException ( $e, [ |
143
|
|
|
'app' => 'ocr', |
144
|
|
|
'message' => 'Exception during message queue processing' |
145
|
|
|
] ); |
146
|
|
|
if ($e instanceof NotFoundException) { |
147
|
|
|
throw new NotFoundException ( $e->getMessage () ); |
148
|
|
|
} else { |
149
|
|
|
throw $e; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.