From 680363d54a9e0c8e7a44d6d153b70c7b1c99e97b Mon Sep 17 00:00:00 2001 From: Squidly271 Date: Thu, 14 Aug 2025 19:30:56 -0400 Subject: [PATCH] Fixed: Multiple Tabs on Main would cause issues on parity sync changes --- .../dynamix.docker.manager/nchan/docker_load | 3 +- .../dynamix.vm.manager/VMUsageStats.page | 5 ++++ .../plugins/dynamix.vm.manager/nchan/vm_usage | 5 ++-- emhttp/plugins/dynamix/ArrayOperation.page | 2 +- emhttp/plugins/dynamix/DashStats.page | 5 ++++ emhttp/plugins/dynamix/include/publish.php | 29 +++++++++---------- emhttp/plugins/dynamix/nchan/device_list | 8 ++--- emhttp/plugins/dynamix/nchan/file_manager | 4 +-- emhttp/plugins/dynamix/nchan/notify_poller | 1 + emhttp/plugins/dynamix/nchan/parity_list | 14 ++++----- emhttp/plugins/dynamix/nchan/session_check | 1 + emhttp/plugins/dynamix/nchan/update_1 | 4 +-- emhttp/plugins/dynamix/nchan/update_2 | 3 +- emhttp/plugins/dynamix/nchan/update_3 | 5 ++-- emhttp/plugins/dynamix/nchan/ups_status | 3 +- emhttp/plugins/dynamix/nchan/vm_dashusage | 3 +- emhttp/plugins/dynamix/nchan/wg_poller | 3 +- emhttp/plugins/dynamix/nchan/wlan0 | 2 +- 18 files changed, 58 insertions(+), 42 deletions(-) mode change 100644 => 100755 emhttp/plugins/dynamix.vm.manager/VMUsageStats.page mode change 100644 => 100755 emhttp/plugins/dynamix/ArrayOperation.page mode change 100644 => 100755 emhttp/plugins/dynamix/DashStats.page mode change 100644 => 100755 emhttp/plugins/dynamix/include/publish.php diff --git a/emhttp/plugins/dynamix.docker.manager/nchan/docker_load b/emhttp/plugins/dynamix.docker.manager/nchan/docker_load index 85940bbd8..f5e99b984 100755 --- a/emhttp/plugins/dynamix.docker.manager/nchan/docker_load +++ b/emhttp/plugins/dynamix.docker.manager/nchan/docker_load @@ -19,7 +19,8 @@ require_once "$docroot/webGui/include/publish.php"; while (true) { $output = shell_exec("docker stats --no-stream --format='{{.ID}};{{.CPUPerc}};{{.MemUsage}}' 2>&1"); - publish("dockerload", $output); + // continually publish and abort if no listeners + publish("dockerload", $output,1,true); if ( $output === null ) { // slow down publishing if no containers are running sleep(10); diff --git a/emhttp/plugins/dynamix.vm.manager/VMUsageStats.page b/emhttp/plugins/dynamix.vm.manager/VMUsageStats.page old mode 100644 new mode 100755 index 2b072c488..14ad65693 --- a/emhttp/plugins/dynamix.vm.manager/VMUsageStats.page +++ b/emhttp/plugins/dynamix.vm.manager/VMUsageStats.page @@ -32,6 +32,11 @@ vmusage.on('message', function(msg){ }); $(function(){ + // ping subscriber to check for listeners + var vmPing = new NchanSubscriber('/sub/vmPing',{subscriber:'websocket', reconnectTimeout:5000}); + vmPing.on('message', function(msg){}); + vmPing.start(); + vmusage.start(); }); diff --git a/emhttp/plugins/dynamix.vm.manager/nchan/vm_usage b/emhttp/plugins/dynamix.vm.manager/nchan/vm_usage index 27a0cd902..c4bb8f5a7 100755 --- a/emhttp/plugins/dynamix.vm.manager/nchan/vm_usage +++ b/emhttp/plugins/dynamix.vm.manager/nchan/vm_usage @@ -87,8 +87,9 @@ while (true) { } if ($running < 1) $echo = ""._('No VMs running').""; - publish('vm_usage',$echo); - + publish_noDupe('vm_usage',json_encode($echo)); + ping('vmPing'); + sleep($timer); } ?> diff --git a/emhttp/plugins/dynamix/ArrayOperation.page b/emhttp/plugins/dynamix/ArrayOperation.page old mode 100644 new mode 100755 index 8195ba066..74381e295 --- a/emhttp/plugins/dynamix/ArrayOperation.page +++ b/emhttp/plugins/dynamix/ArrayOperation.page @@ -506,7 +506,7 @@ setTimeout(function(){paritymonitor.start();},5000); $(function(){ -// Create dummy listeners for parity monitor and device info to monitor for listeners +// Create ping listener for parity monitor and device info to monitor for listeners var mainPingListener = new NchanSubscriber('/sub/mainPingListener',{subscriber:'websocket', reconnectTimeout:5000}); mainPingListener.on('message', function(msg){}); mainPingListener.start(); diff --git a/emhttp/plugins/dynamix/DashStats.page b/emhttp/plugins/dynamix/DashStats.page old mode 100644 new mode 100755 index be79f6d9f..64bada24d --- a/emhttp/plugins/dynamix/DashStats.page +++ b/emhttp/plugins/dynamix/DashStats.page @@ -2411,6 +2411,11 @@ apcups.on('message',function(msg) { $(function() { + // ping subscriber to check for listeners + var dashboardPing = new NchanSubscriber('/sub/dashboardPing',{subscriber:'websocket', reconnectTimeout:5000}); + dashboardPing.on('message', function(msg){}); + dashboardPing.start(); + initCharts(); cpuchart.render(); netchart.render(); diff --git a/emhttp/plugins/dynamix/include/publish.php b/emhttp/plugins/dynamix/include/publish.php old mode 100644 new mode 100755 index 08571beff..880f941f6 --- a/emhttp/plugins/dynamix/include/publish.php +++ b/emhttp/plugins/dynamix/include/publish.php @@ -25,20 +25,17 @@ function curl_socket($socket, $url, $message='') { return $reply; } -// $endpoint: the name of the endpoint to publish to -// $message: the message to publish (if an array, it will be converted to a JSON string) +// $endpoint: the name of the endpoint to publish to (string) +// $message: the message to publish (string) // $len: the length of the buffer (default 1) // $abort: if true, the script will exit if the endpoint is without subscribers on the next publish attempt after $abortTime seconds (default true) // $abortTime: the time in seconds to wait before exiting the script if the endpoint is without subscribers (default 30) -function publish($endpoint, $message, $len=1, $abort=true, $abortTime=30) { +function publish($endpoint, $message, $len=1, $abort=false, $abortTime=30) { static $abortStart = [], $com = [], $lens = []; if ( is_file("/tmp/publishPaused") ) return false; - if ( is_array($message) ) { - $message = json_encode($message); - } // Check for the unlikely case of a buffer length change if ( (($lens[$endpoint] ?? 1) !== $len) && isset($com[$endpoint]) ) { @@ -85,9 +82,8 @@ function publish($endpoint, $message, $len=1, $abort=true, $abortTime=30) { if ( ! ($abortStart[$endpoint]??false) ) $abortStart[$endpoint] = time(); if ( (time() - $abortStart[$endpoint]) > $abortTime) { - my_logger("$endpoint timed out after $abortTime seconds. Exiting.", 'publish'); - - removeNChanScript(); + $script = removeNChanScript(); + my_logger("$script timed out after $abortTime seconds. Exiting.", 'publish'); exit(); } $reply = false; // if no subscribers, force return value to false @@ -100,7 +96,7 @@ function publish($endpoint, $message, $len=1, $abort=true, $abortTime=30) { } // Function to not continually republish the same message if it hasn't changed since the last publish -function publish_noDupe($endpoint, $message, $len=1, $abort=true, $abortTime=30) { +function publish_noDupe($endpoint, $message, $noListenerAbort=false, $abortTime=30) { static $msg_old = []; static $msg_time = []; static $listener = []; @@ -109,12 +105,8 @@ function publish_noDupe($endpoint, $message, $len=1, $abort=true, $abortTime=30) $listener[$endpoint] = false; } - if ( is_array($message) ) { - $message = json_encode($message); - } - // if abort is set, republish the message even if it hasn't changed after $timeout seconds to check for subscribers and exit accordingly - if ( $abort ) { + if ( $noListenerAbort ) { if ( (time() - ($msg_time[$endpoint]??0)) > $abortTime ) { $msg_old[$endpoint] = null; } @@ -124,7 +116,7 @@ function publish_noDupe($endpoint, $message, $len=1, $abort=true, $abortTime=30) $msg_old[$endpoint] = $message; $msg_time[$endpoint] = time(); - $return = publish($endpoint, $message, $len, $abort, $abortTime); + $return = publish($endpoint, $message,1,$noListenerAbort); // if no listener, keep publishing whether or not its the same message. $listener[$endpoint] = $return ? true : false; @@ -133,6 +125,10 @@ function publish_noDupe($endpoint, $message, $len=1, $abort=true, $abortTime=30) } } +// Wrapper to publish a ping message to the endpoint with occasional republishing and checking if anyone is listening +function ping($endpoint) { + publish_noDupe($endpoint,"ping",true); +} // Removes the script calling this function from nchan.pid function removeNChanScript() { @@ -148,5 +144,6 @@ function removeNChanScript() { } else { @unlink("/var/run/nchan.pid"); } + return $script; } ?> diff --git a/emhttp/plugins/dynamix/nchan/device_list b/emhttp/plugins/dynamix/nchan/device_list index 12afd2e7f..6302f04fb 100755 --- a/emhttp/plugins/dynamix/nchan/device_list +++ b/emhttp/plugins/dynamix/nchan/device_list @@ -567,12 +567,12 @@ while (true) { $echo['stop'] = _var($var,'fsState')=='Stopped' ? 1 : 0; // publish without a timeout for no listeners - publish('devices',$echo,1,false); + publish('devices',json_encode($echo)); $fs_new = _var($var,'fsState')=='Started' ? 1 : 0; - publish('arraymonitor',$fs_new,1,false); + publish('arraymonitor',$fs_new); - //publish to the dummy listener with the timeout to see if there are any subscribers - publish('mainPingListener',"ping"); + // ping the page so we can terminate if nobody is listening + ping('mainPingListener'); sleep(1); } ?> diff --git a/emhttp/plugins/dynamix/nchan/file_manager b/emhttp/plugins/dynamix/nchan/file_manager index 94896a4f2..076a6b09d 100755 --- a/emhttp/plugins/dynamix/nchan/file_manager +++ b/emhttp/plugins/dynamix/nchan/file_manager @@ -252,10 +252,10 @@ while (true) { } if (time() - $timer) { // update every second - publish('filemonitor', file_exists($active) ? 1 : 0,1,false); + publish('filemonitor', file_exists($active) ? 1 : 0); $timer = time(); } - publish('filemanager', json_encode($reply),1,false); + publish('filemanager', json_encode($reply)); usleep(250000); } ?> diff --git a/emhttp/plugins/dynamix/nchan/notify_poller b/emhttp/plugins/dynamix/nchan/notify_poller index 46a1b9195..299017010 100755 --- a/emhttp/plugins/dynamix/nchan/notify_poller +++ b/emhttp/plugins/dynamix/nchan/notify_poller @@ -18,6 +18,7 @@ $md5_old = -1; require_once "$docroot/webGui/include/publish.php"; +// Not necessary to check for listeners. This will automatically exit if there are no listeners anywhere while (true) { $echo = shell_exec("$notify get"); if ( $echo ) { diff --git a/emhttp/plugins/dynamix/nchan/parity_list b/emhttp/plugins/dynamix/nchan/parity_list index b1aa567e1..d503498e1 100755 --- a/emhttp/plugins/dynamix/nchan/parity_list +++ b/emhttp/plugins/dynamix/nchan/parity_list @@ -147,14 +147,14 @@ while (true) { $process = 0; } - // publish without a timeout for no listeners - publish('parity', $echo,1,false); - publish('paritymonitor', $spot > 0 ? 1 : 0,1,false); - publish('fsState', $fsState,1,false); - publish('mymonitor', $process,1,false); + // publish but don't republish duplicate messages + publish_noDupe('parity', json_encode($echo)); + publish_noDupe('paritymonitor', $spot > 0 ? 1 : 0); + publish_noDupe('fsState', $fsState); + publish_noDupe('mymonitor', $process); - //publish to the dummy listener with the timeoutto see if there are any subscribers - publish('mainPingListener',"ping"); + // ping the page so we can terminate if nobody is listening + ping("mainPingListener"); sleep(3); } diff --git a/emhttp/plugins/dynamix/nchan/session_check b/emhttp/plugins/dynamix/nchan/session_check index 9f8750f68..b0bf0ca44 100755 --- a/emhttp/plugins/dynamix/nchan/session_check +++ b/emhttp/plugins/dynamix/nchan/session_check @@ -18,6 +18,7 @@ $varroot = '/var/local/emhttp'; require_once "$docroot/webGui/include/publish.php"; require_once "$docroot/webGui/include/Wrappers.php"; +// Not necessary to check for listeners. This will automatically exit if there are no listeners anywhere $csrf_old = ''; while (true) { $csrf_new = @parse_ini_file("$varroot/var.ini")['csrf_token']; diff --git a/emhttp/plugins/dynamix/nchan/update_1 b/emhttp/plugins/dynamix/nchan/update_1 index 25d036508..a30199a22 100755 --- a/emhttp/plugins/dynamix/nchan/update_1 +++ b/emhttp/plugins/dynamix/nchan/update_1 @@ -123,8 +123,8 @@ while (true) { $count = array_count_values($lsof ?? array()); foreach ($share as $name) $echo['stream'][] = $count[$name]??0; - publish('update1',$echo); - + publish_noDupe('update1',json_encode($echo)); + ping('dashboardPing'); sleep(5); } ?> diff --git a/emhttp/plugins/dynamix/nchan/update_2 b/emhttp/plugins/dynamix/nchan/update_2 index ef7532903..543034b45 100755 --- a/emhttp/plugins/dynamix/nchan/update_2 +++ b/emhttp/plugins/dynamix/nchan/update_2 @@ -499,7 +499,8 @@ while (true) { $echo[$a] = [implode($echo[$a]), $extra]; } - publish('update2',$echo); + publish_noDupe('update2',json_encode($echo)); + ping('dashboardPing'); sleep(2); } ?> diff --git a/emhttp/plugins/dynamix/nchan/update_3 b/emhttp/plugins/dynamix/nchan/update_3 index 8e13282b5..24819530f 100755 --- a/emhttp/plugins/dynamix/nchan/update_3 +++ b/emhttp/plugins/dynamix/nchan/update_3 @@ -159,8 +159,9 @@ while (true) { $date = my_date($xdate ? 'D j M Y, T' : $display['date'].', T',$now); $echo['time'] = [$clock,_($date,0)]; - publish('update3',$echo); - + publish_noDupe('update3',json_encode($echo)); + ping('dashboardPing'); + sleep(1); $time1 = microtime(true); } diff --git a/emhttp/plugins/dynamix/nchan/ups_status b/emhttp/plugins/dynamix/nchan/ups_status index 518620e04..a3cde1865 100755 --- a/emhttp/plugins/dynamix/nchan/ups_status +++ b/emhttp/plugins/dynamix/nchan/ups_status @@ -132,7 +132,8 @@ while (true) { $echo[6] = isset($output) ? ((empty($volt) || ($minv<$output && $output<$maxv) ? "" : "").$echo[6].(isset($freq) ? " ~ $freq Hz" : "")."") : $echo[6]; } - publish('apcups',$echo); + publish_noDupe('apcups',json_encode($echo)); + ping('dashboardPing'); sleep(3); } diff --git a/emhttp/plugins/dynamix/nchan/vm_dashusage b/emhttp/plugins/dynamix/nchan/vm_dashusage index 0c0097447..26efac025 100755 --- a/emhttp/plugins/dynamix/nchan/vm_dashusage +++ b/emhttp/plugins/dynamix/nchan/vm_dashusage @@ -109,7 +109,8 @@ while (true) { } } - publish('vm_dashusage',$echo); + publish_noDupe('vm_dashusage',json_encode($echo)); + ping('dashboardPing'); sleep($timer); } diff --git a/emhttp/plugins/dynamix/nchan/wg_poller b/emhttp/plugins/dynamix/nchan/wg_poller index 7259cdaf0..739d754fd 100755 --- a/emhttp/plugins/dynamix/nchan/wg_poller +++ b/emhttp/plugins/dynamix/nchan/wg_poller @@ -36,7 +36,8 @@ while (true) { $row = preg_split('/\s+/',$row); if (count($row)>5) $echo[] = [$row[0], $row[5]?$now-$row[5]:0, wg_scale($row[6],$unit)." $unit", wg_scale($row[7],$unit)." $unit"]; } - publish('wireguard',$echo); + publish_noDupe('wireguard',json_encode($echo)); + ping('dashboardPing'); sleep(1); } ?> diff --git a/emhttp/plugins/dynamix/nchan/wlan0 b/emhttp/plugins/dynamix/nchan/wlan0 index 1e87700aa..c710646e4 100755 --- a/emhttp/plugins/dynamix/nchan/wlan0 +++ b/emhttp/plugins/dynamix/nchan/wlan0 @@ -66,7 +66,7 @@ while (true) { } } // Short of closing all web pages, the timeout should only happen if wifi is enabled and then disabled - publish('wlan0',$echo); + publish('wlan0',json_encode($echo)); sleep(3); } ?>