generated from cfpb/open-source-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
131 look into setting time limit on background validation task #159
Merged
lchen-2101
merged 11 commits into
main
from
131-look-into-setting-time-limit-on-background-validation-task
Apr 18, 2024
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ac50621
Added a @repeat_every check, env vars, VALIDATION_EXPIRED state and r…
jcadam14 913a9d0
Removed comment
jcadam14 e3c9296
Updated pytests
jcadam14 30c7b6e
Merge branch 'main' into 131-look-into-setting-time-limit-on-backgrou…
jcadam14 594f00c
Flipped time delta check and added a logger.warn message
jcadam14 dada53e
Linting
jcadam14 89e25e6
Merge branch 'main' into 131-look-into-setting-time-limit-on-backgrou…
jcadam14 666bf66
Rewrite using asyncio
jcadam14 fadcebf
Added exception handling prior to the background_task
jcadam14 e0a9745
Removed unneeded env var, updated default timeout sec to 60
jcadam14 8335647
poetry removed fastapi-utlities
jcadam14 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
db_revisions/versions/0040045eae14_add_validation_expired_state.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"""Add VALIDATION_EXPIRED state | ||
|
||
Revision ID: 0040045eae14 | ||
Revises: fb46d55283d6 | ||
Create Date: 2024-04-11 13:08:20.850470 | ||
|
||
""" | ||
from typing import Sequence, Union | ||
|
||
from alembic import op, context | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "0040045eae14" | ||
down_revision: Union[str, None] = "ccc50ec18a7e" | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
# fmt: off | ||
old_options = ( | ||
'SUBMISSION_ACCEPTED', | ||
'SUBMISSION_STARTED', | ||
'SUBMISSION_UPLOADED', | ||
'SUBMISSION_UPLOAD_MALFORMED', | ||
'VALIDATION_IN_PROGRESS', | ||
'VALIDATION_WITH_ERRORS', | ||
'VALIDATION_WITH_WARNINGS', | ||
'VALIDATION_SUCCESSFUL', | ||
) | ||
new_options = tuple(sorted(old_options + ('VALIDATION_EXPIRED','UPLOAD_FAILED'))) | ||
# fmt: on | ||
|
||
|
||
def upgrade() -> None: | ||
if "sqlite" not in context.get_context().dialect.name: | ||
op.execute("ALTER TYPE submissionstate RENAME TO submissionstate_old") | ||
op.execute(f"CREATE TYPE submissionstate AS ENUM{new_options}") | ||
op.execute("ALTER TABLE submission ALTER COLUMN state TYPE submissionstate USING state::text::submissionstate") | ||
op.execute("DROP TYPE submissionstate_old") | ||
|
||
|
||
def downgrade() -> None: | ||
if "sqlite" not in context.get_context().dialect.name: | ||
op.execute("ALTER TYPE submissionstate RENAME TO submissionstate_old") | ||
op.execute(f"CREATE TYPE submissionstate AS ENUM{old_options}") | ||
op.execute("ALTER TABLE submission ALTER COLUMN state TYPE submissionstate USING state::text::submissionstate") | ||
op.execute("DROP TYPE submissionstate_old") |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,6 @@ JWT_OPTS_VERIFY_ISS="false" | |
FS_UPLOAD_CONFIG__PROTOCOL="file" | ||
FS_UPLOAD_CONFIG__ROOT="../upload" | ||
FS_UPLOAD_CONFIG__MKDIR=true | ||
FS_DOWNLOAD_CONFIG__PROTOCOL="file" | ||
FS_DOWNLOAD_CONFIG__PROTOCOL="file" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this one isn't needed anymore, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope thank you, removing |
||
EXPIRED_SUBMISSION_CHECK_SECS=60 | ||
EXPIRED_SUBMISSION_DIFF_SECS=300 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -215,9 +215,7 @@ def test_authed_upload_file( | |
mock_validate_file.return_value = None | ||
mock_upload = mocker.patch("sbl_filing_api.services.submission_processor.upload_to_storage") | ||
mock_upload.return_value = None | ||
mock_validate_submission = mocker.patch( | ||
"sbl_filing_api.services.submission_processor.validate_and_update_submission" | ||
) | ||
mock_validate_submission = mocker.patch("sbl_filing_api.services.submission_processor.validation_monitor") | ||
mock_validate_submission.return_value = None | ||
async_mock = AsyncMock(return_value=return_sub) | ||
mock_add_submission = mocker.patch( | ||
|
@@ -240,6 +238,7 @@ def test_authed_upload_file( | |
|
||
res = client.post("/v1/filing/institutions/1234567890/filings/2024/submissions", files=files) | ||
mock_add_submission.assert_called_with(ANY, 1, "submission.csv") | ||
mock_validate_submission.assert_called_with("2024", "1234567890", return_sub, open(submission_csv, "rb").read()) | ||
assert mock_update_submission.call_args.args[0].state == SubmissionState.SUBMISSION_UPLOADED | ||
assert res.status_code == 200 | ||
assert res.json()["id"] == 1 | ||
|
@@ -280,6 +279,72 @@ def test_upload_file_invalid_size( | |
res = client.post("/v1/filing/institutions/1234567890/filings/2024/submissions", files=files) | ||
assert res.status_code == HTTPStatus.REQUEST_ENTITY_TOO_LARGE | ||
|
||
def test_submission_update_fail( | ||
self, | ||
mocker: MockerFixture, | ||
app_fixture: FastAPI, | ||
authed_user_mock: Mock, | ||
submission_csv: str, | ||
get_filing_mock: Mock, | ||
): | ||
return_sub = SubmissionDAO( | ||
id=1, | ||
filing=1, | ||
state=SubmissionState.SUBMISSION_UPLOADED, | ||
filename="submission.csv", | ||
) | ||
|
||
mock_logger = mocker.patch("sbl_filing_api.routers.filing.logger") | ||
|
||
mock_validate_file = mocker.patch("sbl_filing_api.services.submission_processor.validate_file_processable") | ||
mock_validate_file.return_value = None | ||
|
||
async_mock = AsyncMock(return_value=return_sub) | ||
mocker.patch("sbl_filing_api.entities.repos.submission_repo.add_submission", side_effect=async_mock) | ||
|
||
mock_upload = mocker.patch("sbl_filing_api.services.submission_processor.upload_to_storage") | ||
mock_upload.return_value = None | ||
mock_validate_submission = mocker.patch("sbl_filing_api.services.submission_processor.validation_monitor") | ||
mock_validate_submission.return_value = None | ||
|
||
mock_update_submission = mocker.patch( | ||
"sbl_filing_api.entities.repos.submission_repo.update_submission", side_effect=async_mock | ||
) | ||
|
||
mock_add_submitter = mocker.patch("sbl_filing_api.entities.repos.submission_repo.add_submitter") | ||
mock_add_submitter.side_effect = RuntimeError("Failed to add submitter.") | ||
|
||
file = {"file": ("submission.csv", open(submission_csv, "rb"))} | ||
client = TestClient(app_fixture) | ||
|
||
res = client.post("/v1/filing/institutions/1234567890/filings/2024/submissions", files=file) | ||
mock_logger.mock_calls[0].error.assert_called_with( | ||
"Error while trying to process Submission 1", RuntimeError, exec_info=True, stack_info=True | ||
) | ||
assert mock_update_submission.call_args.args[0].state == SubmissionState.UPLOAD_FAILED | ||
assert res.status_code == 500 | ||
assert res.json() == "Failed to add submitter." | ||
|
||
mock_add_submitter.side_effect = None | ||
mock_add_submitter.return_value = SubmitterDAO( | ||
id=1, | ||
submission=1, | ||
submitter="123456-7890-ABCDEF-GHIJ", | ||
submitter_name="test", | ||
submitter_email="[email protected]", | ||
) | ||
|
||
mock_upload.side_effect = HTTPException( | ||
status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail="Failed to upload file" | ||
) | ||
res = client.post("/v1/filing/institutions/1234567890/filings/2024/submissions", files=file) | ||
mock_logger.mock_calls[0].error.assert_called_with( | ||
"Error while trying to process Submission 1", RuntimeError, exec_info=True, stack_info=True | ||
) | ||
assert mock_update_submission.call_args.args[0].state == SubmissionState.UPLOAD_FAILED | ||
assert res.status_code == 500 | ||
assert res.json() == "500: Failed to upload file" | ||
|
||
async def test_unauthed_patch_filing(self, app_fixture: FastAPI): | ||
client = TestClient(app_fixture) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry, one more, 😂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and probably need to update the lock file as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curses! removed