use crate::container::{remove_container, remove_network, remove_volume, stop_container};
use crate::neo4j::Neo4JContainer;
use crate::progress::{CommandStatus, Progress, step_header, summary};
use indicatif::MultiProgress;
use tokio::task::JoinSet;
use super::wildfly::AnalysisInstance;
const TOTAL_STEPS: u32 = 4;
pub(super) async fn build_neo4j_image(neo4j: &Neo4JContainer) -> anyhow::Result<()> {
step_header(3, TOTAL_STEPS, "Building Neo4J image...");
let progress = Progress::new(&neo4j.image.image_tag());
progress.show_progress("Stopping neo4j...");
stop_container(&neo4j.container_name()).await?;
neo4j
.image
.build_image(&neo4j.container_name(), &progress)
.await?;
progress.finish_success(Some("Ready"));
Ok(())
}
pub(super) async fn cleanup(
instances: &[AnalysisInstance],
neo4j: &Neo4JContainer,
network: &str,
) -> anyhow::Result<()> {
step_header(4, TOTAL_STEPS, "Cleaning up...");
let multi_progress = MultiProgress::new();
let mut tasks = JoinSet::new();
let mut status: Vec<CommandStatus> = Vec::new();
for instance in instances {
let container_name = instance.name.clone();
let progress = Progress::join(&multi_progress, &container_name);
tasks.spawn(async move {
match stop_container(&container_name).await {
Ok(()) => {
progress.finish_success(Some("Stopped"));
CommandStatus::success(&container_name)
}
Err(e) => {
let msg = e.to_string();
progress.finish_error(&msg);
CommandStatus::error(&container_name, &msg)
}
}
});
}
status.extend(tasks.join_all().await);
status.extend(cleanup_neo4j_and_network(neo4j, network).await);
let count = status.len();
summary(count, &status);
Ok(())
}
pub(super) async fn cleanup_minimal(neo4j: &Neo4JContainer, network: &str) -> Vec<CommandStatus> {
step_header(4, TOTAL_STEPS, "Cleaning up...");
let status = cleanup_neo4j_and_network(neo4j, network).await;
let count = status.len();
summary(count, &status);
status
}
async fn cleanup_neo4j_and_network(neo4j: &Neo4JContainer, network: &str) -> Vec<CommandStatus> {
let neo4j_container = neo4j.container_name();
let _ = stop_container(&neo4j_container).await;
vec![
cleanup_resource(&neo4j_container, remove_container(&neo4j_container).await),
cleanup_resource(
&neo4j.volume_name(),
remove_volume(&neo4j.volume_name()).await,
),
cleanup_resource(network, remove_network(network).await),
]
}
fn cleanup_resource(name: &str, result: anyhow::Result<()>) -> CommandStatus {
let progress = Progress::new(name);
match result {
Ok(()) => {
progress.finish_success(Some("Removed"));
CommandStatus::success(name)
}
Err(e) => {
let msg = e.to_string();
progress.finish_error(&msg);
CommandStatus::error(name, &msg)
}
}
}