2025-12-16 03:28:23 +01:00
|
|
|
use std::{
|
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
|
time::Duration,
|
|
|
|
|
};
|
2025-12-01 12:48:39 +01:00
|
|
|
|
2026-02-02 07:19:22 +01:00
|
|
|
use testing_framework_core::{
|
|
|
|
|
adjust_timeout,
|
|
|
|
|
scenario::{Application, DynError, NodeControlHandle},
|
|
|
|
|
};
|
2025-12-01 12:48:39 +01:00
|
|
|
use tokio::process::Command;
|
2025-12-11 10:08:49 +01:00
|
|
|
use tracing::info;
|
2025-12-01 12:48:39 +01:00
|
|
|
|
2025-12-10 15:26:34 +01:00
|
|
|
use crate::{docker::commands::run_docker_command, errors::ComposeRunnerError};
|
2025-12-01 12:48:39 +01:00
|
|
|
|
2025-12-16 03:28:23 +01:00
|
|
|
const COMPOSE_RESTART_TIMEOUT: Duration = Duration::from_secs(120);
|
2026-02-02 07:19:22 +01:00
|
|
|
const COMPOSE_RESTART_DESCRIPTION: &str = "docker compose restart";
|
2025-12-16 03:28:23 +01:00
|
|
|
|
2025-12-01 12:48:39 +01:00
|
|
|
pub async fn restart_compose_service(
|
|
|
|
|
compose_file: &Path,
|
|
|
|
|
project_name: &str,
|
|
|
|
|
service: &str,
|
|
|
|
|
) -> Result<(), ComposeRunnerError> {
|
2026-02-02 07:19:22 +01:00
|
|
|
let command = compose_restart_command(compose_file, project_name, service);
|
|
|
|
|
|
|
|
|
|
info!(
|
|
|
|
|
service,
|
|
|
|
|
project = project_name,
|
|
|
|
|
compose_file = %compose_file.display(),
|
|
|
|
|
"restarting compose service"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
run_docker_command(
|
|
|
|
|
command,
|
|
|
|
|
adjust_timeout(COMPOSE_RESTART_TIMEOUT),
|
|
|
|
|
COMPOSE_RESTART_DESCRIPTION,
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(ComposeRunnerError::Compose)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn compose_restart_command(compose_file: &Path, project_name: &str, service: &str) -> Command {
|
2025-12-01 12:48:39 +01:00
|
|
|
let mut command = Command::new("docker");
|
|
|
|
|
command
|
|
|
|
|
.arg("compose")
|
|
|
|
|
.arg("-f")
|
|
|
|
|
.arg(compose_file)
|
|
|
|
|
.arg("-p")
|
|
|
|
|
.arg(project_name)
|
|
|
|
|
.arg("restart")
|
|
|
|
|
.arg(service);
|
2026-02-02 07:19:22 +01:00
|
|
|
command
|
2025-12-01 12:48:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Compose-specific node control handle for restarting nodes.
|
|
|
|
|
pub struct ComposeNodeControl {
|
|
|
|
|
pub(crate) compose_file: PathBuf,
|
|
|
|
|
pub(crate) project_name: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[async_trait::async_trait]
|
2026-02-02 07:19:22 +01:00
|
|
|
impl<E: Application> NodeControlHandle<E> for ComposeNodeControl {
|
2026-02-05 08:23:14 +02:00
|
|
|
async fn restart_node(&self, name: &str) -> Result<(), DynError> {
|
|
|
|
|
restart_compose_service(&self.compose_file, &self.project_name, name)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(|err| format!("node restart failed: {err}").into())
|
2025-12-01 12:48:39 +01:00
|
|
|
}
|
|
|
|
|
}
|