Skip to content

Commit

Permalink
Store exception traceback in logs f pipeline fails (#331)
Browse files Browse the repository at this point in the history
* add test

* store failure info
  • Loading branch information
zigaLuksic authored Mar 8, 2024
1 parent febdd59 commit f905255
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
10 changes: 10 additions & 0 deletions eogrow/core/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

import logging
import time
import traceback
import uuid
from typing import Any, TypeVar

import fs
import ray

from eolearn.core import CreateEOPatchTask, EOExecutor, EONode, EOWorkflow, LoadTask, SaveTask, WorkflowResults
Expand Down Expand Up @@ -255,6 +257,14 @@ def run(self) -> None:

if failed and self.config.raise_on_failure:
raise PipelineExecutionError(f"Pipeline failed some executions. Check {log_folder}.")
except Exception as e:
# store any exception info you can
failure_log_path = fs.path.join(
self.logging_manager.get_pipeline_logs_folder(self.current_execution_name), "failure.log"
)
with self.storage.filesystem.open(failure_log_path, mode="w") as log_file:
log_file.writelines(traceback.format_exc())
raise e
finally:
self.logging_manager.stop_logging(root_logger, handlers)

Expand Down
19 changes: 19 additions & 0 deletions tests/core/test_pipeline.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging
import os
from contextlib import suppress
from pathlib import Path
from typing import List, Tuple, Union

import pytest
Expand Down Expand Up @@ -98,6 +100,23 @@ def test_get_patch_list_filtration_error(test_subset: List[Union[int, str]], sim
pipeline.get_patch_list()


def test_pipeline_logs_exception(simple_config_filename: str) -> None:
def cookie_alarm():
raise RuntimeError("Oh no, someone stole my cookie! :(")

config = interpret_config_from_path(simple_config_filename)
pipeline = SimplePipeline.from_raw_config(config)
pipeline.run_procedure = cookie_alarm

with suppress(RuntimeError):
pipeline.run()

log_folder = pipeline.logging_manager.get_pipeline_logs_folder(pipeline.current_execution_name, full_path=True)
log_path = Path(log_folder) / "failure.log"
assert log_path.exists()
assert "Oh no, someone stole my cookie! :(" in log_path.read_text()


@pytest.mark.parametrize("fail", [True, False])
@pytest.mark.parametrize("raise_on_failure", [True, False])
def test_pipeline_raises_on_failure(fail: bool, raise_on_failure: bool, simple_config_filename: str):
Expand Down

0 comments on commit f905255

Please sign in to comment.