refactor: remove uneccessary is_running load

This commit is contained in:
Jack Wills
2024-11-15 16:30:02 +00:00
parent f0b1145651
commit 76ccf7c006
5 changed files with 8 additions and 45 deletions
+1
View File
@@ -133,6 +133,7 @@ impl ContainerPorts {
.count() .count()
} }
/// Return as tuple of Strings, ip address, private port, and public port
pub fn print(&self) -> (String, String, String) { pub fn print(&self) -> (String, String, String) {
( (
self.ip self.ip
-8
View File
@@ -8,14 +8,6 @@ use tokio::sync::oneshot::Sender;
pub enum DockerMessage { pub enum DockerMessage {
ConfirmDelete(ContainerId), ConfirmDelete(ContainerId),
Control((DockerCommand, ContainerId)), Control((DockerCommand, ContainerId)),
// Delete(ContainerId),
Exec(Sender<Arc<Docker>>), Exec(Sender<Arc<Docker>>),
// Pause(ContainerId),
Quit,
// Restart(ContainerId),
// Start(ContainerId),
// Stop(ContainerId),
// Resume(ContainerId),
Update, Update,
} }
+1 -15
View File
@@ -10,10 +10,7 @@ use futures_util::StreamExt;
use parking_lot::Mutex; use parking_lot::Mutex;
use std::{ use std::{
collections::HashMap, collections::HashMap,
sync::{ sync::{atomic::AtomicUsize, Arc},
atomic::{AtomicBool, AtomicUsize},
Arc,
},
}; };
use tokio::{ use tokio::{
sync::mpsc::{Receiver, Sender}, sync::mpsc::{Receiver, Sender},
@@ -62,7 +59,6 @@ pub struct DockerData {
binate: Binate, binate: Binate,
docker: Arc<Docker>, docker: Arc<Docker>,
gui_state: Arc<Mutex<GuiState>>, gui_state: Arc<Mutex<GuiState>>,
is_running: Arc<AtomicBool>,
init: Option<Arc<AtomicUsize>>, init: Option<Arc<AtomicUsize>>,
receiver: Receiver<DockerMessage>, receiver: Receiver<DockerMessage>,
spawns: Arc<Mutex<HashMap<SpawnId, JoinHandle<()>>>>, spawns: Arc<Mutex<HashMap<SpawnId, JoinHandle<()>>>>,
@@ -410,14 +406,6 @@ impl DockerData {
docker_tx.send(Arc::clone(&self.docker)).ok(); docker_tx.send(Arc::clone(&self.docker)).ok();
} }
DockerMessage::Update => self.update_everything().await, DockerMessage::Update => self.update_everything().await,
DockerMessage::Quit => {
self.spawns
.lock()
.values()
.for_each(tokio::task::JoinHandle::abort);
self.is_running
.store(false, std::sync::atomic::Ordering::SeqCst);
}
} }
} }
} }
@@ -443,7 +431,6 @@ impl DockerData {
docker_rx: Receiver<DockerMessage>, docker_rx: Receiver<DockerMessage>,
docker_tx: Sender<DockerMessage>, docker_tx: Sender<DockerMessage>,
gui_state: Arc<Mutex<GuiState>>, gui_state: Arc<Mutex<GuiState>>,
is_running: Arc<AtomicBool>,
) { ) {
let args = app_data.lock().args.clone(); let args = app_data.lock().args.clone();
if app_data.lock().get_error().is_none() { if app_data.lock().get_error().is_none() {
@@ -454,7 +441,6 @@ impl DockerData {
docker: Arc::new(docker), docker: Arc::new(docker),
gui_state, gui_state,
init: Some(Arc::new(AtomicUsize::new(0))), init: Some(Arc::new(AtomicUsize::new(0))),
is_running,
receiver: docker_rx, receiver: docker_rx,
spawns: Arc::new(Mutex::new(HashMap::new())), spawns: Arc::new(Mutex::new(HashMap::new())),
}; };
+4 -10
View File
@@ -1,10 +1,7 @@
use std::{ use std::{
fs::OpenOptions, fs::OpenOptions,
io::{BufWriter, Write}, io::{BufWriter, Write},
sync::{ sync::{atomic::AtomicBool, Arc},
atomic::{AtomicBool, Ordering},
Arc,
},
time::SystemTime, time::SystemTime,
}; };
@@ -84,9 +81,6 @@ impl InputHandler {
} }
} }
} }
if !self.is_running.load(Ordering::SeqCst) {
break;
}
} }
} }
@@ -97,12 +91,12 @@ impl InputHandler {
/// Send a quit message to docker, to abort all spawns, if an error is returned, set is_running to false here instead /// Send a quit message to docker, to abort all spawns, if an error is returned, set is_running to false here instead
/// If gui_status is Error or Init, then just set the is_running to false immediately, for a quicker exit /// If gui_status is Error or Init, then just set the is_running to false immediately, for a quicker exit
async fn quit(&self) { fn quit(&self) {
let error_init = self let error_init = self
.gui_state .gui_state
.lock() .lock()
.status_contains(&[Status::Error, Status::Init]); .status_contains(&[Status::Error, Status::Init]);
if error_init || self.docker_tx.send(DockerMessage::Quit).await.is_err() { if !error_init {
self.is_running self.is_running
.store(false, std::sync::atomic::Ordering::SeqCst); .store(false, std::sync::atomic::Ordering::SeqCst);
} }
@@ -466,7 +460,7 @@ impl InputHandler {
let is_q = || key_code == KeyCode::Char('q') || key_code == KeyCode::Char('Q'); let is_q = || key_code == KeyCode::Char('q') || key_code == KeyCode::Char('Q');
if key_modifier == KeyModifiers::CONTROL && is_c() || is_q() && !contains_filter { if key_modifier == KeyModifiers::CONTROL && is_c() || is_q() && !contains_filter {
// Always just quit on Ctrl + c/C or q/Q, unless in FIlter status active // Always just quit on Ctrl + c/C or q/Q, unless in FIlter status active
self.quit().await; self.quit();
} }
if contains_error { if contains_error {
+2 -12
View File
@@ -52,7 +52,6 @@ async fn docker_init(
docker_rx: Receiver<DockerMessage>, docker_rx: Receiver<DockerMessage>,
docker_tx: Sender<DockerMessage>, docker_tx: Sender<DockerMessage>,
gui_state: &Arc<Mutex<GuiState>>, gui_state: &Arc<Mutex<GuiState>>,
is_running: &Arc<AtomicBool>,
host: Option<String>, host: Option<String>,
) { ) {
let connection = host.map_or_else(Docker::connect_with_socket_defaults, |host| { let connection = host.map_or_else(Docker::connect_with_socket_defaults, |host| {
@@ -63,10 +62,9 @@ async fn docker_init(
if docker.ping().await.is_ok() { if docker.ping().await.is_ok() {
let app_data = Arc::clone(app_data); let app_data = Arc::clone(app_data);
let gui_state = Arc::clone(gui_state); let gui_state = Arc::clone(gui_state);
let is_running = Arc::clone(is_running);
tokio::spawn(DockerData::init( tokio::spawn(DockerData::init(
app_data, docker, docker_rx, docker_tx, gui_state, is_running, app_data, docker, docker_rx, docker_tx, gui_state,
)); ));
} else { } else {
app_data app_data
@@ -118,15 +116,7 @@ async fn main() {
let is_running = Arc::new(AtomicBool::new(true)); let is_running = Arc::new(AtomicBool::new(true));
let (docker_tx, docker_rx) = tokio::sync::mpsc::channel(32); let (docker_tx, docker_rx) = tokio::sync::mpsc::channel(32);
docker_init( docker_init(&app_data, docker_rx, docker_tx.clone(), &gui_state, host).await;
&app_data,
docker_rx,
docker_tx.clone(),
&gui_state,
&is_running,
host,
)
.await;
if args.gui { if args.gui {
let (input_tx, input_rx) = tokio::sync::mpsc::channel(32); let (input_tx, input_rx) = tokio::sync::mpsc::channel(32);