refactor: docker functions
This commit is contained in:
+52
-66
@@ -183,8 +183,7 @@ impl DockerData {
|
||||
}
|
||||
|
||||
/// Update all logs, spawn each container into own tokio::spawn thread
|
||||
// rename init all logs, as only gets run once
|
||||
async fn update_all_logs(&mut self, all_ids: &[(bool, String)]) {
|
||||
async fn init_all_logs(&mut self, all_ids: &[(bool, String)]) {
|
||||
let mut handles = vec![];
|
||||
|
||||
for (_, id) in all_ids.iter() {
|
||||
@@ -212,7 +211,9 @@ impl DockerData {
|
||||
self.update_all_container_stats(&all_ids).await;
|
||||
}
|
||||
|
||||
async fn loading_spin(gui_state: Arc<Mutex<GuiState>>) -> JoinHandle<()> {
|
||||
/// Animate the loading icon
|
||||
async fn loading_spin(&mut self ) -> JoinHandle<()> {
|
||||
let gui_state = Arc::clone(&self.gui_state);
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
@@ -221,20 +222,22 @@ impl DockerData {
|
||||
})
|
||||
}
|
||||
|
||||
fn stop_loading_spin(handle: JoinHandle<()>, gui_state: &Arc<Mutex<GuiState>>) {
|
||||
/// Stop the loading_spin fn, and reset gui loading status
|
||||
fn stop_loading_spin(&mut self, handle: JoinHandle<()>) {
|
||||
handle.abort();
|
||||
gui_state.lock().reset_loading();
|
||||
self.gui_state.lock().reset_loading();
|
||||
}
|
||||
|
||||
|
||||
// Initialize docker container data, before any messages are received
|
||||
async fn initialise_container_data(&mut self) {
|
||||
let gui_state = Arc::clone(&self.gui_state);
|
||||
let loading_spin = Self::loading_spin(gui_state).await;
|
||||
let loading_spin = self.loading_spin().await;
|
||||
|
||||
let all_ids = self.update_all_containers().await;
|
||||
self.update_all_container_stats(&all_ids).await;
|
||||
|
||||
// Maybe only do a single one at first?
|
||||
self.update_all_logs(&all_ids).await;
|
||||
self.init_all_logs(&all_ids).await;
|
||||
|
||||
if all_ids.is_empty() {
|
||||
self.initialised = true;
|
||||
@@ -246,7 +249,7 @@ impl DockerData {
|
||||
self.initialised = self.app_data.lock().initialised(&all_ids);
|
||||
}
|
||||
self.app_data.lock().init = true;
|
||||
Self::stop_loading_spin(loading_spin, &self.gui_state);
|
||||
self.stop_loading_spin(loading_spin);
|
||||
}
|
||||
|
||||
/// Handle incoming messages, container controls & all container information update
|
||||
@@ -254,80 +257,65 @@ impl DockerData {
|
||||
while let Some(message) = self.receiver.recv().await {
|
||||
let docker = Arc::clone(&self.docker);
|
||||
let app_data = Arc::clone(&self.app_data);
|
||||
let gui_state = Arc::clone(&self.gui_state);
|
||||
match message {
|
||||
DockerMessage::Pause(id) => {
|
||||
let spin_gui = Arc::clone(&gui_state);
|
||||
let loading_spin = Self::loading_spin(gui_state).await;
|
||||
tokio::spawn(async move {
|
||||
docker.pause_container(&id).await.unwrap_or_else(|_| {
|
||||
app_data
|
||||
.lock()
|
||||
.set_error(AppError::DockerCommand(DockerControls::Pause))
|
||||
});
|
||||
Self::stop_loading_spin(loading_spin, &spin_gui);
|
||||
let loading_spin =self.loading_spin().await;
|
||||
docker.pause_container(&id).await.unwrap_or_else(|_| {
|
||||
app_data
|
||||
.lock()
|
||||
.set_error(AppError::DockerCommand(DockerControls::Pause))
|
||||
});
|
||||
self.stop_loading_spin(loading_spin);
|
||||
}
|
||||
DockerMessage::Restart(id) => {
|
||||
let spin_gui = Arc::clone(&gui_state);
|
||||
let loading_spin = Self::loading_spin(gui_state).await;
|
||||
tokio::spawn(async move {
|
||||
docker
|
||||
.restart_container(&id, None)
|
||||
.await
|
||||
.unwrap_or_else(|_| {
|
||||
app_data
|
||||
.lock()
|
||||
.set_error(AppError::DockerCommand(DockerControls::Restart))
|
||||
});
|
||||
Self::stop_loading_spin(loading_spin, &spin_gui);
|
||||
});
|
||||
let loading_spin =self.loading_spin().await;
|
||||
docker
|
||||
.restart_container(&id, None)
|
||||
.await
|
||||
.unwrap_or_else(|_| {
|
||||
app_data
|
||||
.lock()
|
||||
.set_error(AppError::DockerCommand(DockerControls::Restart))
|
||||
});
|
||||
self.stop_loading_spin(loading_spin);
|
||||
}
|
||||
DockerMessage::Start(id) => {
|
||||
let spin_gui = Arc::clone(&gui_state);
|
||||
let loading_spin = Self::loading_spin(gui_state).await;
|
||||
tokio::spawn(async move {
|
||||
docker
|
||||
.start_container(&id, None::<StartContainerOptions<String>>)
|
||||
.await
|
||||
.unwrap_or_else(|_| {
|
||||
app_data
|
||||
.lock()
|
||||
.set_error(AppError::DockerCommand(DockerControls::Start))
|
||||
});
|
||||
Self::stop_loading_spin(loading_spin, &spin_gui);
|
||||
});
|
||||
let loading_spin =self.loading_spin().await;
|
||||
docker
|
||||
.start_container(&id, None::<StartContainerOptions<String>>)
|
||||
.await
|
||||
.unwrap_or_else(|_| {
|
||||
app_data
|
||||
.lock()
|
||||
.set_error(AppError::DockerCommand(DockerControls::Start))
|
||||
});
|
||||
self.stop_loading_spin(loading_spin);
|
||||
}
|
||||
DockerMessage::Stop(id) => {
|
||||
let spin_gui = Arc::clone(&gui_state);
|
||||
let loading_spin = Self::loading_spin(gui_state).await;
|
||||
tokio::spawn(async move {
|
||||
docker.stop_container(&id, None).await.unwrap_or_else(|_| {
|
||||
app_data
|
||||
.lock()
|
||||
.set_error(AppError::DockerCommand(DockerControls::Stop))
|
||||
});
|
||||
Self::stop_loading_spin(loading_spin, &spin_gui);
|
||||
let loading_spin =self.loading_spin().await;
|
||||
docker.stop_container(&id, None).await.unwrap_or_else(|_| {
|
||||
app_data
|
||||
.lock()
|
||||
.set_error(AppError::DockerCommand(DockerControls::Stop))
|
||||
});
|
||||
self.stop_loading_spin(loading_spin);
|
||||
}
|
||||
DockerMessage::Unpause(id) => {
|
||||
let spin_gui = Arc::clone(&gui_state);
|
||||
let loading_spin = Self::loading_spin(gui_state).await;
|
||||
tokio::spawn(async move {
|
||||
docker.unpause_container(&id).await.unwrap_or_else(|_| {
|
||||
app_data
|
||||
.lock()
|
||||
.set_error(AppError::DockerCommand(DockerControls::Unpause))
|
||||
});
|
||||
Self::stop_loading_spin(loading_spin, &spin_gui);
|
||||
let loading_spin =self.loading_spin().await;
|
||||
docker.unpause_container(&id).await.unwrap_or_else(|_| {
|
||||
app_data
|
||||
.lock()
|
||||
.set_error(AppError::DockerCommand(DockerControls::Unpause))
|
||||
});
|
||||
self.stop_loading_spin(loading_spin);
|
||||
self.update_everything().await
|
||||
}
|
||||
DockerMessage::Update => self.update_everything().await,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Initialise self, and start the updated loop
|
||||
/// Initialise self, and start the message receiving loop
|
||||
pub async fn init(
|
||||
args: CliArgs,
|
||||
app_data: Arc<Mutex<AppData>>,
|
||||
@@ -346,8 +334,6 @@ impl DockerData {
|
||||
};
|
||||
inner.initialise_container_data().await;
|
||||
|
||||
// todo!(" change this to recv.next()");
|
||||
// inner.update_loop().await;
|
||||
inner.message_handler().await;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,7 +223,7 @@ pub fn draw_logs<B: Backend>(
|
||||
f: &mut Frame<'_, B>,
|
||||
gui_state: &Arc<Mutex<GuiState>>,
|
||||
index: Option<usize>,
|
||||
loading_icon: String,
|
||||
loading_icon: String,
|
||||
selected_panel: &SelectablePanel,
|
||||
) {
|
||||
let panel = SelectablePanel::Logs;
|
||||
@@ -368,7 +368,7 @@ pub fn draw_heading_bar<B: Backend>(
|
||||
columns: &Columns,
|
||||
f: &mut Frame<'_, B>,
|
||||
has_containers: bool,
|
||||
loading_icon: String,
|
||||
loading_icon: String,
|
||||
info_visible: bool,
|
||||
) {
|
||||
let block = || Block::default().style(Style::default().bg(Color::Magenta).fg(Color::Black));
|
||||
|
||||
+2
-2
@@ -167,8 +167,8 @@ pub struct GuiState {
|
||||
// If a BMapTree think it would mean have to implement ordering for SelectablePanel
|
||||
area_map: HashMap<SelectablePanel, Rect>,
|
||||
loading_icon: Loading,
|
||||
// Should be a vec, each time loading add a new to the vec, and reset remove from vec
|
||||
// for for if is_loading just check if vec is empty or not
|
||||
// Should be a vec, each time loading add a new to the vec, and reset remove from vec
|
||||
// for for if is_loading just check if vec is empty or not
|
||||
is_loading: bool,
|
||||
pub selected_panel: SelectablePanel,
|
||||
pub show_help: bool,
|
||||
|
||||
+4
-5
@@ -14,7 +14,6 @@ use std::{
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
use tokio::sync::mpsc::Sender;
|
||||
use tracing::error;
|
||||
use tui::{
|
||||
backend::{Backend, CrosstermBackend},
|
||||
layout::{Constraint, Direction, Layout},
|
||||
@@ -68,7 +67,7 @@ pub async fn create_ui(
|
||||
terminal.show_cursor().unwrap();
|
||||
|
||||
if let Err(err) = res {
|
||||
error!(%err);
|
||||
println!("{}", err);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -157,7 +156,7 @@ fn ui<B: Backend>(
|
||||
let selected_panel = gui_state.lock().selected_panel;
|
||||
let show_help = gui_state.lock().show_help;
|
||||
let info_text = gui_state.lock().info_box_text.clone();
|
||||
let loading_icon = gui_state.lock().get_loading();
|
||||
let loading_icon = gui_state.lock().get_loading();
|
||||
|
||||
let whole_layout = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
@@ -219,7 +218,7 @@ fn ui<B: Backend>(
|
||||
f,
|
||||
gui_state,
|
||||
log_index,
|
||||
loading_icon.to_owned(),
|
||||
loading_icon.to_owned(),
|
||||
&selected_panel,
|
||||
);
|
||||
|
||||
@@ -228,7 +227,7 @@ fn ui<B: Backend>(
|
||||
&column_widths,
|
||||
f,
|
||||
has_containers,
|
||||
loading_icon,
|
||||
loading_icon,
|
||||
show_help,
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user