feat: spawnId for docker hashmap
Use custom enum for docker spawns id, so logs & stats can exist in hashmap at same time
This commit is contained in:
+38
-15
@@ -9,7 +9,7 @@ use std::{
|
|||||||
sync::{
|
sync::{
|
||||||
atomic::{AtomicBool, Ordering},
|
atomic::{AtomicBool, Ordering},
|
||||||
Arc,
|
Arc,
|
||||||
},
|
}, fmt,
|
||||||
};
|
};
|
||||||
use tokio::{sync::mpsc::Receiver, task::JoinHandle};
|
use tokio::{sync::mpsc::Receiver, task::JoinHandle};
|
||||||
|
|
||||||
@@ -22,6 +22,22 @@ use crate::{
|
|||||||
mod message;
|
mod message;
|
||||||
pub use message::DockerMessage;
|
pub use message::DockerMessage;
|
||||||
|
|
||||||
|
#[derive(Debug, Hash, Clone, PartialEq, Eq)]
|
||||||
|
enum SpawnId{
|
||||||
|
Stats(String),
|
||||||
|
Log(String)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for SpawnId {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
let disp = match self {
|
||||||
|
Self::Stats(id) => format!("stats::{id}"),
|
||||||
|
Self::Log(id) => format!("logs::{id}")
|
||||||
|
};
|
||||||
|
write!(f, "{}", disp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct DockerData {
|
pub struct DockerData {
|
||||||
app_data: Arc<Mutex<AppData>>,
|
app_data: Arc<Mutex<AppData>>,
|
||||||
docker: Arc<Docker>,
|
docker: Arc<Docker>,
|
||||||
@@ -29,7 +45,7 @@ pub struct DockerData {
|
|||||||
initialised: bool,
|
initialised: bool,
|
||||||
is_running: Arc<AtomicBool>,
|
is_running: Arc<AtomicBool>,
|
||||||
receiver: Receiver<DockerMessage>,
|
receiver: Receiver<DockerMessage>,
|
||||||
spawns: Arc<Mutex<HashMap<String, JoinHandle<()>>>>,
|
spawns: Arc<Mutex<HashMap<SpawnId, JoinHandle<()>>>>,
|
||||||
timestamps: bool,
|
timestamps: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,7 +85,7 @@ impl DockerData {
|
|||||||
id: String,
|
id: String,
|
||||||
app_data: Arc<Mutex<AppData>>,
|
app_data: Arc<Mutex<AppData>>,
|
||||||
is_running: bool,
|
is_running: bool,
|
||||||
spawns: Arc<Mutex<HashMap<String, JoinHandle<()>>>>,
|
spawns: Arc<Mutex<HashMap<SpawnId, JoinHandle<()>>>>,
|
||||||
) {
|
) {
|
||||||
let mut stream = docker
|
let mut stream = docker
|
||||||
.stats(
|
.stats(
|
||||||
@@ -117,7 +133,8 @@ impl DockerData {
|
|||||||
.lock()
|
.lock()
|
||||||
.update_stats(id.clone(), None, None, mem_limit, rx, tx);
|
.update_stats(id.clone(), None, None, mem_limit, rx, tx);
|
||||||
}
|
}
|
||||||
spawns.lock().remove(&id);
|
let key = SpawnId::Stats(id.to_owned());
|
||||||
|
spawns.lock().remove(&key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,7 +147,8 @@ impl DockerData {
|
|||||||
let is_running = *is_running;
|
let is_running = *is_running;
|
||||||
let id = id.to_owned();
|
let id = id.to_owned();
|
||||||
|
|
||||||
let spawn_contains_id = spawns.lock().contains_key(&id);
|
let key = SpawnId::Stats(id.to_owned());
|
||||||
|
let spawn_contains_id = spawns.lock().contains_key(&key);
|
||||||
let s = tokio::spawn(Self::update_container_stat(
|
let s = tokio::spawn(Self::update_container_stat(
|
||||||
docker,
|
docker,
|
||||||
id.to_owned(),
|
id.to_owned(),
|
||||||
@@ -139,7 +157,7 @@ impl DockerData {
|
|||||||
spawns,
|
spawns,
|
||||||
));
|
));
|
||||||
if !spawn_contains_id {
|
if !spawn_contains_id {
|
||||||
self.spawns.lock().insert(id, s);
|
self.spawns.lock().insert(key, s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -190,7 +208,7 @@ impl DockerData {
|
|||||||
timestamps: bool,
|
timestamps: bool,
|
||||||
since: i64,
|
since: i64,
|
||||||
app_data: Arc<Mutex<AppData>>,
|
app_data: Arc<Mutex<AppData>>,
|
||||||
spawns: Arc<Mutex<HashMap<String, JoinHandle<()>>>>,
|
spawns: Arc<Mutex<HashMap<SpawnId, JoinHandle<()>>>>,
|
||||||
) {
|
) {
|
||||||
let options = Some(LogsOptions::<String> {
|
let options = Some(LogsOptions::<String> {
|
||||||
stdout: true,
|
stdout: true,
|
||||||
@@ -211,7 +229,8 @@ impl DockerData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
spawns.lock().remove(&id);
|
let key = SpawnId::Log(id.to_owned());
|
||||||
|
spawns.lock().remove(&key);
|
||||||
app_data.lock().update_log_by_id(output, id.to_owned());
|
app_data.lock().update_log_by_id(output, id.to_owned());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -223,11 +242,14 @@ impl DockerData {
|
|||||||
let id = id.to_owned();
|
let id = id.to_owned();
|
||||||
let app_data = Arc::clone(&self.app_data);
|
let app_data = Arc::clone(&self.app_data);
|
||||||
let spawns = Arc::clone(&self.spawns);
|
let spawns = Arc::clone(&self.spawns);
|
||||||
self.spawns.lock().insert(
|
let key = SpawnId::Log(id.to_owned());
|
||||||
id.to_owned(),
|
let s = tokio::spawn(Self::update_log(
|
||||||
tokio::spawn(Self::update_log(
|
|
||||||
docker, id, timestamps, 0, app_data, spawns,
|
docker, id, timestamps, 0, app_data, spawns,
|
||||||
)),
|
));
|
||||||
|
|
||||||
|
self.spawns.lock().insert(
|
||||||
|
key,
|
||||||
|
s
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -238,7 +260,8 @@ impl DockerData {
|
|||||||
if let Some(index) = optional_index {
|
if let Some(index) = optional_index {
|
||||||
let id = self.app_data.lock().containers.items[index].id.to_owned();
|
let id = self.app_data.lock().containers.items[index].id.to_owned();
|
||||||
|
|
||||||
let running = self.spawns.lock().contains_key(&id);
|
let key = SpawnId::Log(id.to_owned());
|
||||||
|
let running = self.spawns.lock().contains_key(&key);
|
||||||
|
|
||||||
if !running {
|
if !running {
|
||||||
let since = self.app_data.lock().containers.items[index].last_updated as i64;
|
let since = self.app_data.lock().containers.items[index].last_updated as i64;
|
||||||
@@ -249,13 +272,13 @@ impl DockerData {
|
|||||||
let spawns = Arc::clone(&self.spawns);
|
let spawns = Arc::clone(&self.spawns);
|
||||||
let s = tokio::spawn(Self::update_log(
|
let s = tokio::spawn(Self::update_log(
|
||||||
docker,
|
docker,
|
||||||
id.to_owned(),
|
id,
|
||||||
timestamps,
|
timestamps,
|
||||||
since,
|
since,
|
||||||
app_data,
|
app_data,
|
||||||
spawns,
|
spawns,
|
||||||
));
|
));
|
||||||
self.spawns.lock().insert(id, s);
|
self.spawns.lock().insert(key, s);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user