mirror of
https://github.com/unraid/webgui.git
synced 2026-02-17 14:38:58 -06:00
Refactor: Support not sending duplicate messages
Keep track of the md5 within a separate function. Avoids duplication of code on the nchan scripts, and allows for the script to still exit if no listeners without adding in more code within each script to handle this.
This commit is contained in:
@@ -20,7 +20,7 @@ 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,true);
|
||||
publish_md5("dockerload", $output,true);
|
||||
if ( $output === null ) {
|
||||
// slow down publishing if no containers are running
|
||||
sleep(10);
|
||||
|
||||
@@ -88,10 +88,7 @@ while (true) {
|
||||
if ($running < 1) $echo = "<tr><td colspan='7' style='text-align:center;padding-top:12px'>"._('No VMs running')."</td></tr>";
|
||||
|
||||
$echo = json_encode($echo);
|
||||
$md5_new = md5($echo,true);
|
||||
if ($md5_new !== $md5_old) {
|
||||
$md5_old = publish('vm_usage',$echo,true)!==false ? $md5_new : -1;
|
||||
}
|
||||
publish_md5('vm_usage',$echo,true);
|
||||
|
||||
sleep($timer);
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ function curl_socket($socket, $url, $message='') {
|
||||
// $opt3: if $opt1 is not numeric, it's a value for $abortTime.
|
||||
function publish($endpoint, $message, $opt1=1, $opt2=false, $opt3=120) {
|
||||
static $abortStart = [], $com = [], $lens = [];
|
||||
|
||||
|
||||
if ( is_file("/tmp/publishPaused") )
|
||||
return false;
|
||||
|
||||
@@ -107,6 +107,36 @@ function publish($endpoint, $message, $opt1=1, $opt2=false, $opt3=120) {
|
||||
return $reply;
|
||||
}
|
||||
|
||||
// Function to not continually republish the same message if it hasn't changed since the last publish
|
||||
function publish_md5($endpoint, $message, $opt1=1, $opt2=false, $opt3=120) {
|
||||
static $md5_old = [];
|
||||
static $md5_time = [];
|
||||
|
||||
if ( is_numeric($opt1) ) {
|
||||
$timeout = $opt3;
|
||||
$abort = $opt2;
|
||||
} else {
|
||||
$abort = $opt1;
|
||||
$timeout = $opt2 ?: $opt3;
|
||||
}
|
||||
|
||||
// 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 ( (time() - ($md5_time[$endpoint]??0)) > $timeout ) {
|
||||
$md5_old[$endpoint] = null;
|
||||
}
|
||||
}
|
||||
|
||||
$md5_new = $message ? md5($message,true) : -1 ;
|
||||
if ($md5_new !== ($md5_old[$endpoint]??null)) {
|
||||
$md5_old[$endpoint] = $md5_new;
|
||||
$md5_time[$endpoint] = time();
|
||||
|
||||
return publish($endpoint, $message, $opt1, $opt2, $opt3);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Removes the script calling this function from nchan.pid
|
||||
function removeNChanScript() {
|
||||
global $docroot, $argv;
|
||||
|
||||
@@ -123,10 +123,8 @@ while (true) {
|
||||
$count = array_count_values($lsof ?? array());
|
||||
foreach ($share as $name) $echo['stream'][] = $count[$name]??0;
|
||||
$echo = json_encode($echo);
|
||||
$md5_new = md5($echo,true);
|
||||
if ($md5_new !== $md5_old) {
|
||||
$md5_old = publish('update1',$echo,true)!==false ? $md5_new : -1;
|
||||
}
|
||||
publish_md5('update1',$echo,true);
|
||||
|
||||
sleep(5);
|
||||
}
|
||||
?>
|
||||
|
||||
@@ -500,10 +500,7 @@ while (true) {
|
||||
}
|
||||
|
||||
$echo = json_encode($echo);
|
||||
$md5_new = md5($echo,true);
|
||||
if ($md5_new !== $md5_old) {
|
||||
$md5_old = publish('update2',$echo,true)!==false ? $md5_new : -1;
|
||||
}
|
||||
publish_md5('update2',$echo,true);
|
||||
sleep(2);
|
||||
}
|
||||
?>
|
||||
|
||||
@@ -160,11 +160,8 @@ while (true) {
|
||||
$echo['time'] = [$clock,_($date,0)];
|
||||
|
||||
$echo = json_encode($echo);
|
||||
$md5_new = md5($echo,true);
|
||||
if ($md5_new !== $md5_old) {
|
||||
$md5_old = publish('update3',$echo,true)!==false ? $md5_new : -1;
|
||||
$time0 = $time1;
|
||||
}
|
||||
publish_md5('update3',$echo,true);
|
||||
|
||||
sleep(1);
|
||||
$time1 = microtime(true);
|
||||
}
|
||||
|
||||
@@ -132,10 +132,9 @@ 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];
|
||||
}
|
||||
$echo = json_encode($echo);
|
||||
$md5_new = md5($echo,true);
|
||||
if ($md5_new !== $md5_old) {
|
||||
$md5_old = publish('apcups',$echo,true)!==false ? $md5_new : -1;
|
||||
}
|
||||
|
||||
publish_md5('apcups',$echo,true);
|
||||
|
||||
sleep(3);
|
||||
}
|
||||
?>
|
||||
|
||||
@@ -38,10 +38,7 @@ while (true) {
|
||||
if (count($row)>5) $echo[] = [$row[0], $row[5]?$now-$row[5]:0, my_scale($row[6],$unit)." $unit", my_scale($row[7],$unit)." $unit"];
|
||||
}
|
||||
$echo = json_encode($echo);
|
||||
$md5_new = md5($echo,true);
|
||||
if ($md5_new !== $md5_old) {
|
||||
$md5_old = publish('wireguard',$echo,true)!==false ? $md5_new : -1;
|
||||
}
|
||||
publish_md5('wireguard',$echo,true);
|
||||
sleep(1);
|
||||
}
|
||||
?>
|
||||
|
||||
@@ -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',json_encode($echo),1,true);
|
||||
publish_md5('wlan0',json_encode($echo),true);
|
||||
sleep(3);
|
||||
}
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user