Completed
Pull Request — master (#93)
by Janis
14:35
created

QueueService::sendJob()   B

Complexity

Conditions 3
Paths 7

Size

Total Lines 24
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3.3332

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 12
cts 18
cp 0.6667
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 20
nc 7
nop 3
crap 3.3332
1
<?php
2
/**
3
 * Nextcloud - OCR
4
 *
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
12
 // TODO: tempfile creation can be removed - output directory will not be in temp dir.
13
14
namespace OCA\Ocr\Service;
15
16
use Exception;
17
use OCA\Ocr\Db\OcrJob;
18
use OCA\Ocr\Db\OcrJobMapper;
19
use OCP\IConfig;
20
use OCP\IL10N;
21
use OCP\ILogger;
22
23
/**
24
 * Class QueueService
25
 *
26
 * @package OCA\Ocr\Service
27
 */
28
class QueueService {
29
30
	/**
31
	 * @var IConfig
32
	 */
33
	private $config;
34
35
	/**
36
	 * @var OcrStatusMapper
37
	 */
38
	private $mapper;
39
40
	/**
41
	 * @var ILogger
42
	 */
43
	private $logger;
44
45
	/**
46
	 * @var IL10N
47
	 */
48
	private $l10n;
49
50
	/**
51
	 * @var resource
52
	 */
53
	private $queue;
54
55
	/**
56
	 * @var resource
57
	 */
58
	private $statusqueue;
59
60
	/**
61
	 * QueueService constructor.
62
	 *
63
	 * @param OcrJobMapper $mapper
64
	 * @param IConfig $config
65
	 * @param IL10N $l10n
66 7
	 * @param ILogger $logger
67 7
	 */
68 7
	public function __construct(OcrJobMapper $mapper, IConfig $config, IL10N $l10n, ILogger $logger) {
69 7
		$this->mapper = $mapper;
70 7
		$this->logger = $logger;
71
		$this->l10n = $l10n;
72 7
		$this->config = $config;
73 7
		try {
74
			$this->queue = msg_get_queue(21671);
75
			$this->statusqueue = msg_get_queue(27672);
76
		} catch (Exception $e) {
77
			$this->logger->debug('It seems that the message queueing capabilities are not available in your local php installation.', ['app' => 'ocr']);
78 7
			$this->handleException($e);
79
		}
80
	}
81
82
	/**
83
	 * Inits the client and sends the task to the background worker (async)
84
	 *
85
	 * @param OcrJob $status
86
	 * @param string[] $languages
87 1
	 * @param string $occDir
88
	 */
89 1
	public function sendJob($status, $languages, $occDir) {
90 1
		try {
91 1
				$this->mapper->insert($status);
92 1
				$msg = json_encode(array(
93 1
					'type' => $status->getType(),
94 1
					'source' => $this->config->getSystemValue('datadirectory') . '/' . $status->getSource(),
95 1
					'tempfile' => $status->getTempFile(),
96 1
					'languages' => $languages,
97
					'statusid' => $status->getId(),
98 1
					'occdir' => $occDir
99 1
				));
100
				if (msg_send($this->queue, 1, $msg)) {
101
					$this->logger->debug('Client message: ' . $msg, ['app' => 'ocr']);
102 1
				} else {
103
					$this->mapper->delete($status);
104
					throw new NotFoundException($this->l10n->t('Could not add files to the OCR processing queue.'));
105
				}
106
		} catch (Exception $e) {
107
			exec('rm ' . $status->getTempFile());
108
			$status->setStatus('FAILED');
109
			$this->mapper->update($status);
110 1
			$this->handleException($e);
111
		}
112
	}
113
114
	/**
115
	 * TODO: in the future this function could be used to give an admin information
116
	 * Counts the messages in the message queue.
117
	 *
118 1
	 * @return mixed
119
	 */
120 1 View Code Duplication
	public function countMessages() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
121 1
		try {
122 1
			$stats = msg_stat_queue($this->queue);
123
			$this->logger->debug('Current message count: ' . json_encode($stats['msg_qnum']), ['app' => 'ocr']);
124
			return $stats['msg_qnum'];
125
		} catch (Exception $e) {
126
			$this->handleException($e);
127
		}
128
	}
129
130
	/**
131
	 * TODO: in the future this function could be used to give an admin information
132
	 * Counts the at this point processed files
133
	 *
134 1
	 * @return mixed
135
	 */
136 1 View Code Duplication
	public function countActiveProcesses() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
137 1
		try {
138 1
			$stats = msg_stat_queue($this->statusqueue);
139
			$this->logger->debug('Current active processing count: ' . json_encode($stats['msg_qnum']), ['app' => 'ocr']);
140
			return $stats['msg_qnum'];
141
		} catch (Exception $e) {
142
			$this->handleException($e);
143
		}
144
	}
145
146
	/**
147
	 * Handle the possible thrown Exceptions from all methods of this class.
148
	 *
149
	 * @param Exception $e
150
	 * @throws Exception
151
	 * @throws NotFoundException
152
	 */
153 View Code Duplication
	private function handleException($e) {
154
		$this->logger->logException($e, ['app' => 'ocr', 'message' => 'Exception during message queue processing']);
155
		if ($e instanceof NotFoundException) {
156
			throw new NotFoundException($e->getMessage());
157
		} else {
158
			throw $e;
159
		}
160
	}
161
162
}