1
0
Fork 0
mirror of https://codeberg.org/icewind/haze.git synced 2026-10-02 01:04:10 +02:00

show information about slow starting services on start

This commit is contained in:
Robin Appelman 2026-09-08 00:15:29 +02:00
commit 5555efbfcc
5 changed files with 154 additions and 56 deletions

View file

@ -16,13 +16,18 @@ use crate::service::ServiceTrait;
use crate::service::{RedisTls, Service};
use crate::sources::Sources;
use bollard::Docker;
use futures_util::future::{Either, join_all, select};
use itertools::Itertools;
use miette::{IntoDiagnostic, Report, Result, WrapErr};
use std::collections::HashMap;
use std::env::{var, vars};
use std::fs::{create_dir_all, remove_dir_all, remove_file, write};
use std::io::stdout;
use std::os::unix::process::CommandExt;
use std::pin::pin;
use std::process::{Command, ExitCode};
use std::time::Duration;
use tokio::time::sleep;
mod args;
mod cloud;
@ -92,23 +97,24 @@ async fn main() -> Result<ExitCode> {
let cache_dir = cache_dir.into_diagnostic()?;
if let Some(id) = cache_dir.file_name().to_str()
&& id.starts_with("haze-")
&& !retain.iter().any(|cloud| cloud.id == id) {
let path = cache_dir.path();
// By default, the worktrees live within the work dir, we do not want to delete it here.
if path == config.worktree_dir {
continue;
}
if path.is_dir() {
remove_dir_all(&path).into_diagnostic().wrap_err_with(|| {
format!("Failed to cleanup stray cache dir: {}", path.display())
})?;
} else {
remove_file(&path).into_diagnostic().wrap_err_with(|| {
format!("Failed to cleanup stray cache file: {}", path.display())
})?;
}
&& !retain.iter().any(|cloud| cloud.id == id)
{
let path = cache_dir.path();
// By default, the worktrees live within the work dir, we do not want to delete it here.
if path == config.worktree_dir {
continue;
}
if path.is_dir() {
remove_dir_all(&path).into_diagnostic().wrap_err_with(|| {
format!("Failed to cleanup stray cache dir: {}", path.display())
})?;
} else {
remove_file(&path).into_diagnostic().wrap_err_with(|| {
format!("Failed to cleanup stray cache file: {}", path.display())
})?;
}
}
}
prune_worktrees(&config)?;
@ -146,7 +152,7 @@ async fn main() -> Result<ExitCode> {
}
}
HazeArgs::Start { options } => {
setup(&docker, options, &config).await?;
setup(&docker, options, &config, false, false).await?;
}
HazeArgs::Stop { filter } => {
let cloud = Cloud::get_by_filter(&docker, filter, &config).await?;
@ -277,37 +283,7 @@ async fn main() -> Result<ExitCode> {
}
}
HazeArgs::Test { options, mut args } => {
let cloud = Cloud::create(&docker, options, &config).await?;
println!("Waiting for servers to start");
cloud.wait_for_start(&docker).await?;
if !cloud.preset_config.is_empty() {
println!("Writing preset config");
let encoded_preset_config =
serde_json::to_string(&cloud.preset_config).into_diagnostic()?;
cloud
.write_file(&docker, "config/preset.config.json", encoded_preset_config)
.await?;
cloud.write_file(&docker, "config/preset.config.php", "<?php $CONFIG=json_decode(file_get_contents(__DIR__ . '/preset.config.json'), true);").await?;
}
println!("Installing");
if let Err(e) = cloud
.exec(
&docker,
vec![
"install",
&config.auto_setup.username,
&config.auto_setup.password,
],
false,
Vec::<String>::default(),
)
.await
{
cloud.destroy(&docker, &config).await?;
return Err(e);
}
let cloud = setup(&docker, options, &config, true, true).await?;
if let Some(app) = args
.first()
.as_ref()
@ -326,7 +302,7 @@ async fn main() -> Result<ExitCode> {
return Ok(result.into());
}
HazeArgs::Integration { options, mut args } => {
let cloud = setup(&docker, options, &config).await?;
let cloud = setup(&docker, options, &config, true, true).await?;
args.insert(0, "integration".to_string());
cloud.exec(&docker, args, false, get_forward_env()).await?;
@ -398,7 +374,7 @@ async fn main() -> Result<ExitCode> {
cloud.destroy(&docker, &config).await?;
}
HazeArgs::Shell { command, options } => {
let cloud = setup(&docker, options, &config).await?;
let cloud = setup(&docker, options, &config, true, false).await?;
cloud
.exec(
&docker,
@ -566,11 +542,17 @@ async fn main() -> Result<ExitCode> {
Ok(ExitCode::SUCCESS)
}
async fn setup(docker: &Docker, options: CloudOptions, config: &HazeConfig) -> Result<Cloud> {
async fn setup(
docker: &Docker,
options: CloudOptions,
config: &HazeConfig,
always_setup: bool,
force_wait: bool,
) -> Result<Cloud> {
let cloud = Cloud::create(docker, options, config).await?;
println!("{}", cloud.address);
let host = cloud.address.split_once("://").expect("no address?").1;
if config.auto_setup.enabled {
if always_setup || config.auto_setup.enabled {
println!("Waiting for servers to start");
cloud.wait_for_start(docker).await?;
@ -703,5 +685,37 @@ async fn setup(docker: &Docker, options: CloudOptions, config: &HazeConfig) -> R
.await?;
}
}
{
let mut waiting = HashMap::new();
for service in cloud.services() {
// give the service 100ms before we mark them as pending
let sleep = pin!(sleep(Duration::from_millis(100)));
let future = service.final_wait(docker, &cloud.id);
if let Either::Left((_, future)) = select(sleep, future).await {
waiting.insert(service.name(), future);
}
}
if !waiting.is_empty() {
println!();
println!("The folowing services are still starting in the background:");
for service in waiting.keys() {
println!(" - {service}");
}
println!();
if !force_wait {
println!(
"You can safely terminate this command with CTRL-C and use the instance, but some functionality provided by the service might not be ready yet."
)
}
join_all(waiting.into_values())
.await
.into_iter()
.collect::<Result<Vec<_>, _>>()?;
}
}
Ok(cloud)
}