Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Runner.php « Model « lib - github.com/nextcloud/fulltextsearch.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 086186139fd830245f9f2dfd9804ff3faf0d60af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
<?php
declare(strict_types=1);


/**
 * FullTextSearch - Full text search framework for Nextcloud
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author Maxence Lange <maxence@artificial-owl.com>
 * @copyright 2018
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


namespace OCA\FullTextSearch\Model;


use ArtificialOwl\MySmallPhpTools\Traits\TArrayTools;
use Exception;
use OCA\FullTextSearch\ACommandBase;
use OCA\FullTextSearch\Exceptions\RunnerAlreadyUpException;
use OCA\FullTextSearch\Exceptions\TickDoesNotExistException;
use OCA\FullTextSearch\Exceptions\TickIsNotAliveException;
use OCA\FullTextSearch\Service\RunningService;
use OCP\FullTextSearch\Model\IIndex;
use OCP\FullTextSearch\Model\IRunner;
use Symfony\Component\Console\Output\OutputInterface;


/**
 * Class Runner
 *
 * @package OCA\FullTextSearch\Model
 */
class Runner implements IRunner {


	use TArrayTools;


	const TICK_MINIMUM = 2;
	const TICK_UPDATE = 10;
	const MEMORY_UPDATE = 5;

	/** @var RunningService */
	private $runningService;

	/** @var string */
	private $source;

	/** @var int */
	private $tickId = 0;

	/** @var ACommandBase */
	private $base = null;

	/** @var OutputInterface */
	private $outputInterface = null;

	/** @var array */
	private $info = [];

	/** @var int */
	private $oldTick = 0;

	/** @var string */
	private $oldAction = '';

	/** @var int */
	private $ramUpdate = 0;

	/** @var int */
	private $tickUpdate = 0;

	/** @var array */
	private $methodOnKeyPress = [];

	/** @var array */
	private $methodOnInfoUpdate = [];

	/** @var array */
	private $methodOnIndexError = [];

	/** @var array */
	private $methodOnIndexResult = [];

	/** @var bool */
	private $paused = false;

	/** @var bool */
	private $pauseRunning = false;

	/** @var array */
	private $keys = ['nextStep' => 'n'];


	/**
	 * Runner constructor.
	 *
	 * @param RunningService $runningService
	 * @param string $source
	 * @param array $keys
	 */
	public function __construct(RunningService $runningService, string $source, array $keys = []) {
		$this->runningService = $runningService;
		$this->source = $source;

		if (sizeof($keys) > 0) {
			$this->keys = $keys;
		}
	}


	/**
	 * @throws RunnerAlreadyUpException
	 */
	public function start() {
		$this->tickId = $this->runningService->start($this->source);
	}


	/**
	 * @param string $action
	 * @param bool $force
	 *
	 * @return string
	 * @throws Exception
	 */
	public function updateAction(string $action = '', bool $force = false): string {

		if ($this->base !== null) {
			$this->base->abort();
		}

		$n = '';
		if (sizeof($this->methodOnKeyPress) > 0) {
			$n = fread(STDIN, 9999);
			if ($n !== '') {
				$n = substr($n, 0, 1);
				$this->keyPressed($n);
			}
		}

		if ($action === '') {
			return $n;
		}

		$tick = time();
		if ($this->oldAction !== $action || $force) {
			while (true) {
				if (!$this->isPaused()) {
					break;
				}

				$this->pauseRunning(true);
				$pressed = strtolower($this->updateAction(''));
				if ($pressed === $this->keys['nextStep']) {
					break;
				}

				usleep(300000);
				if ($this->base !== null) {
					$this->base->abort();
				}
			}

			$this->pauseRunning(false);
		}

		if ($this->oldAction === $action && ($this->oldTick + self::TICK_MINIMUM > $tick)) {
			return '';
		}

		$this->setInfo('action', $action);

		$this->updateTick($tick, $action);
		$this->updateRamInfo($tick);

		$this->oldAction = $action;
		$this->oldTick = $tick;

		return '';
	}


	/**
	 * @param string $info
	 * @param string $value
	 * @param int $type
	 */
	public function setInfo(string $info, string $value, int $type = 0) {
		$this->info[$info] = $value;
		$this->setInfoColored($info, $type);
		$this->infoUpdated();
	}

	/**
	 * @param string $info
	 * @param int $value
	 */
	public function setInfoInt(string $info, int $value): void {
		$this->info[$info] = $value;
		$this->infoUpdated();
	}

	/**
	 * @param array $data
	 */
	public function setInfoArray(array $data) {
		$keys = array_keys($data);
		foreach ($keys as $k) {
			$this->info[$k] = $data[$k];
		}

		$this->infoUpdated();
	}


