1111 Callable ,
1212 Coroutine ,
1313)
14+ from math import log
1415from pathlib import Path
1516from pprint import pformat
1617from typing import Any , Final , cast
@@ -221,9 +222,24 @@ async def _parse_container_log_file( # noqa: PLR0913 # pylint: disable=too-many
221222 logging .DEBUG ,
222223 "started monitoring of pre-1.0 service - using log file in /logs folder" ,
223224 ):
224- async with aiofiles .open (log_file ) as file_pointer :
225- while (await container .show ())["State" ]["Running" ]:
226- if line := await file_pointer .readline ():
225+ try :
226+ async with aiofiles .open (log_file ) as file_pointer :
227+ while (await container .show ())["State" ]["Running" ]:
228+ if line := await file_pointer .readline ():
229+ _logger .info (
230+ "[%s]: %s" ,
231+ f"{ service_key } :{ service_version } - { container .id } { container_name } " ,
232+ line ,
233+ )
234+ await _parse_and_publish_logs (
235+ line ,
236+ task_publishers = task_publishers ,
237+ progress_regexp = progress_regexp ,
238+ progress_bar = progress_bar ,
239+ )
240+
241+ # finish reading the logs if possible
242+ async for line in file_pointer :
227243 _logger .info (
228244 "[%s]: %s" ,
229245 f"{ service_key } :{ service_version } - { container .id } { container_name } " ,
@@ -235,25 +251,12 @@ async def _parse_container_log_file( # noqa: PLR0913 # pylint: disable=too-many
235251 progress_regexp = progress_regexp ,
236252 progress_bar = progress_bar ,
237253 )
238-
239- # finish reading the logs if possible
240- async for line in file_pointer :
241- _logger .info (
242- "[%s]: %s" ,
243- f"{ service_key } :{ service_version } - { container .id } { container_name } " ,
244- line ,
245- )
246- await _parse_and_publish_logs (
247- line ,
248- task_publishers = task_publishers ,
249- progress_regexp = progress_regexp ,
250- progress_bar = progress_bar ,
251- )
252-
254+ finally :
253255 # copy the log file to the log_file_url
254- await push_file_to_remote (
255- log_file , log_file_url , log_publishing_cb , s3_settings
256- )
256+ if log_file .exists ():
257+ await push_file_to_remote (
258+ log_file , log_file_url , log_publishing_cb , s3_settings
259+ )
257260
258261
259262_MINUTE : Final [int ] = 60
@@ -279,12 +282,13 @@ async def _parse_container_docker_logs(
279282 Raises:
280283 ServiceTimeoutLoggingError: raised when no logs are received for longer than _AIODOCKER_LOGS_TIMEOUT_S
281284 """
282- try :
283- with log_context (
284- _logger ,
285- logging .DEBUG ,
286- "started monitoring of >=1.0 service - using docker logs" ,
287- ):
285+
286+ with log_context (
287+ _logger ,
288+ logging .DEBUG ,
289+ "started monitoring of >=1.0 service - using docker logs" ,
290+ ):
291+ try :
288292 assert isinstance (
289293 container .docker .connector , aiohttp .UnixConnector
290294 ) # nosec
@@ -336,17 +340,19 @@ async def _parse_container_docker_logs(
336340 progress_bar = progress_bar ,
337341 )
338342
339- # copy the log file to the log_file_url
340- await push_file_to_remote (
341- log_file_path , log_file_url , log_publishing_cb , s3_settings
342- )
343- except TimeoutError as e :
344- raise ServiceTimeoutLoggingError (
345- service_key = service_key ,
346- service_version = service_version ,
347- container_id = container .id ,
348- timeout_timedelta = datetime .timedelta (seconds = _AIODOCKER_LOGS_TIMEOUT_S ),
349- ) from e
343+ except TimeoutError as e :
344+ raise ServiceTimeoutLoggingError (
345+ service_key = service_key ,
346+ service_version = service_version ,
347+ container_id = container .id ,
348+ timeout_timedelta = datetime .timedelta (seconds = _AIODOCKER_LOGS_TIMEOUT_S ),
349+ ) from e
350+ finally :
351+ if log_file_path .exists ():
352+ # copy the log file to the log_file_url
353+ await push_file_to_remote (
354+ log_file_path , log_file_url , log_publishing_cb , s3_settings
355+ )
350356
351357
352358async def _monitor_container_logs ( # noqa: PLR0913 # pylint: disable=too-many-arguments
0 commit comments