Skip to content

Commit b9baa46

Browse files
authored
Merge pull request #2338 from Squidly271/patch-8
Fixed: Multiple Tabs on Main would cause issues on parity sync changes
2 parents c0a544f + 680363d commit b9baa46

File tree

18 files changed

+59
-43
lines changed

18 files changed

+59
-43
lines changed

emhttp/plugins/dynamix.docker.manager/nchan/docker_load

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ require_once "$docroot/webGui/include/publish.php";
1919
while (true) {
2020
$output = shell_exec("docker stats --no-stream --format='{{.ID}};{{.CPUPerc}};{{.MemUsage}}' 2>&1");
2121

22-
publish("dockerload", $output);
22+
// continually publish and abort if no listeners
23+
publish("dockerload", $output,1,true);
2324
if ( $output === null ) {
2425
// slow down publishing if no containers are running
2526
sleep(10);

emhttp/plugins/dynamix.vm.manager/VMUsageStats.page

100644100755
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ vmusage.on('message', function(msg){
3232
});
3333

3434
$(function(){
35+
// ping subscriber to check for listeners
36+
var vmPing = new NchanSubscriber('/sub/vmPing',{subscriber:'websocket', reconnectTimeout:5000});
37+
vmPing.on('message', function(msg){});
38+
vmPing.start();
39+
3540
vmusage.start();
3641
});
3742

emhttp/plugins/dynamix.vm.manager/nchan/vm_usage

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ while (true) {
8787
}
8888
if ($running < 1) $echo = "<tr><td colspan='7' style='text-align:center;padding-top:12px'>"._('No VMs running')."</td></tr>";
8989

90-
publish('vm_usage',$echo);
91-
90+
publish_noDupe('vm_usage',json_encode($echo));
91+
ping('vmPing');
92+
9293
sleep($timer);
9394
}
9495
?>

emhttp/plugins/dynamix/ArrayOperation.page

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ setTimeout(function(){paritymonitor.start();},5000);
506506
<?endif;?>
507507

508508
$(function(){
509-
// Create dummy listeners for parity monitor and device info to monitor for listeners
509+
// Create ping listener for parity monitor and device info to monitor for listeners
510510
var mainPingListener = new NchanSubscriber('/sub/mainPingListener',{subscriber:'websocket', reconnectTimeout:5000});
511511
mainPingListener.on('message', function(msg){});
512512
mainPingListener.start();

emhttp/plugins/dynamix/DashStats.page

100644100755
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2411,6 +2411,11 @@ apcups.on('message',function(msg) {
24112411
<?endif;?>
24122412

24132413
$(function() {
2414+
// ping subscriber to check for listeners
2415+
var dashboardPing = new NchanSubscriber('/sub/dashboardPing',{subscriber:'websocket', reconnectTimeout:5000});
2416+
dashboardPing.on('message', function(msg){});
2417+
dashboardPing.start();
2418+
24142419
initCharts();
24152420
cpuchart.render();
24162421
netchart.render();

emhttp/plugins/dynamix/include/publish.php

100644100755
Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,17 @@ function curl_socket($socket, $url, $message='') {
2525
return $reply;
2626
}
2727

28-
// $endpoint: the name of the endpoint to publish to
29-
// $message: the message to publish (if an array, it will be converted to a JSON string)
28+
// $endpoint: the name of the endpoint to publish to (string)
29+
// $message: the message to publish (string)
3030
// $len: the length of the buffer (default 1)
3131
// $abort: if true, the script will exit if the endpoint is without subscribers on the next publish attempt after $abortTime seconds (default true)
3232
// $abortTime: the time in seconds to wait before exiting the script if the endpoint is without subscribers (default 30)
33-
function publish($endpoint, $message, $len=1, $abort=true, $abortTime=30) {
33+
function publish($endpoint, $message, $len=1, $abort=false, $abortTime=30) {
3434
static $abortStart = [], $com = [], $lens = [];
3535

3636
if ( is_file("/tmp/publishPaused") )
3737
return false;
3838

39-
if ( is_array($message) ) {
40-
$message = json_encode($message);
41-
}
4239

4340
// Check for the unlikely case of a buffer length change
4441
if ( (($lens[$endpoint] ?? 1) !== $len) && isset($com[$endpoint]) ) {
@@ -85,9 +82,8 @@ function publish($endpoint, $message, $len=1, $abort=true, $abortTime=30) {
8582
if ( ! ($abortStart[$endpoint]??false) )
8683
$abortStart[$endpoint] = time();
8784
if ( (time() - $abortStart[$endpoint]) > $abortTime) {
88-
my_logger("$endpoint timed out after $abortTime seconds. Exiting.", 'publish');
89-
90-
removeNChanScript();
85+
$script = removeNChanScript();
86+
my_logger("$script timed out after $abortTime seconds. Exiting.", 'publish');
9187
exit();
9288
}
9389
$reply = false; // if no subscribers, force return value to false
@@ -100,7 +96,7 @@ function publish($endpoint, $message, $len=1, $abort=true, $abortTime=30) {
10096
}
10197

10298
// Function to not continually republish the same message if it hasn't changed since the last publish
103-
function publish_noDupe($endpoint, $message, $len=1, $abort=true, $abortTime=30) {
99+
function publish_noDupe($endpoint, $message, $noListenerAbort=false, $abortTime=30) {
104100
static $msg_old = [];
105101
static $msg_time = [];
106102
static $listener = [];
@@ -109,12 +105,8 @@ function publish_noDupe($endpoint, $message, $len=1, $abort=true, $abortTime=30)
109105
$listener[$endpoint] = false;
110106
}
111107

112-
if ( is_array($message) ) {
113-
$message = json_encode($message);
114-
}
115-
116108
// if abort is set, republish the message even if it hasn't changed after $timeout seconds to check for subscribers and exit accordingly
117-
if ( $abort ) {
109+
if ( $noListenerAbort ) {
118110
if ( (time() - ($msg_time[$endpoint]??0)) > $abortTime ) {
119111
$msg_old[$endpoint] = null;
120112
}
@@ -124,7 +116,7 @@ function publish_noDupe($endpoint, $message, $len=1, $abort=true, $abortTime=30)
124116
$msg_old[$endpoint] = $message;
125117
$msg_time[$endpoint] = time();
126118

127-
$return = publish($endpoint, $message, $len, $abort, $abortTime);
119+
$return = publish($endpoint, $message,1,$noListenerAbort);
128120

129121
// if no listener, keep publishing whether or not its the same message.
130122
$listener[$endpoint] = $return ? true : false;
@@ -133,6 +125,10 @@ function publish_noDupe($endpoint, $message, $len=1, $abort=true, $abortTime=30)
133125
}
134126
}
135127

128+
// Wrapper to publish a ping message to the endpoint with occasional republishing and checking if anyone is listening
129+
function ping($endpoint) {
130+
publish_noDupe($endpoint,"ping",true);
131+
}
136132

137133
// Removes the script calling this function from nchan.pid
138134
function removeNChanScript() {
@@ -148,5 +144,6 @@ function removeNChanScript() {
148144
} else {
149145
@unlink("/var/run/nchan.pid");
150146
}
147+
return $script;
151148
}
152149
?>

emhttp/plugins/dynamix/nchan/device_list

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -567,12 +567,12 @@ while (true) {
567567
$echo['stop'] = _var($var,'fsState')=='Stopped' ? 1 : 0;
568568

569569
// publish without a timeout for no listeners
570-
publish('devices',$echo,1,false);
570+
publish('devices',json_encode($echo));
571571
$fs_new = _var($var,'fsState')=='Started' ? 1 : 0;
572-
publish('arraymonitor',$fs_new,1,false);
572+
publish('arraymonitor',$fs_new);
573573

574-
//publish to the dummy listener with the timeout to see if there are any subscribers
575-
publish('mainPingListener',"ping");
574+
// ping the page so we can terminate if nobody is listening
575+
ping('mainPingListener');
576576
sleep(1);
577577
}
578578
?>

emhttp/plugins/dynamix/nchan/file_manager

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,10 @@ while (true) {
252252
}
253253
if (time() - $timer) {
254254
// update every second
255-
publish('filemonitor', file_exists($active) ? 1 : 0,1,false);
255+
publish('filemonitor', file_exists($active) ? 1 : 0);
256256
$timer = time();
257257
}
258-
publish('filemanager', json_encode($reply),1,false);
258+
publish('filemanager', json_encode($reply));
259259
usleep(250000);
260260
}
261261
?>

emhttp/plugins/dynamix/nchan/notify_poller

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ $md5_old = -1;
1818

1919
require_once "$docroot/webGui/include/publish.php";
2020

21+
// Not necessary to check for listeners. This will automatically exit if there are no listeners anywhere
2122
while (true) {
2223
$echo = shell_exec("$notify get");
2324
if ( $echo ) {

emhttp/plugins/dynamix/nchan/parity_list

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,14 @@ while (true) {
147147
$process = 0;
148148
}
149149

150-
// publish without a timeout for no listeners
151-
publish('parity', $echo,1,false);
152-
publish('paritymonitor', $spot > 0 ? 1 : 0,1,false);
153-
publish('fsState', $fsState,1,false);
154-
publish('mymonitor', $process,1,false);
155-
156-
//publish to the dummy listener with the timeoutto see if there are any subscribers
157-
publish('mainPingListener',"ping");
150+
// publish but don't republish duplicate messages
151+
publish_noDupe('parity', json_encode($echo));
152+
publish_noDupe('paritymonitor', $spot > 0 ? 1 : 0);
153+
publish_noDupe('fsState', $fsState);
154+
publish_noDupe('mymonitor', $process);
155+
156+
// ping the page so we can terminate if nobody is listening
157+
ping("mainPingListener");
158158

159159
sleep(3);
160160
}

0 commit comments

Comments
 (0)