1212from pydantic import BaseModel
1313from servicelib .fastapi .requests_decorators import cancel_on_disconnect
1414
15- from ..core .docker_compose_utils import docker_compose_down , docker_compose_up
15+ from ..core .docker_compose_utils import (
16+ docker_compose_down ,
17+ docker_compose_rm ,
18+ docker_compose_up ,
19+ )
1620from ..core .docker_logs import start_log_fetching , stop_log_fetching
1721from ..core .docker_utils import docker_client
1822from ..core .rabbitmq import RabbitMQ
@@ -50,18 +54,21 @@ async def _task_docker_compose_up_and_send_message(
5054 app : FastAPI ,
5155 application_health : ApplicationHealth ,
5256 rabbitmq : RabbitMQ ,
53- command_timeout : float ,
57+ command_timeout : int ,
5458) -> None :
5559 # building is a security risk hence is disabled via "--no-build" parameter
5660 await send_message (rabbitmq , "starting service containers" )
5761 assert shared_store .compose_spec # nosec
5862
5963 with directory_watcher_disabled (app ):
64+ # prunes first stopped containers
65+ await docker_compose_rm (shared_store .compose_spec , settings )
66+
6067 r = await docker_compose_up (
61- shared_store , settings , command_timeout = command_timeout
68+ shared_store . compose_spec , settings , timeout = command_timeout
6269 )
6370
64- message = f"Finished docker-compose up with output\n { r .decoded_stdout } "
71+ message = f"Finished docker-compose up with output\n { r .message } "
6572
6673 if r .success :
6774 await send_message (rabbitmq , "service containers started" )
@@ -117,11 +124,11 @@ async def create_containers(
117124 request : Request ,
118125 containers_create : ContainersCreate ,
119126 background_tasks : BackgroundTasks ,
120- command_timeout : float = Query (
121- 3600.0 , description = "docker-compose up command timeout run as a background"
127+ command_timeout : int = Query (
128+ 3600 , description = "docker-compose up command timeout run as a background"
122129 ),
123- validation_timeout : float = Query (
124- 60.0 , description = "docker-compose config timeout (EXPERIMENTAL)"
130+ validation_timeout : int = Query (
131+ 60 , description = "docker-compose config timeout (EXPERIMENTAL)"
125132 ),
126133 settings : DynamicSidecarSettings = Depends (get_settings ),
127134 shared_store : SharedStore = Depends (get_shared_store ),
@@ -176,8 +183,8 @@ async def create_containers(
176183 },
177184)
178185async def runs_docker_compose_down (
179- command_timeout : float = Query (
180- 10.0 , description = "docker-compose down command timeout default (EXPERIMENTAL)"
186+ command_timeout : int = Query (
187+ 10 , description = "docker-compose down command timeout default (EXPERIMENTAL)"
181188 ),
182189 settings : DynamicSidecarSettings = Depends (get_settings ),
183190 shared_store : SharedStore = Depends (get_shared_store ),
@@ -193,24 +200,28 @@ async def runs_docker_compose_down(
193200 )
194201
195202 result = await docker_compose_down (
196- shared_store = shared_store ,
197- settings = settings ,
198- command_timeout = command_timeout ,
203+ shared_store . compose_spec ,
204+ settings ,
205+ timeout = min ( command_timeout , settings . DYNAMIC_SIDECAR_STOP_AND_REMOVE_TIMEOUT ) ,
199206 )
200207
201208 for container_name in shared_store .container_names :
202209 await stop_log_fetching (app , container_name )
203210
211+ await docker_compose_rm (shared_store .compose_spec , settings )
212+
204213 if not result .success :
205214 logger .warning (
206215 "docker-compose down command finished with errors\n %s" ,
207- result .decoded_stdout ,
208- )
209- raise HTTPException (
210- status .HTTP_422_UNPROCESSABLE_ENTITY , detail = result .decoded_stdout
216+ result .message ,
211217 )
218+ raise HTTPException (status .HTTP_422_UNPROCESSABLE_ENTITY , detail = result .message )
219+
220+ # removing compose-file spec
221+ assert result .success # nosec
222+ shared_store .clear ()
212223
213- return result .decoded_stdout
224+ return result .message
214225
215226
216227@containers_router .get (
0 commit comments