Refactor docker_load

keep publishing at a slower rate if no containers are running.

Precursor to having publish not publish if md5 hasn't changed but still honoring timeouts.
This commit is contained in:
Squidly271
2025-08-10 16:44:06 -04:00
parent 353ae247b8
commit fa5c87cc75

View File

@@ -17,34 +17,14 @@ $docroot ??= ($_SERVER['DOCUMENT_ROOT'] ?: '/usr/local/emhttp');
require_once "$docroot/webGui/include/Helpers.php";
require_once "$docroot/webGui/include/publish.php";
$noDockerStats = false;
$noDockerStatsTime = null;
$exitAfterTimeout = 120;
while (true) {
$output = shell_exec("docker stats --no-stream --format='{{.ID}};{{.CPUPerc}};{{.MemUsage}}' 2>&1");
if ( ! $noDockerStats ) {
// publish and exit the script if no listeners after the timeout
publish("dockerload", $output,true,$exitAfterTimeout);
}
publish("dockerload", $output,true);
if ( $output === null ) {
// Don't continually publish messages if no stats are available -> only publish the first time
$noDockerStats = true;
if ( ! $noDockerStatsTime ) {
$noDockerStatsTime = time();
}
// handle if no containers are running and user navigates to another page.
// publish requires a second publish attempt with no listeners in order to exit the script after the timeout has elapsed
if ( (time() - $noDockerStatsTime) > $exitAfterTimeout ) {
$noDockerStats = false;
$noDockerStatsTime = null;
}
// slow down publishing if no containers are running
sleep(10);
} else {
$noDockerStats = false;
sleep(1);
}
}