Completed
Push — master ( e5ee26...c91a04 )
by Maxence
01:58
created

Runner::sourceIsCommandLine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * FullTextSearch - Full text search framework for Nextcloud
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2018
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
28
namespace OCA\FullTextSearch\Model;
29
30
use Exception;
31
use OCA\FullTextSearch\Exceptions\InterruptException;
32
use OCA\FullTextSearch\Exceptions\RunnerAlreadyUpException;
33
use OCA\FullTextSearch\Exceptions\TickDoesNotExistException;
34
use OCA\FullTextSearch\Exceptions\TickIsNotAliveException;
35
use OCA\FullTextSearch\Service\RunningService;
36
use Symfony\Component\Console\Output\OutputInterface;
37
38
39
class Runner {
40
41
42
	const TICK_TTL = 300;
43
	const TICK_MINIMUM = 2;
44
	const INFO_UPDATE = 10;
45
46
	/** @var RunningService */
47
	private $runningService;
48
49
	/** @var string */
50
	private $source;
51
52
	/** @var bool */
53
	private $strict = false;
54
55
	/** @var int */
56
	private $tickId;
57
58
	/** @var ExtendedBase */
59
	private $commandBase = null;
60
61
	/** @var OutputInterface */
62
	private $outputInterface = null;
63
64
	/** @var int */
65
	private $oldTick = 0;
66
67
	/** @var string */
68
	private $oldAction = '';
69
70
	/** @var int */
71
	private $ramTick = 0;
72
73
	/**
74
	 * Runner constructor.
75
	 *
76
	 * @param RunningService $runningService
77
	 * @param string $source
78
	 */
79
	public function __construct(RunningService $runningService, $source) {
80
		$this->runningService = $runningService;
81
		$this->source = $source;
82
	}
83
84
85
	/**
86
	 * @param bool $strict
87
	 *
88
	 * @throws RunnerAlreadyUpException
89
	 */
90
	public function start($strict = false) {
91
		$this->strict = $strict;
92
		$this->tickId = $this->runningService->start($this->source);
93
	}
94
95
96
	/**
97
	 * @param $action
98
	 *
99
	 * @throws InterruptException
100
	 * @throws TickDoesNotExistException
101
	 */
102
	public function update($action) {
103
104
		$tick = time();
105
		try {
106
			$this->hasBeenInterrupted();
107
		} catch (InterruptException $e) {
108
			$this->stop();
109
			throw $e;
110
		}
111
112
		if ($this->oldAction === $action && ($this->oldTick + self::TICK_MINIMUM > $tick)) {
113
			return;
114
		}
115
116
		try {
117
			$this->runningService->update($this->tickId, $action);
118
		} catch (TickIsNotAliveException $e) {
119
			$this->output('Force Quit');
120
			exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method update() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
121
		}
122
123
		$this->updateInfo($tick);
124
		$this->oldAction = $action;
125
		$this->oldTick = $tick;
126
	}
127
128
129
	/**
130
	 * @throws InterruptException
131
	 */
132
	private function hasBeenInterrupted() {
133
		if ($this->commandBase === null) {
134
			return;
135
		}
136
		$this->commandBase->hasBeenInterrupted();
137
	}
138
139
140
	/**
141
	 * @param $tick
142
	 */
143
	private function updateInfo($tick) {
144
		if (($this->ramTick + self::INFO_UPDATE) > $tick) {
145
			return;
146
		}
147
148
		$this->output('- RAM: ' . (memory_get_usage() / 1024 / 1024));
149
		$this->ramTick = $tick;
150
	}
151
152
153
	public function exception($reason, $stop) {
154
		if (!$stop) {
155
			$this->output('Exception: ' . $reason);
156
			// TODO: feed an array of exceptions for log;
157
		}
158
		$this->runningService->exception($this->tickId, $reason, $stop);
159
	}
160
161
162
	/**
163
	 * @throws TickDoesNotExistException
164
	 */
165
	public function stop() {
166
		$this->runningService->stop($this->tickId);
167
	}
168
169
170
	/**
171
	 * @param ExtendedBase $base
172
	 * @param OutputInterface $output
173
	 */
174
	public function sourceIsCommandLine(ExtendedBase $base, OutputInterface $output) {
175
		$this->outputInterface = $output;
176
		$this->commandBase = $base;
177
	}
178
179
180
	public function isStrict() {
181
		return $this->strict;
182
	}
183
184
	/**
185
	 * @param string $line
186
	 */
187
	public function output($line) {
188
		if ($this->outputInterface === null) {
189
			return;
190
		}
191
192
		$this->outputInterface->writeln($line);
193
	}
194
195
196
}