From d3c456ee98eaa9dfb48c75b5ee11fb52fb237dc2 Mon Sep 17 00:00:00 2001 From: DariusIII Date: Wed, 24 May 2017 11:55:31 +0200 Subject: [PATCH] Change return values and logic for starting and stopping tmux scripts --- Changelog | 3 ++- misc/update/nix/tmux/start.php | 12 ++++++------ misc/update/nix/tmux/tmux-ui.php | 10 +++++----- nntmux/Tmux.php | 12 +++++++++--- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/Changelog b/Changelog index 1525823a2..d7c8cc9cf 100755 --- a/Changelog +++ b/Changelog @@ -1,5 +1,6 @@ 2017-05-24 DariusIII - * Fix: Fix user creation in admin area + * Chg: Change return values and logic for starting and stopping tmux scripts + * Fix: Fix user creation in admin area 2017-05-23 DariusIII * Chg: Update ReleaseComments and comments-list * Chg: Update DNZBFailures class diff --git a/misc/update/nix/tmux/start.php b/misc/update/nix/tmux/start.php index d043ce902..e72dafb03 100644 --- a/misc/update/nix/tmux/start.php +++ b/misc/update/nix/tmux/start.php @@ -6,11 +6,11 @@ * * It will start the tmux server and monitoring scripts if needed. */ -//require_once realpath(dirname(dirname(dirname(dirname(__DIR__)))) . DIRECTORY_SEPARATOR . 'bootstrap.php'); require_once dirname(__DIR__, 4) . DIRECTORY_SEPARATOR . 'bootstrap.php'; use nntmux\db\DB; use nntmux\Tmux; +use nntmux\ColorCLI; $pdo = new DB(); @@ -18,15 +18,15 @@ $pdo = new DB(); if (`which tmux`) { $tmux_version = trim(str_replace('tmux ', '', shell_exec('tmux -V'))); if (version_compare($tmux_version, '2.0', '>') && version_compare($tmux_version, '2.3', '<')) { - exit($pdo->log->error("tmux versions 2.1 and 2.2 are not compatible with NNTmux. Aborting\n")); + exit(ColorCLI::error('tmux versions 2.1 and 2.2 are not compatible with NNTmux. Aborting' . PHP_EOL)); } } else { - exit($pdo->log->error("tmux binary not found. Aborting\n")); + exit(ColorCLI::error('tmux binary not found. Aborting' . PHP_EOL)); } $tmux = new Tmux(); $tmux_settings = $tmux->get(); -$tmux_session = (isset($tmux_settings->tmux_session)) ? $tmux_settings->tmux_session : 0; +$tmux_session = $tmux_settings->tmux_session ?? 0; $path = __DIR__; // Set running value to on. @@ -39,7 +39,7 @@ exec('tmux new-session -ds placeholder 2>/dev/null'); $session = shell_exec("tmux list-session | grep $tmux_session"); // Kill the placeholder exec('tmux kill-session -t placeholder'); -if (count($session) == 0) { - echo $pdo->log->info("Starting the tmux server and monitor script.\n"); +if (count($session) === 0) { + echo ColorCLI::info("Starting the tmux server and monitor script.\n"); passthru("php $path/run.php"); } diff --git a/misc/update/nix/tmux/tmux-ui.php b/misc/update/nix/tmux/tmux-ui.php index 02ef4cedd..2f001e63f 100644 --- a/misc/update/nix/tmux/tmux-ui.php +++ b/misc/update/nix/tmux/tmux-ui.php @@ -29,15 +29,15 @@ Start or stop the processing of tmux scripts. This is functionally equivalent to 'tmux running' setting in admin. HELP_TEXT; -if ($argc == 1) { +if ($argc === 1) { exit($message); } -if (in_array($argv[1], $start)) { +if (in_array($argv[1], $start, false)) { passthru('php start.php'); -} else if (in_array($argv[1], $stop)) { +} else if (in_array($argv[1], $stop, false)) { passthru('php stop.php'); } else { - echo "Unrecognised command '{$argv[1]}''\n"; + echo 'Unrecognised command '. $argv[1] . PHP_EOL; exit($message); -} \ No newline at end of file +} diff --git a/nntmux/Tmux.php b/nntmux/Tmux.php index ece16c677..bea73d127 100755 --- a/nntmux/Tmux.php +++ b/nntmux/Tmux.php @@ -585,33 +585,39 @@ class Tmux if ($running === false) { throw new \RuntimeException('Tmux\\\'s running flag was not found in the database.' . PHP_EOL . 'Please check the tables are correctly setup.' . PHP_EOL); } - return ($running === 1); + if ((int)$running === 0) { + return false; + } + return true; } /** * Check if Tmux is running, if it is, stop it. * * @return bool true if scripts were running, false otherwise. + * @throws \RuntimeException * @access public */ public function stopIfRunning(): bool { - if ($this->isRunning() === 1) { + if ($this->isRunning() === true) { $this->pdo->queryExec("UPDATE tmux SET value = 0 WHERE setting = 'running'"); $sleep = $this->get()->monitor_delay; echo ColorCLI::header('Stopping tmux scripts and waiting ' . $sleep . ' seconds for all panes to shutdown'); sleep($sleep); return true; } + ColorCLI::doEcho(ColorCLI::info('Tmux scripts are not running!')); return false; } /** * @return bool|\PDOStatement + * @throws \RuntimeException */ public function startRunning() { - if (!$this->isRunning()) { + if ($this->isRunning() === false) { return $this->pdo->queryExec("UPDATE tmux SET value = 1 WHERE setting = 'running'"); } return true;