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

QueueService::sendJob()   B

Complexity

Conditions 3
Paths 7

Size

Total Lines 26
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.7085

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
ccs 8
cts 14
cp 0.5714
cc 3
eloc 21
nc 7
nop 3
crap 3.7085
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
// TODO: tempfile creation can be removed - output directory will not be in temp dir.
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
	 *
32
	 * @var IConfig
33
	 */
34
	private $config;
35
	
36
	/**
37
	 *
38
	 * @var OcrJobMapper
39
	 */
40
	private $mapper;
41
	
42
	/**
43
	 *
44
	 * @var ILogger
45
	 */
46
	private $logger;
47
	
48
	/**
49
	 *
50
	 * @var IL10N
51
	 */
52
	private $l10n;
53
	
54
	/**
55
	 *
56
	 * @var resource
57
	 */
58
	private $queue;
59
	
60
	/**
61
	 *
62
	 * @var resource
63
	 */
64
	private $statusqueue;
65
	
66 7
	/**
67 7
	 * QueueService constructor.
68 7
	 *
69 7
	 * @param OcrJobMapper $mapper        	
70 7
	 * @param IConfig $config        	
71
	 * @param IL10N $l10n        	
72 7
	 * @param ILogger $logger        	
73 7
	 */
74
	public function __construct(OcrJobMapper $mapper, IConfig $config, IL10N $l10n, ILogger $logger) {
75
		$this->mapper = $mapper;
76
		$this->logger = $logger;
77
		$this->l10n = $l10n;
78 7
		$this->config = $config;
79
		try {
80
			$this->queue = msg_get_queue ( 21671 );
81
			$this->statusqueue = msg_get_queue ( 27672 );
82
		} catch ( Exception $e ) {
83
			$this->logger->debug ( 'It seems that the message queueing capabilities are not available in your local php installation.', [ 
84
					'app' => 'ocr' 
85
			] );
86
			$this->handleException ( $e );
87 1
		}
88
	}
89 1
	
90 1
	/**
91 1
	 * Inits the client and sends the task to the background worker (async)
92 1
	 *
93 1
	 * @param OcrJob $job        	
94 1
	 * @param string[] $languages        	
95 1
	 * @param string $occDir        	
96 1
	 */
97
	public function sendJob($job, $languages, $occDir) {
98 1
		try {
99 1
			$job = $this->mapper->insert ( $job );
100
			$msg = json_encode ( array (
101
					'type' => $job->getType (),
102 1
					'source' => $this->config->getSystemValue ( 'datadirectory' ) . '/' . $job->getSource (),
103
					'tempfile' => $job->getTempFile (),
104
					'languages' => $languages,
105
					'jobid' => $job->getId (),
106
					'occdir' => $occDir 
107
			) );
108
			if (msg_send ( $this->queue, 1, $msg )) {
109
				$this->logger->debug ( 'Client message: ' . $msg, [ 
110 1
						'app' => 'ocr' 
111
				] );
112
			} else {
113
				$this->mapper->delete ( $job );
114
				throw new NotFoundException ( $this->l10n->t ( 'Could not add files to the OCR processing queue.' ) );
115
			}
116
		} catch ( Exception $e ) {
117
 			exec ( 'rm ' . $job->getTempFile () );
118 1
			$job->setStatus ( 'FAILED' );
119
 			$this->mapper->update ( $job );
120 1
			$this->handleException ( $e );
121 1
		}
122 1
	}
123
	
124
	/**
125
	 * TODO: in the future this function could be used to give an admin information
126
	 * Counts the messages in the message queue.
127
	 *
128
	 * @return mixed
129
	 */
130 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...
131
		try {
132
			$stats = msg_stat_queue ( $this->queue );
133
			$this->logger->debug ( 'Current message count: ' . json_encode ( $stats ['msg_qnum'] ), [ 
134 1
					'app' => 'ocr' 
135
			] );
136 1
			return $stats ['msg_qnum'];
137 1
		} catch ( Exception $e ) {
138 1
			$this->handleException ( $e );
139
		}
140
	}
141
	
142
	/**
143
	 * TODO: in the future this function could be used to give an admin information
144
	 * Counts the at this point processed files
145
	 *
146
	 * @return mixed
147
	 */
148 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...
149
		try {
150
			$stats = msg_stat_queue ( $this->statusqueue );
151
			$this->logger->debug ( 'Current active processing count: ' . json_encode ( $stats ['msg_qnum'] ), [ 
152
					'app' => 'ocr' 
153
			] );
154
			return $stats ['msg_qnum'];
155
		} catch ( Exception $e ) {
156
			$this->handleException ( $e );
157
		}
158
	}
159
	
160
	/**
161
	 * Handle the possible thrown Exceptions from all methods of this class.
162
	 *
163
	 * @param Exception $e        	
164
	 * @throws Exception
165
	 * @throws NotFoundException
166
	 */
167 View Code Duplication
	private function handleException($e) {
168
		$this->logger->logException ( $e, [ 
169
				'app' => 'ocr',
170
				'message' => 'Exception during message queue processing' 
171
		] );
172
		if ($e instanceof NotFoundException) {
173
			throw new NotFoundException ( $e->getMessage () );
174
		} else {
175
			throw $e;
176
		}
177
	}
178
}