1010
1111import aiofiles
1212import aiofiles .tempfile
13+ import aiohttp
1314import arrow
1415from aiodocker import Docker , DockerError
1516from aiodocker .containers import DockerContainer
@@ -268,42 +269,57 @@ async def _parse_container_docker_logs(
268269 with log_context (
269270 logger , logging .DEBUG , "started monitoring of >=1.0 service - using docker logs"
270271 ):
271- async with aiofiles .tempfile .TemporaryDirectory () as tmp_dir :
272- log_file_path = (
273- Path (tmp_dir )
274- / f"{ service_key .split (sep = '/' )[- 1 ]} _{ service_version } .logs"
272+ assert isinstance (container .docker .connector , aiohttp .UnixConnector ) # nosec
273+ async with Docker (
274+ session = aiohttp .ClientSession (
275+ connector = aiohttp .UnixConnector (container .docker .connector .path ),
276+ timeout = aiohttp .ClientTimeout (total = _AIODOCKER_LOGS_TIMEOUT_S ),
275277 )
276- log_file_path .parent .mkdir (parents = True , exist_ok = True )
277- async with aiofiles .open (log_file_path , mode = "wb+" ) as log_fp :
278- async for log_line in cast (
279- AsyncGenerator [str , None ],
280- container .log (
281- stdout = True ,
282- stderr = True ,
283- follow = True ,
284- timestamps = True ,
285- timeout = _AIODOCKER_LOGS_TIMEOUT_S ,
286- ),
287- ):
288- log_msg_without_timestamp = log_line .split (" " , maxsplit = 1 )[1 ]
289- logger .info (
290- "[%s]: %s" ,
291- f"{ service_key } :{ service_version } - { container .id } { container_name } " ,
292- log_msg_without_timestamp ,
293- )
294- await log_fp .write (log_line .encode ("utf-8" ))
295- # NOTE: here we remove the timestamp, only needed for the file
296- await _parse_and_publish_logs (
297- log_msg_without_timestamp ,
298- task_publishers = task_publishers ,
299- progress_regexp = progress_regexp ,
300- progress_bar = progress_bar ,
301- )
302-
303- # copy the log file to the log_file_url
304- await push_file_to_remote (
305- log_file_path , log_file_url , log_publishing_cb , s3_settings
278+ ) as docker_client_for_logs :
279+ # NOTE: this is a workaround for aiodocker not being able to get the container
280+ # logs when the container is not running
281+ container_for_long_running_logs = (
282+ await docker_client_for_logs .containers .get (container .id )
306283 )
284+ # NOTE: this is a workaround for aiodocker not being able to get the container
285+ # logs when the container is not running
286+ await container .show ()
287+ await container_for_long_running_logs .show ()
288+ async with aiofiles .tempfile .TemporaryDirectory () as tmp_dir :
289+ log_file_path = (
290+ Path (tmp_dir )
291+ / f"{ service_key .split (sep = '/' )[- 1 ]} _{ service_version } .logs"
292+ )
293+ log_file_path .parent .mkdir (parents = True , exist_ok = True )
294+ async with aiofiles .open (log_file_path , mode = "wb+" ) as log_fp :
295+ async for log_line in cast (
296+ AsyncGenerator [str , None ],
297+ container_for_long_running_logs .log (
298+ stdout = True ,
299+ stderr = True ,
300+ follow = True ,
301+ timestamps = True ,
302+ ),
303+ ):
304+ log_msg_without_timestamp = log_line .split (" " , maxsplit = 1 )[1 ]
305+ logger .info (
306+ "[%s]: %s" ,
307+ f"{ service_key } :{ service_version } - { container_for_long_running_logs .id } { container_name } " ,
308+ log_msg_without_timestamp ,
309+ )
310+ await log_fp .write (log_line .encode ("utf-8" ))
311+ # NOTE: here we remove the timestamp, only needed for the file
312+ await _parse_and_publish_logs (
313+ log_msg_without_timestamp ,
314+ task_publishers = task_publishers ,
315+ progress_regexp = progress_regexp ,
316+ progress_bar = progress_bar ,
317+ )
318+
319+ # copy the log file to the log_file_url
320+ await push_file_to_remote (
321+ log_file_path , log_file_url , log_publishing_cb , s3_settings
322+ )
307323
308324
309325async def _monitor_container_logs ( # noqa: PLR0913 # pylint: disable=too-many-arguments
@@ -325,6 +341,7 @@ async def _monitor_container_logs( # noqa: PLR0913 # pylint: disable=too-many-a
325341 Services above are not creating a file and use the usual docker logging. These logs
326342 are retrieved using the usual cli 'docker logs CONTAINERID'
327343 """
344+
328345 with log_catch (logger , reraise = False ):
329346 container_info = await container .show ()
330347 container_name = container_info .get ("Name" , "undefined" )
0 commit comments