Fix Docker state drift: add 5s background state poller and make mutating operations idempotent (404/304 as success)
Some checks failed
Build & Upload tux-dock / build (push) Has been cancelled
Some checks failed
Build & Upload tux-dock / build (push) Has been cancelled
This commit is contained in:
parent
c3419524e5
commit
23a461c584
10 changed files with 465 additions and 32 deletions
59
src/docker_state_poller.cpp
Normal file
59
src/docker_state_poller.cpp
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
#include "docker_state_poller.hpp"
|
||||
|
||||
DockerStatePoller::~DockerStatePoller() {
|
||||
stop();
|
||||
}
|
||||
|
||||
void DockerStatePoller::start(const DockerManager& docker,
|
||||
PostFunction post,
|
||||
ResultCallback callback) {
|
||||
stop();
|
||||
docker_ = &docker;
|
||||
post_ = std::move(post);
|
||||
callback_ = std::move(callback);
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
stop_ = false;
|
||||
immediate_ = false;
|
||||
}
|
||||
thread_ = std::thread(&DockerStatePoller::loop, this);
|
||||
}
|
||||
|
||||
void DockerStatePoller::stop() {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
stop_ = true;
|
||||
}
|
||||
cv_.notify_all();
|
||||
if (thread_.joinable()) thread_.join();
|
||||
}
|
||||
|
||||
void DockerStatePoller::trigger_now() {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
immediate_ = true;
|
||||
}
|
||||
cv_.notify_all();
|
||||
}
|
||||
|
||||
void DockerStatePoller::loop() {
|
||||
while (true) {
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(mutex_);
|
||||
cv_.wait_for(lock, kInterval, [this] { return stop_ || immediate_; });
|
||||
if (stop_) break;
|
||||
immediate_ = false;
|
||||
}
|
||||
|
||||
if (!docker_ || !post_ || !callback_) continue;
|
||||
|
||||
auto containers = docker_->getContainerList();
|
||||
auto images = docker_->getImageList();
|
||||
|
||||
post_([callback = callback_,
|
||||
containers = std::move(containers),
|
||||
images = std::move(images)]() mutable {
|
||||
callback(std::move(containers), std::move(images));
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue