From 89a7c55dfe9032f2ecfb463ff39ba5619862a8bf Mon Sep 17 00:00:00 2001 From: Paul Kirby Date: Mon, 17 Feb 2020 22:18:26 -0600 Subject: [PATCH] Automatically try to start the Daemon --- classes/LBRYPress.php | 4 +++- classes/LBRY_Daemon.php | 47 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/classes/LBRYPress.php b/classes/LBRYPress.php index 386d2ea..5bffd67 100644 --- a/classes/LBRYPress.php +++ b/classes/LBRYPress.php @@ -122,6 +122,7 @@ class LBRYPress $this->define('LBRY_CLAIM_ID', '_lbry_claim_id'); // The Claim ID for the post as it was published on LBRY $this->define('LBRY_CANONICAL_URL', '_lbry_canonical_url'); // The canonical url for the published lbry post $this->define('LBRY_SPEECH_ASSET_URL', 'speech_asset_url'); // The meta key for an asset's speech url + $this->define('LBRY_DAEMON_PID', 'lbry_daemon_pid'); } /** @@ -184,7 +185,8 @@ class LBRYPress $option_defaults = array( LBRY_SPEECH => null, LBRY_LICENSE => $this->licenses[0], - LBRY_LBC_PUBLISH => 1 + LBRY_LBC_PUBLISH => 1, + LBRY_DAEMON_PID => false ); add_option(LBRY_SETTINGS, $option_defaults, false); diff --git a/classes/LBRY_Daemon.php b/classes/LBRY_Daemon.php index 76e7871..e618ed3 100644 --- a/classes/LBRY_Daemon.php +++ b/classes/LBRY_Daemon.php @@ -40,7 +40,7 @@ class LBRY_Daemon if (!$this->daemon_running) { $this->start_daemon(); - $this->notice->set_notice('error', 'Cannot connect to the LBRY Daemon. Attempting to start server.
If you are still having troubles, click HERE for help.'); + $this->notice->set_notice('error', 'Cannot connect to the LBRY Daemon. Attempting to start daemon server.
If you are still having troubles, click HERE for help.'); } } @@ -64,16 +64,53 @@ class LBRY_Daemon */ private function start_daemon() { - $response = popen(ABSPATH . '/lbrynet start', "w"); - error_log(print_r($response)); + // Check if a daemon start process is already running + $options = get_option(LBRY_SETTINGS); + if ($options[LBRY_DAEMON_PID]) { + if ($this->is_process_running($options[LBRY_DAEMON_PID])) { + return; + } + + $options[LBRY_DAEMON_PID] = false; + update_option(LBRY_SETTINGS, $options); + } + + // Using proc_open to set up the request + // Again, this is unix only + $cmd = ABSPATH . "lbrynet start"; + $command = $cmd . ' > /dev/null 2> /dev/null & echo $!;'; + $pid = exec($command); + + if ($pid) { + $options = get_option(LBRY_SETTINGS); + $options[LBRY_DAEMON_PID] = $pid; + update_option(LBRY_SETTINGS, $options); + } } /** - * Attempts to start the daemon + * Attempts to stop the daemon */ private function stop_daemon() { - exec(ABSPATH . '/lbrynet stop &'); + exec(ABSPATH . 'lbrynet stop > /dev/null 2> /dev/null &'); + } + + /** + * Checks to see if a PID is currently running + * @param int $pid + * @return boolean + */ + private function is_process_running($pid) + { + // This is unix specific + $lines_out = array(); + exec('ps '.(int)$pid, $lines_out); + if(count($lines_out) >= 2) { + // Process is running + return true; + } + return false; } /**