-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: prevent tasks dying from temporary loss of db connection (#3674)
- Loading branch information
1 parent
0806dbc
commit b872a6c
Showing
4 changed files
with
91 additions
and
23 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,6 +1,35 @@ | ||
import logging | ||
import typing | ||
|
||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def run_by_processor(monkeypatch): | ||
monkeypatch.setenv("RUN_BY_PROCESSOR", "True") | ||
|
||
|
||
class GetTaskProcessorCaplog(typing.Protocol): | ||
def __call__( | ||
self, log_level: str | int = logging.INFO | ||
) -> pytest.LogCaptureFixture: ... | ||
|
||
|
||
@pytest.fixture | ||
def get_task_processor_caplog( | ||
caplog: pytest.LogCaptureFixture, | ||
) -> GetTaskProcessorCaplog: | ||
# caplog doesn't allow you to capture logging outputs from loggers that don't | ||
# propagate to root. Quick hack here to get the task_processor logger to | ||
# propagate. | ||
# TODO: look into using loguru. | ||
|
||
def _inner(log_level: str | int = logging.INFO) -> pytest.LogCaptureFixture: | ||
task_processor_logger = logging.getLogger("task_processor") | ||
task_processor_logger.propagate = True | ||
# Assume required level for the logger. | ||
task_processor_logger.setLevel(log_level) | ||
caplog.set_level(log_level) | ||
return caplog | ||
|
||
return _inner |
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
38 changes: 38 additions & 0 deletions
38
api/tests/unit/task_processor/test_unit_task_processor_threads.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,38 @@ | ||
import logging | ||
|
||
from django.db import DatabaseError | ||
from pytest_django.fixtures import SettingsWrapper | ||
from pytest_mock import MockerFixture | ||
|
||
from task_processor.threads import TaskRunner | ||
from tests.unit.task_processor.conftest import GetTaskProcessorCaplog | ||
|
||
|
||
def test_task_runner_is_resilient_to_database_errors( | ||
db: None, | ||
mocker: MockerFixture, | ||
get_task_processor_caplog: GetTaskProcessorCaplog, | ||
settings: SettingsWrapper, | ||
) -> None: | ||
# Given | ||
caplog = get_task_processor_caplog(logging.DEBUG) | ||
|
||
task_runner = TaskRunner() | ||
mocker.patch( | ||
"task_processor.threads.run_tasks", side_effect=DatabaseError("Database error") | ||
) | ||
|
||
# When | ||
task_runner.run_iteration() | ||
|
||
# Then | ||
assert len(caplog.records) == 2 | ||
|
||
assert caplog.records[0].levelno == logging.ERROR | ||
assert ( | ||
caplog.records[0].message | ||
== "Received database error retrieving tasks: Database error." | ||
) | ||
|
||
assert caplog.records[1].levelno == logging.DEBUG | ||
assert caplog.records[1].message.startswith("Traceback") |