Merge pull request #2338 from Squidly271/patch-8

Fixed: Multiple Tabs on Main would cause issues on parity sync changes
This commit is contained in:
tom mortensen
2025-08-15 10:03:20 -07:00
committed by GitHub
18 changed files with 58 additions and 42 deletions

View File

@@ -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);

5
emhttp/plugins/dynamix.vm.manager/VMUsageStats.page Normal file → Executable file
View File

@@ -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();
});

View File

@@ -87,8 +87,9 @@ while (true) {
}
if ($running < 1) $echo = "<tr><td colspan='7' style='text-align:center;padding-top:12px'>"._('No VMs running')."</td></tr>";
publish('vm_usage',$echo);
publish_noDupe('vm_usage',json_encode($echo));
ping('vmPing');
sleep($timer);
}
?>

2
emhttp/plugins/dynamix/ArrayOperation.page Normal file → Executable file
View File

@@ -506,7 +506,7 @@ setTimeout(function(){paritymonitor.start();},5000);
<?endif;?>
$(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();

5
emhttp/plugins/dynamix/DashStats.page Normal file → Executable file
View File

@@ -2411,6 +2411,11 @@ apcups.on('message',function(msg) {
<?endif;?>
$(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();

29
emhttp/plugins/dynamix/include/publish.php Normal file → Executable file
View File

@@ -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;
}
?>

View File

@@ -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);
}
?>

View File

@@ -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);
}
?>

View File

@@ -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 ) {

View File

@@ -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);
}

View File

@@ -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'];

View File

@@ -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);
}
?>

View File

@@ -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);
}
?>

View File

@@ -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);
}

View File

@@ -132,7 +132,8 @@ while (true) {
$echo[6] = isset($output) ? ((empty($volt) || ($minv<$output && $output<$maxv) ? "<span $green>" : "<span $red>").$echo[6].(isset($freq) ? " ~ $freq Hz" : "")."</span>") : $echo[6];
}
publish('apcups',$echo);
publish_noDupe('apcups',json_encode($echo));
ping('dashboardPing');
sleep(3);
}

View File

@@ -109,7 +109,8 @@ while (true) {
}
}
publish('vm_dashusage',$echo);
publish_noDupe('vm_dashusage',json_encode($echo));
ping('dashboardPing');
sleep($timer);
}

View File

@@ -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);
}
?>

View File

@@ -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);
}
?>