3
3
import logging
4
4
import re
5
5
import socket
6
+ from collections .abc import AsyncGenerator , AsyncIterator , Awaitable , Callable
6
7
from pathlib import Path
7
8
from pprint import pformat
8
- from typing import Any , AsyncGenerator , AsyncIterator , Awaitable , Callable , cast
9
+ from typing import Any , Final , cast
9
10
10
11
import aiofiles
11
12
import aiofiles .tempfile
@@ -151,15 +152,20 @@ def _guess_progress_value(progress_match: re.Match[str]) -> float:
151
152
return float (value_str .strip ())
152
153
153
154
155
+ _OSPARC_LOG_NUM_PARTS : Final [int ] = 2
156
+
157
+
154
158
async def _try_parse_progress (
155
159
line : str , * , progress_regexp : re .Pattern [str ]
156
160
) -> float | None :
157
161
with log_catch (logger , reraise = False ):
158
162
# pattern might be like "timestamp log"
159
163
log = line .strip ("\n " )
160
164
splitted_log = log .split (" " , maxsplit = 1 )
161
- with contextlib .suppress (arrow .ParserError ):
162
- if len (splitted_log ) == 2 and arrow .get (splitted_log [0 ]):
165
+ with contextlib .suppress (arrow .ParserError , ValueError ):
166
+ if len (splitted_log ) == _OSPARC_LOG_NUM_PARTS and arrow .get (
167
+ splitted_log [0 ]
168
+ ):
163
169
log = splitted_log [1 ]
164
170
if match := re .search (progress_regexp , log ):
165
171
return _guess_progress_value (match )
@@ -172,19 +178,23 @@ async def _parse_and_publish_logs(
172
178
* ,
173
179
task_publishers : TaskPublisher ,
174
180
progress_regexp : re .Pattern [str ],
181
+ container_processing_progress_weight : float ,
175
182
) -> None :
176
183
progress_value = await _try_parse_progress (
177
184
log_line , progress_regexp = progress_regexp
178
185
)
186
+ assert 0 < container_processing_progress_weight <= 1.0 # nosec # noqa: PLR2004
179
187
if progress_value is not None :
180
- task_publishers .publish_progress (progress_value )
188
+ task_publishers .publish_progress (
189
+ container_processing_progress_weight * progress_value
190
+ )
181
191
182
192
task_publishers .publish_logs (
183
193
message = log_line , log_level = guess_message_log_level (log_line )
184
194
)
185
195
186
196
187
- async def _parse_container_log_file (
197
+ async def _parse_container_log_file ( # noqa: PLR0913 # pylint: disable=too-many-arguments
188
198
* ,
189
199
container : DockerContainer ,
190
200
progress_regexp : re .Pattern [str ],
@@ -196,6 +206,7 @@ async def _parse_container_log_file(
196
206
log_file_url : LogFileUploadURL ,
197
207
log_publishing_cb : LogPublishingCB ,
198
208
s3_settings : S3Settings | None ,
209
+ max_monitoring_progress_value : float ,
199
210
) -> None :
200
211
log_file = task_volumes .logs_folder / LEGACY_SERVICE_LOG_FILE_NAME
201
212
with log_context (
@@ -215,6 +226,7 @@ async def _parse_container_log_file(
215
226
line ,
216
227
task_publishers = task_publishers ,
217
228
progress_regexp = progress_regexp ,
229
+ container_processing_progress_weight = max_monitoring_progress_value ,
218
230
)
219
231
220
232
# finish reading the logs if possible
@@ -228,6 +240,7 @@ async def _parse_container_log_file(
228
240
line ,
229
241
task_publishers = task_publishers ,
230
242
progress_regexp = progress_regexp ,
243
+ container_processing_progress_weight = max_monitoring_progress_value ,
231
244
)
232
245
233
246
# copy the log file to the log_file_url
@@ -247,6 +260,7 @@ async def _parse_container_docker_logs(
247
260
log_file_url : LogFileUploadURL ,
248
261
log_publishing_cb : LogPublishingCB ,
249
262
s3_settings : S3Settings | None ,
263
+ container_processing_progress_weight : float ,
250
264
) -> None :
251
265
with log_context (
252
266
logger , logging .DEBUG , "started monitoring of >=1.0 service - using docker logs"
@@ -276,6 +290,7 @@ async def _parse_container_docker_logs(
276
290
log_msg_without_timestamp ,
277
291
task_publishers = task_publishers ,
278
292
progress_regexp = progress_regexp ,
293
+ container_processing_progress_weight = container_processing_progress_weight ,
279
294
)
280
295
281
296
# copy the log file to the log_file_url
@@ -284,7 +299,7 @@ async def _parse_container_docker_logs(
284
299
)
285
300
286
301
287
- async def _monitor_container_logs (
302
+ async def _monitor_container_logs ( # noqa: PLR0913 # pylint: disable=too-many-arguments
288
303
* ,
289
304
container : DockerContainer ,
290
305
progress_regexp : re .Pattern [str ],
@@ -296,6 +311,7 @@ async def _monitor_container_logs(
296
311
log_file_url : LogFileUploadURL ,
297
312
log_publishing_cb : LogPublishingCB ,
298
313
s3_settings : S3Settings | None ,
314
+ container_processing_progress_weight : float ,
299
315
) -> None :
300
316
"""Services running with integration version 0.0.0 are logging into a file
301
317
that must be available in task_volumes.log / log.dat
@@ -321,6 +337,7 @@ async def _monitor_container_logs(
321
337
log_file_url = log_file_url ,
322
338
log_publishing_cb = log_publishing_cb ,
323
339
s3_settings = s3_settings ,
340
+ container_processing_progress_weight = container_processing_progress_weight ,
324
341
)
325
342
else :
326
343
await _parse_container_log_file (
@@ -334,11 +351,12 @@ async def _monitor_container_logs(
334
351
log_file_url = log_file_url ,
335
352
log_publishing_cb = log_publishing_cb ,
336
353
s3_settings = s3_settings ,
354
+ max_monitoring_progress_value = container_processing_progress_weight ,
337
355
)
338
356
339
357
340
358
@contextlib .asynccontextmanager
341
- async def managed_monitor_container_log_task (
359
+ async def managed_monitor_container_log_task ( # noqa: PLR0913 # pylint: disable=too-many-arguments
342
360
container : DockerContainer ,
343
361
progress_regexp : re .Pattern [str ],
344
362
service_key : ContainerImage ,
@@ -349,6 +367,7 @@ async def managed_monitor_container_log_task(
349
367
log_file_url : LogFileUploadURL ,
350
368
log_publishing_cb : LogPublishingCB ,
351
369
s3_settings : S3Settings | None ,
370
+ container_processing_progress_weight : float ,
352
371
) -> AsyncIterator [Awaitable [None ]]:
353
372
monitoring_task = None
354
373
try :
@@ -369,6 +388,7 @@ async def managed_monitor_container_log_task(
369
388
log_file_url = log_file_url ,
370
389
log_publishing_cb = log_publishing_cb ,
371
390
s3_settings = s3_settings ,
391
+ container_processing_progress_weight = container_processing_progress_weight ,
372
392
),
373
393
name = f"{ service_key } :{ service_version } _{ container .id } _monitoring_task" ,
374
394
)
0 commit comments