File tree Expand file tree Collapse file tree 1 file changed +37
-2
lines changed Expand file tree Collapse file tree 1 file changed +37
-2
lines changed Original file line number Diff line number Diff line change @@ -32,6 +32,41 @@ def validate_json(message: Any, schema: Any) -> None:
3232 assert False , "The provided schema is faulty:" + str (e )
3333
3434
35+ def wait_for_container_health (container_name : str , max_attempts : int = 3 ) -> None :
36+ """Wait for container to be healthy."""
37+ for attempt in range (max_attempts ):
38+ try :
39+ result = subprocess .run (
40+ [
41+ "docker" ,
42+ "inspect" ,
43+ "--format={{.State.Health.Status}}" ,
44+ container_name ,
45+ ],
46+ capture_output = True ,
47+ text = True ,
48+ check = True ,
49+ timeout = 10 ,
50+ )
51+ if result .stdout .strip () == "healthy" :
52+ break
53+ else :
54+ if attempt < max_attempts - 1 :
55+ time .sleep (5 )
56+ else :
57+ print (
58+ f"{ container_name } not healthy after { max_attempts * 5 } seconds"
59+ )
60+ except (subprocess .CalledProcessError , subprocess .TimeoutExpired ):
61+ pass
62+
63+ if attempt < max_attempts - 1 :
64+ print (f"⏱ Attempt { attempt + 1 } /{ max_attempts } - waiting..." )
65+ time .sleep (5 )
66+ else :
67+ print (f"Could not check health status for { container_name } " )
68+
69+
3570def switch_config_and_restart (
3671 original_file : str ,
3772 replacement_file : str ,
@@ -76,8 +111,8 @@ def switch_config_and_restart(
76111 print (f"Failed to restart container { container_name } : { e .stderr } " )
77112 raise
78113
79- # Wait for container to be ready
80- time . sleep ( 5 )
114+ # Wait for container to be healthy
115+ wait_for_container_health ( container_name )
81116
82117 # Clean up backup file
83118 if cleanup and os .path .exists (backup_file ):
You can’t perform that action at this time.
0 commit comments