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

github.com/nextcloud/fulltextsearch.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxence Lange <maxence@artificial-owl.com>2017-12-26 15:02:40 +0300
committerMaxence Lange <maxence@artificial-owl.com>2017-12-26 15:02:40 +0300
commit480de70f60ae666a474ea623e01c9338f4d66038 (patch)
treeb98eaa45273af3aa3b3497d0c121da72ed1b7ea2 /lib/Model/Runner.php
parentca7b3d05e6f592e144ebae6f696a76cefd04f83e (diff)
nextant v2
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
Diffstat (limited to 'lib/Model/Runner.php')
-rw-r--r--lib/Model/Runner.php188
1 files changed, 188 insertions, 0 deletions
diff --git a/lib/Model/Runner.php b/lib/Model/Runner.php
new file mode 100644
index 0000000..cf6c15b
--- /dev/null
+++ b/lib/Model/Runner.php
@@ -0,0 +1,188 @@
+<?php
+/**
+ * FullNextSearch - Full Text Search your 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 2017
+ * @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\FullNextSearch\Model;
+
+use Exception;
+use OCA\FullNextSearch\Exceptions\InterruptException;
+use OCA\FullNextSearch\Exceptions\RunnerAlreadyUpException;
+use OCA\FullNextSearch\Exceptions\TickDoesNotExistException;
+use OCA\FullNextSearch\Exceptions\TickIsNotAliveException;
+use OCA\FullNextSearch\Service\RunningService;
+use Symfony\Component\Console\Output\OutputInterface;
+
+
+class Runner {
+
+
+ const TICK_TTL = 300;
+ const TICK_MINIMUM = 2;
+ const INFO_UPDATE = 10;
+
+ /** @var RunningService */
+ private $runningService;
+
+ /** @var string */
+ private $source;
+
+ /** @var int */
+ private $tickId;
+
+ /** @var ExtendedBase */
+ private $commandBase = null;
+
+ /** @var OutputInterface */
+ private $outputInterface = null;
+
+ /** @var int */
+ private $oldTick = 0;
+
+ /** @var string */
+ private $oldAction = '';
+
+ /** @var int */
+ private $ramTick = 0;
+
+ /**
+ * Runner constructor.
+ *
+ * @param RunningService $runningService
+ * @param string $source
+ */
+ public function __construct(RunningService $runningService, $source) {
+ $this->runningService = $runningService;
+ $this->source = $source;
+ }
+
+
+ /**
+ * @throws RunnerAlreadyUpException
+ * @throws Exception
+ */
+ public function start() {
+ $this->tickId = $this->runningService->start($this->source);
+ }
+
+
+ /**
+ * @param $action
+ *
+ * @throws InterruptException
+ * @throws TickDoesNotExistException
+ */
+ public function update($action) {
+
+ $tick = time();
+ try {
+ $this->hasBeenInterrupted();
+ } catch (InterruptException $e) {
+ $this->stop();
+ throw $e;
+ }
+
+ if ($this->oldAction === $action && ($this->oldTick + self::TICK_MINIMUM > $tick)) {
+ return;
+ }
+
+ try {
+ $this->runningService->update($this->tickId, $action);
+ } catch (TickIsNotAliveException $e) {
+ $this->output('Force Quit');
+ exit();
+ }
+
+ $this->updateInfo($tick);
+ $this->oldAction = $action;
+ $this->oldTick = $tick;
+ }
+
+
+ /**
+ * @throws InterruptException
+ */
+ private function hasBeenInterrupted() {
+ if ($this->commandBase === null) {
+ return;
+ }
+ $this->commandBase->hasBeenInterrupted();
+ }
+
+
+ /**
+ * @param $tick
+ */
+ private function updateInfo($tick) {
+ if (($this->ramTick + self::INFO_UPDATE) > $tick) {
+ return;
+ }
+
+ $this->output('- RAM: ' . (memory_get_usage() / 1024 / 1024));
+ $this->ramTick = $tick;
+ }
+
+
+ public function exception($reason, $stop) {
+ if (!$stop) {
+ $this->output('Exception: ' . $reason);
+ // TODO: feed an array of exceptions for log;
+ }
+ $this->runningService->exception($this->tickId, $reason, $stop);
+ }
+
+
+ /**
+ * @throws TickDoesNotExistException
+ */
+ public function stop() {
+ $this->runningService->stop($this->tickId);
+ }
+
+
+ /**
+ * @param ExtendedBase $base
+ * @param OutputInterface $output
+ */
+ public function sourceIsCommandLine(ExtendedBase $base, OutputInterface $output) {
+ $this->outputInterface = $output;
+ $this->commandBase = $base;
+ }
+
+
+ /**
+ * @param string $line
+ */
+ public function output($line) {
+ if ($this->outputInterface === null) {
+ return;
+ }
+
+ $this->outputInterface->writeln($line);
+ }
+
+
+} \ No newline at end of file