Completed
Push — master ( f21795...e63d25 )
by Janis
02:34
created

QueueService::countActiveProcesses()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 9
Code Lines 7

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 2.3149

Importance

Changes 0
Metric Value
dl 9
loc 9
ccs 4
cts 7
cp 0.5714
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 7
nc 4
nop 0
crap 2.3149
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 2016
10
 */
11
12
namespace OCA\Ocr\Service;
13
14
use Exception;
15
use OCA\Ocr\Db\OcrStatus;
16
use OCA\Ocr\Db\OcrStatusMapper;
17
use OCP\IConfig;
18
use OCP\IL10N;
19
use OCP\ILogger;
20
21
/**
22
 * Class QueueService
23
 *
24
 * @package OCA\Ocr\Service
25
 */
26
class QueueService {
27
28
	/**
29
	 * @var IConfig
30
	 */
31
	private $config;
32
33
	/**
34
	 * @var OcrStatusMapper
35
	 */
36
	private $mapper;
37
38
	/**
39
	 * @var ILogger
40
	 */
41
	private $logger;
42
43
	/**
44
	 * @var IL10N
45
	 */
46
	private $l10n;
47
48
	/**
49
	 * @var resource
50
	 */
51
	private $queue;
52
53
	/**
54
	 * @var resource
55
	 */
56
	private $statusqueue;
57
58
	/**
59
	 * QueueService constructor.
60
	 *
61
	 * @param OcrStatusMapper $mapper
62
	 * @param IConfig $config
63
	 * @param IL10N $l10n
64
	 * @param ILogger $logger
65
	 */
66 7
	public function __construct(OcrStatusMapper $mapper, IConfig $config, IL10N $l10n, ILogger $logger) {
67 7
		$this->mapper = $mapper;
68 7
		$this->logger = $logger;
69 7
		$this->l10n = $l10n;
70 7
		$this->queue = msg_get_queue(21671);
71 7
		$this->statusqueue = msg_get_queue(27672);
72 7
		$this->config = $config;
73 7
	}
74
75
	/**
76
	 * Inits the client and sends the task to the background worker (async)
77
	 *
78
	 * @param OcrStatus $status
79
	 * @param string $language
80
	 * @param string $occDir
81
	 */
82 1
	public function clientSend($status, $language, $occDir) {
83
		try {
84 1
				$this->mapper->insert($status);
85 1
				$msg = json_encode(array(
86 1
					'type' => $status->getType(),
87 1
					'source' => $this->config->getSystemValue('datadirectory') . '/' . $status->getSource(),
88 1
					'tempfile' => $status->getTempFile(),
89 1
					'language' => $language,
90 1
					'statusid' => $status->getId(),
91
					'occdir' => $occDir
92 1
				));
93 1
				if (msg_send($this->queue, 1, $msg)) {
94 1
					$this->logger->debug('Client message: ' . json_encode($msg), ['app' => 'ocr']);
95 1
				} else {
96
					$this->mapper->delete($status);
97
					throw new NotFoundException($this->l10n->t('Could not add files to the ocr processing queue.'));
98
				}
99 1
		} catch (Exception $e) {
100
			exec('rm ' . $status->getTempFile());
101
			$status->setStatus('FAILED');
102
			$this->mapper->update($status);
103
			$this->handleException($e);
104
		}
105 1
	}
106
107
	/**
108
	 * TODO: in the future this function could be used to give an admin information
109
	 * Counts the messages in the message queue.
110
	 *
111
	 * @return mixed
112
	 */
113 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...
114
		try {
115 1
			$stats = msg_stat_queue($this->queue);
116 1
			$this->logger->debug('Current message count: ' . json_encode($stats['msg_qnum']), ['app' => 'ocr']);
117 1
			return $stats['msg_qnum'];
118
		} catch (Exception $e) {
119
			$this->handleException($e);
120
		}
121
	}
122
123
	/**
124
	 * TODO: in the future this function could be used to give an admin information
125
	 * Counts the at this point processed files
126
	 *
127
	 * @return mixed
128
	 */
129 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...
130
		try {
131 1
			$stats = msg_stat_queue($this->statusqueue);
132 1
			$this->logger->debug('Current active processing count: ' . json_encode($stats['msg_qnum']), ['app' => 'ocr']);
133 1
			return $stats['msg_qnum'];
134
		} catch (Exception $e) {
135
			$this->handleException($e);
136
		}
137
	}
138
139
	/**
140
	 * Handle the possible thrown Exceptions from all methods of this class.
141
	 *
142
	 * @param Exception $e
143
	 * @throws Exception
144
	 * @throws NotFoundException
145
	 */
146 View Code Duplication
	private function handleException($e) {
147
		$this->logger->logException($e, ['app' => 'ocr', 'message' => 'Exception during message queue processing']);
148
		if ($e instanceof NotFoundException) {
149
			throw new NotFoundException($e->getMessage());
150
		} else {
151
			throw $e;
152
		}
153
	}
154
155
}