Skip to content

Commit

Permalink
Added logging of the exception caused during the future (#242)
Browse files Browse the repository at this point in the history
Closes #241
  • Loading branch information
jcadam14 authored May 22, 2024
1 parent 89d6e4c commit 64ce651
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
17 changes: 11 additions & 6 deletions src/sbl_filing_api/services/multithread_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@ def handle_submission(period_code: str, lei: str, submission: SubmissionDAO, con

async def check_future(future, submission_id, exec_check):
await asyncio.sleep(settings.expired_submission_check_secs)
if future.done() and future.exception():
future.cancel()
exec_check["continue"] = False
await repo.error_out_submission(submission_id)
logger.error(f"Validation for submission {submission_id} did not complete due to an unexpected error.")
elif not future.done():
try:
future.result()
except asyncio.InvalidStateError:
future.cancel()
exec_check["continue"] = False
await repo.expire_submission(submission_id)
logger.warning(
f"Validation for submission {submission_id} did not complete within the expected timeframe, will be set to VALIDATION_EXPIRED."
)
except Exception:
exec_check["continue"] = False
await repo.error_out_submission(submission_id)
logger.error(
f"Validation for submission {submission_id} did not complete due to an unexpected error.",
exc_info=True,
stack_info=True,
)
10 changes: 7 additions & 3 deletions tests/services/test_multithreader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
from concurrent.futures.process import BrokenProcessPool

from multiprocessing import Manager
from pytest_mock import MockerFixture
Expand All @@ -14,7 +15,7 @@ async def mock_future(self, sleeptime):

async def mock_future_exception(self):
await asyncio.sleep(2)
raise asyncio.InvalidStateError("Pool died.")
raise BrokenProcessPool("Pool died.")

async def test_future_checker(self, mocker: MockerFixture):
exec_check = Manager().dict()
Expand All @@ -30,9 +31,12 @@ async def test_future_checker(self, mocker: MockerFixture):
await check_future(future, 1, exec_check)

assert not exec_check["continue"]
cancel_mock.assert_called_once()
error_mock.assert_called_with(1)
log_mock.error.assert_called_with("Validation for submission 1 did not complete due to an unexpected error.")
log_mock.error.assert_called_with(
"Validation for submission 1 did not complete due to an unexpected error.",
exc_info=True,
stack_info=True,
)

log_mock.reset_mock()

Expand Down

0 comments on commit 64ce651

Please sign in to comment.