Completed
Push — master ( 58d870...e50603 )
by
unknown
02:12
created

Live::liveCycle()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 31

Duplication

Lines 13
Ratio 41.94 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 13
loc 31
rs 9.424
cc 4
nc 6
nop 0
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
namespace OCA\FullTextSearch\Command;
28
29
use Exception;
30
use OCA\FullTextSearch\Exceptions\InterruptException;
31
use OCA\FullTextSearch\Exceptions\TickDoesNotExistException;
32
use OCA\FullTextSearch\Model\ExtendedBase;
33
use OCA\FullTextSearch\Model\Runner;
34
use OCA\FullTextSearch\Service\ConfigService;
35
use OCA\FullTextSearch\Service\IndexService;
36
use OCA\FullTextSearch\Service\MiscService;
37
use OCA\FullTextSearch\Service\PlatformService;
38
use OCA\FullTextSearch\Service\ProviderService;
39
use OCA\FullTextSearch\Service\RunningService;
40
use OCP\IUserManager;
41
use Symfony\Component\Console\Input\InputInterface;
42
use Symfony\Component\Console\Output\OutputInterface;
43
44
45
class Live extends ExtendedBase {
46
47
	const CYCLE_DELAY = 10;
48
49
50
	/** @var IUserManager */
51
	private $userManager;
52
53
	/** @var ConfigService */
54
	private $configService;
55
56
	/** @var IndexService */
57
	private $indexService;
58
59
	/** @var PlatformService */
60
	private $platformService;
61
62
	/** @var ProviderService */
63
	private $providerService;
64
65
	/** @var MiscService */
66
	private $miscService;
67
68
69
	/** @var Runner */
70
	private $runner;
71
72
	/**
73
	 * Index constructor.
74
	 *
75
	 * @param IUserManager $userManager
76
	 * @param RunningService $runningService
77
	 * @param ConfigService $configService
78
	 * @param IndexService $indexService
79
	 * @param PlatformService $platformService
80
	 * @param ProviderService $providerService
81
	 * @param MiscService $miscService
82
	 */
83 View Code Duplication
	public function __construct(
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...
84
		IUserManager $userManager, RunningService $runningService, ConfigService $configService,
85
		IndexService $indexService, PlatformService $platformService,
86
		ProviderService $providerService, MiscService $miscService
87
	) {
88
		parent::__construct();
89
		$this->userManager = $userManager;
90
91
		$this->runner = new Runner($runningService, 'commandLive');
92
		$this->configService = $configService;
93
		$this->indexService = $indexService;
94
		$this->platformService = $platformService;
95
		$this->providerService = $providerService;
96
		$this->miscService = $miscService;
97
	}
98
99
100
	/**
101
	 *
102
	 */
103
	protected function configure() {
104
		parent::configure();
105
		$this->setName('fulltextsearch:live')
106
			 ->setDescription('Index files');
107
	}
108
109
110
	/**
111
	 * @param InputInterface $input
112
	 * @param OutputInterface $output
113
	 *
114
	 * @return int|null|void
115
	 * @throws Exception
116
	 */
117
	protected function execute(InputInterface $input, OutputInterface $output) {
118
119
		if ($this->configService->getCloudVersion() < 14) {
120
			throw new Exception('This feature is only available on Nextcloud 14 or newer');
121
		}
122
123
		try {
124
			$this->runner->sourceIsCommandLine($this, $output);
125
			$this->runner->start();
126
			$this->runner->output('live.');
127
128
		} catch (Exception $e) {
129
			$this->runner->exception($e->getMessage(), true);
130
			throw $e;
131
		}
132
133
		$this->indexService->setRunner($this->runner);
134
135
		$this->liveCycle();
136
		$this->runner->stop();
137
	}
138
139
140
	/**
141
	 * @throws Exception
142
	 * @throws InterruptException
143
	 * @throws TickDoesNotExistException
144
	 */
145
	private function liveCycle() {
146
147
		$platform = $this->platformService->getPlatform();
148
		$platform->setRunner($this->runner);
149
150
		while (true) {
151
152
			$indexes = $this->indexService->getQueuedIndexes();
153
154 View Code Duplication
			foreach ($indexes as $index) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
155
				$this->runner->update('indexing');
156
157
				try {
158
					$provider = $this->providerService->getProvider($index->getProviderId());
159
					$provider->setRunner($this->runner);
160
					$this->indexService->updateDocument($platform, $provider, $index);
161
				} catch (Exception $e) {
162
					$this->runner->exception($e->getMessage(), false);
163
					// TODO - upgrade error number - after too many errors, delete index
164
					// TODO - do not count error if elasticsearch is down.
165
				}
166
			}
167
168
			$this->runner->update('waiting');
169
170
			sleep(self::CYCLE_DELAY);
171
		}
172
173
		$this->runner->stop();
174
175
	}
176
177
178
}
179
180
181
182