	/**
	 * @param string $info
	 * @param int $level
	 */
	public function setInfoColored(string $info, int $level) {

		$value = $this->getInfo($info);
		if ($value === '') {
			return;
		}

		$color = '';
		switch ($level) {
			case IRunner::RESULT_TYPE_SUCCESS:
				$color = 'info';
				break;

			case IRunner::RESULT_TYPE_WARNING:
				$color = 'comment';
				break;

			case IRunner::RESULT_TYPE_FAIL:
				$color = 'error';
				break;
		}

		if ($color !== '') {
			$this->info[$info . 'Colored'] = '<' . $color . '>' . $value . '</' . $color . '>';
		}
	}

	/**
	 * @return array
	 */
	public function getInfoAll(): array {
		return $this->info;
	}


	/**
	 * @param string $info
	 *
	 * @return string
	 */
	public function getInfo(string $info): string {
		return $this->get($info, $this->info, '');
	}

	/**
	 * @param string $info
	 *
	 * @return int
	 */
	public function getInfoInt(string $info): int {
		return $this->getInt($info, $this->info);
	}

	/**
	 * @param array $method
	 */
	public function onKeyPress(array $method) {
		$this->methodOnKeyPress[] = $method;
	}

	/**
	 * @param string $key
	 */
	public function keyPressed(string $key) {
		foreach ($this->methodOnKeyPress as $method) {
			call_user_func($method, $key);
		}
	}


	/**
	 * @param array $method
	 */
	public function onInfoUpdate(array $method) {
		$this->methodOnInfoUpdate[] = $method;
	}


	/**
	 *
	 */
	public function infoUpdated() {
		foreach ($this->methodOnInfoUpdate as $method) {
			call_user_func($method, $this->info);
		}
	}


	/**
	 * @param array $method
	 */
	public function onNewIndexError(array $method) {
		$this->methodOnIndexError[] = $method;
	}

	/**
	 * @param IIndex $index
	 * @param string $message
	 * @param string $class
	 * @param int $sev
	 */
	public function newIndexError(IIndex $index, string $message, string $class = '', int $sev = 3
	) {
		$error = [
			'index' => $index,
			'message' => $message,
			'exception' => $class,
			'severity' => $sev
		];

		foreach ($this->methodOnIndexError as $method) {
			call_user_func($method, $error);
		}
	}


	/**
	 * @param array $method
	 */
	public function onNewIndexResult(array $method) {
		$this->methodOnIndexResult[] = $method;
	}


	/**
	 * @param IIndex $index
	 * @param string $message
	 * @param string $status
	 * @param int $type
	 */
	public function newIndexResult(IIndex $index, string $message, string $status, int $type) {
		$result = [
			'index' => $index,
			'message' => $message,
			'status' => $status,
			'type' => $type
		];

		foreach ($this->methodOnIndexResult as $method) {
			call_user_func($method, $result);
		}
	}


	/**
	 * @param int $tick
	 * @param string $action
	 *
	 * @throws TickDoesNotExistException
	 */
	private function updateTick(int $tick, string $action) {
		if ($this->oldAction === $action && ($this->tickUpdate + self::TICK_UPDATE > $tick)) {
			return;
		}

		try {
			$this->runningService->update($this->tickId, $action);
		} catch (TickIsNotAliveException $e) {
			$this->output('Force Quit');
			exit();
		}

		$this->tickUpdate = $tick;
	}


	/**
	 * @param int $tick
	 */
	private function updateRamInfo(int $tick) {
		if (($this->ramUpdate + self::MEMORY_UPDATE) > $tick) {
			return;
		}

		$this->setInfo('_memory', round((memory_get_usage() / 1024 / 1024)) . ' MB');
		$this->ramUpdate = $tick;
	}


	/**
	 * // TODO: finalize this exception handling about error logs.
	 *
	 * @param string $reason
	 * @param bool $stop
	 */
	public function exception(string $reason, bool $stop) {
		if (!$stop) {
			$this->output('Exception: ' . $reason);
			// TODO: feed an array of exceptions for log;
		} else {
			try {
				$this->runningService->stop($this->tickId, $reason);
			} catch (TickDoesNotExistException $e) {
				/** exception will be managed somewhere else */
				// TODO: Check if above statement is correct.
			}
		}
	}


	/**
	 * @throws TickDoesNotExistException
	 */
	public function stop() {
		$this->runningService->stop($this->tickId);
	}


	/**
	 * @param ACommandBase $base
	 * @param OutputInterface $output
	 */
	public function sourceIsCommandLine(ACommandBase $base, OutputInterface $output) {
		$this->base = $base;
		$this->outputInterface = $output;
	}


	/**
	 * @param bool $pause
	 */
	public function pause(bool $pause) {
		$this->paused = $pause;
		$this->infoUpdated();
	}

	/**
	 * @return bool
	 */
	public function isPaused(): bool {
		return $this->paused;
	}


	/**
	 * @param bool $running
	 */
	public function pauseRunning(bool $running) {
		$this->pauseRunning = $running;
		$this->infoUpdated();
	}

	/**
	 * @return bool
	 */
	public function isPauseRunning(): bool {
		return $this->pauseRunning;
	}


	/**
	 * @param string $line
	 */
	public function output(string $line) {
		if ($this->outputInterface === null) {
			return;
		}

		$this->outputInterface->writeln($line);
	}


}