|
20 | 20 | OrchestrationType, |
21 | 21 | StorageEngine, |
22 | 22 | ) |
23 | | -from clp_py_utils.clp_logging import configure_logging, get_logger |
| 23 | +from clp_py_utils.clp_logging import bind_log_context, configure_logging, get_logger |
24 | 24 | from clp_py_utils.clp_metadata_db_utils import ( |
25 | 25 | add_dataset, |
26 | 26 | fetch_existing_datasets, |
@@ -308,14 +308,15 @@ def search_and_schedule_new_tasks( |
308 | 308 | # TODO: revisit why we need to commit here. To end long transactions? |
309 | 309 | db_context.connection.commit() |
310 | 310 | for job_row in jobs: |
311 | | - _schedule_job( |
312 | | - clp_config, |
313 | | - clp_metadata_db_connection_config, |
314 | | - task_manager, |
315 | | - db_context, |
316 | | - job_row, |
317 | | - existing_datasets, |
318 | | - ) |
| 311 | + with bind_log_context(job_id=job_row["id"]): |
| 312 | + _schedule_job( |
| 313 | + clp_config, |
| 314 | + clp_metadata_db_connection_config, |
| 315 | + task_manager, |
| 316 | + db_context, |
| 317 | + job_row, |
| 318 | + existing_datasets, |
| 319 | + ) |
319 | 320 |
|
320 | 321 |
|
321 | 322 | def _schedule_job( |
@@ -474,54 +475,58 @@ def poll_running_jobs( |
474 | 475 | logger.debug("Poll running jobs") |
475 | 476 | jobs_to_delete = [] |
476 | 477 | for job_id, job in scheduled_jobs.items(): |
477 | | - job_success = True |
478 | | - duration = 0.0 |
479 | | - error_messages: list[str] = [] |
480 | | - num_tasks_in_batch = 0 |
| 478 | + with bind_log_context(job_id=job_id): |
| 479 | + job_success = True |
| 480 | + duration = 0.0 |
| 481 | + error_messages: list[str] = [] |
| 482 | + num_tasks_in_batch = 0 |
481 | 483 |
|
482 | | - try: |
483 | | - returned_results = job.result_handle.get_result() |
484 | | - if returned_results is None: |
485 | | - continue |
| 484 | + try: |
| 485 | + returned_results = job.result_handle.get_result() |
| 486 | + if returned_results is None: |
| 487 | + continue |
486 | 488 |
|
487 | | - duration = ( |
488 | | - datetime.datetime.now(datetime.timezone.utc) - job.start_time |
489 | | - ).total_seconds() |
490 | | - # Check for finished jobs |
491 | | - num_tasks_in_batch = len(returned_results) |
492 | | - for task_result in returned_results: |
493 | | - if task_result.status == CompressionTaskStatus.SUCCEEDED: |
494 | | - logger.info( |
495 | | - f"Compression task job-{job_id}-task-{task_result.task_id} completed in" |
496 | | - f" {task_result.duration} second(s)." |
497 | | - ) |
498 | | - else: |
499 | | - job_success = False |
500 | | - error_messages.append( |
501 | | - f"task {task_result.task_id}: {task_result.error_message}" |
502 | | - ) |
503 | | - logger.error( |
504 | | - f"Compression task job-{job_id}-task-{task_result.task_id} failed with" |
505 | | - f" error: {task_result.error_message}." |
506 | | - ) |
| 489 | + duration = ( |
| 490 | + datetime.datetime.now(datetime.timezone.utc) - job.start_time |
| 491 | + ).total_seconds() |
| 492 | + # Check for finished jobs |
| 493 | + num_tasks_in_batch = len(returned_results) |
| 494 | + for task_result in returned_results: |
| 495 | + with bind_log_context(task_id=task_result.task_id): |
| 496 | + if task_result.status == CompressionTaskStatus.SUCCEEDED: |
| 497 | + logger.info( |
| 498 | + f"Compression task job-{job_id}-task-{task_result.task_id} completed in" |
| 499 | + f" {task_result.duration} second(s)." |
| 500 | + ) |
| 501 | + else: |
| 502 | + job_success = False |
| 503 | + error_messages.append( |
| 504 | + f"task {task_result.task_id}: {task_result.error_message}" |
| 505 | + ) |
| 506 | + logger.error( |
| 507 | + f"Compression task job-{job_id}-task-{task_result.task_id} failed with" |
| 508 | + f" error: {task_result.error_message}." |
| 509 | + ) |
507 | 510 |
|
508 | | - except Exception: |
509 | | - logger.exception("Error while getting results for job %s", job_id) |
510 | | - job_success = False |
| 511 | + except Exception: |
| 512 | + logger.exception("Error while getting results for job %s", job_id) |
| 513 | + job_success = False |
511 | 514 |
|
512 | | - if not job_success: |
513 | | - _handle_failed_compression_job(logs_directory, db_context, job_id, error_messages) |
514 | | - jobs_to_delete.append(job_id) |
515 | | - continue |
| 515 | + if not job_success: |
| 516 | + _handle_failed_compression_job(logs_directory, db_context, job_id, error_messages) |
| 517 | + jobs_to_delete.append(job_id) |
| 518 | + continue |
516 | 519 |
|
517 | | - job.num_tasks_completed += num_tasks_in_batch |
| 520 | + job.num_tasks_completed += num_tasks_in_batch |
518 | 521 |
|
519 | | - if len(job.remaining_tasks) > 0: |
520 | | - _dispatch_next_task_batch(task_manager, db_context, job, max_concurrent_tasks_per_job) |
521 | | - else: |
522 | | - # All tasks completed successfully |
523 | | - _complete_compression_job(db_context, job_id, job.num_tasks_total, duration) |
524 | | - jobs_to_delete.append(job_id) |
| 522 | + if len(job.remaining_tasks) > 0: |
| 523 | + _dispatch_next_task_batch( |
| 524 | + task_manager, db_context, job, max_concurrent_tasks_per_job |
| 525 | + ) |
| 526 | + else: |
| 527 | + # All tasks completed successfully |
| 528 | + _complete_compression_job(db_context, job_id, job.num_tasks_total, duration) |
| 529 | + jobs_to_delete.append(job_id) |
525 | 530 |
|
526 | 531 | for job_id in jobs_to_delete: |
527 | 532 | del scheduled_jobs[job_id] |
|
0 commit comments