Skip to content

Commit 0841ec7

Browse files
committed
chore(nuwax-cli): Bump version to 1.0.74 and add MySQL service readiness handling
- Increment project version from 1.0.73 to 1.0.74 in Cargo.toml and Cargo.lock - Introduce constants for MySQL and other services' timeout durations - Implement a new function to wait for MySQL service readiness, addressing potential startup deadlocks with Java services - Refactor auto upgrade deployment logic to incorporate phased waiting for MySQL and other services
1 parent 0d7abfc commit 0841ec7

File tree

5 files changed

+58
-17
lines changed

5 files changed

+58
-17
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client-core/src/constants.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,12 @@ pub mod sql {
591591
/// MySQL 容器默认映射端口
592592
pub const DEFAULT_MYSQL_CONTAINER_PORT: u16 = 13306;
593593

594+
/// MySQL 服务等待超时(秒)- 用于等待 MySQL 容器就绪
595+
pub const MYSQL_READY_TIMEOUT: u64 = 60;
596+
597+
/// 其他服务启动等待超时(秒)- SQL 升级后等待 Java 等服务
598+
pub const OTHER_SERVICES_TIMEOUT: u64 = 120;
599+
594600
/// 临时 SQL 目录名
595601
pub const TEMP_SQL_DIR: &str = "temp_sql";
596602

nuwax-cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "nuwax-cli"
3-
version = "1.0.73"
3+
version = "1.0.74"
44
edition = "2024"
55
description = "Docker 服务管理和升级工具"
66
authors = ["soddygo"]

nuwax-cli/src/commands/auto_upgrade_deploy.rs

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::commands::{backup, docker_service, update};
44
use crate::docker_service::health_check::HealthChecker;
55
use crate::docker_utils;
66
use anyhow::{Context, Result};
7-
use client_core::constants::{sql, timeout};
7+
use client_core::constants::sql;
88
use client_core::container::DockerManager;
99
use client_core::mysql_executor::{MySqlConfig, MySqlExecutor};
1010
use client_core::sql_diff::generate_live_schema_diff;
@@ -284,19 +284,39 @@ pub async fn run_auto_upgrade_deploy(
284284
info!("▶️ 正在启动Docker服务...");
285285
docker_service::start_docker_services(app, config_file.clone(), project_name.clone()).await?;
286286

287-
// 等待服务启动完成(最多等待90秒,因为部署后启动可能需要更长时间)
288-
info!("⏳ 等待Docker服务完全启动...");
287+
// 获取 compose 文件路径
289288
let compose_path = get_compose_file_path(&config_file);
290-
if docker_utils::wait_for_compose_services_started(&compose_path, timeout::DEPLOY_START_TIMEOUT)
291-
.await?
292-
{
293-
info!("✅ 自动升级部署完成,服务已成功启动");
294289

295-
// 🔄 执行数据库升级(仅在升级部署时)
296-
if !is_first_deployment {
297-
execute_sql_diff_upgrade(&config_file).await?;
290+
// 🔄 分阶段等待服务启动(解决 MySQL-Java 死锁问题)
291+
// 死锁原因:Java 容器健康检查依赖 MySQL 表结构升级,但 SQL 升级要等所有服务就绪后才执行
292+
// 解决方案:先等待 MySQL → 执行 SQL 升级 → 再等待其他服务
293+
if !is_first_deployment {
294+
// 阶段 1/2: 仅等待 MySQL 容器就绪
295+
info!("⏳ 阶段 1/2: 等待 MySQL 服务就绪...");
296+
let mysql_ready =
297+
docker_utils::wait_for_mysql_ready(&compose_path, sql::MYSQL_READY_TIMEOUT).await?;
298+
299+
if mysql_ready {
300+
info!("✅ MySQL 服务已就绪");
301+
} else {
302+
warn!("⚠️ 等待 MySQL 服务超时,仍尝试执行 SQL 升级...");
298303
}
299304

305+
// 阶段 2/2(前半部分): 执行 SQL 升级
306+
info!("🔄 执行数据库升级...");
307+
execute_sql_diff_upgrade(&config_file).await?;
308+
}
309+
310+
// 等待所有服务完全启动(Java 等现在可以正常启动)
311+
if is_first_deployment {
312+
info!("⏳ 等待所有服务完全启动...");
313+
} else {
314+
info!("⏳ 阶段 2/2: 等待所有服务完全启动...");
315+
}
316+
if docker_utils::wait_for_compose_services_started(&compose_path, sql::OTHER_SERVICES_TIMEOUT)
317+
.await?
318+
{
319+
info!("✅ 自动升级部署完成,服务已成功启动");
300320
info!("🎉 自动升级部署流程成功完成");
301321
} else {
302322
warn!("⚠️ 等待服务启动超时,请手动检查服务状态");
@@ -305,11 +325,6 @@ pub async fn run_auto_upgrade_deploy(
305325
match check_docker_service_status(app, &config_file, &project_name).await {
306326
Ok(true) => {
307327
info!("🔍 最终检查:服务似乎已正常启动");
308-
309-
// 🔄 如果服务正常,尝试执行数据库升级
310-
if !is_first_deployment {
311-
execute_sql_diff_upgrade(&config_file).await?;
312-
}
313328
}
314329
Ok(false) => {
315330
info!("🔍 最终检查:服务可能未正常启动");

nuwax-cli/src/docker_utils.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,3 +270,23 @@ pub async fn wait_for_compose_services_started(
270270
let filter = create_compose_filter(compose_file_path).await?;
271271
wait_for_services_started(&filter, timeout_secs).await
272272
}
273+
274+
/// 等待 MySQL 服务就绪
275+
///
276+
/// 使用服务名 "mysql" 过滤容器,等待 MySQL 容器启动完成。
277+
/// 此函数用于解决 MySQL-Java 服务启动死锁问题:
278+
/// - Java 容器健康检查依赖 MySQL 表结构升级
279+
/// - SQL 升级需要 MySQL 先就绪
280+
///
281+
/// # 参数
282+
/// - `_compose_path`: docker-compose 文件路径(保留以便未来扩展)
283+
/// - `timeout_secs`: 超时时间(秒)
284+
///
285+
/// # 返回
286+
/// - `Ok(true)`: MySQL 服务已启动
287+
/// - `Ok(false)`: 超时,MySQL 服务未能启动
288+
/// - `Err`: 检查过程中发生错误
289+
pub async fn wait_for_mysql_ready(_compose_path: &Path, timeout_secs: u64) -> Result<bool> {
290+
let filter = ServiceFilter::NameContains(vec!["mysql".to_string()]);
291+
wait_for_services_started(&filter, timeout_secs).await
292+
}

0 commit comments

Comments
 (0)