-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update with changes from core repo (#6)
* feat(task-processor): add Task Processor inputs as env vars (#3355) * fix: prevent tasks dying from temporary loss of db connection (#3674) * fix(task-processor): catch all exceptions (#3737) * infra: run influxdb feature evaluation in thread (#4125) * fix: Stale connections after task processor errors (#4179) * Formatting fix * chore(task-processor): add better logging for failed tasks (#4186)
- Loading branch information
1 parent
d3018fa
commit 5f43c67
Showing
8 changed files
with
137 additions
and
29 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
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 |
---|---|---|
@@ -1,6 +1,34 @@ | ||
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
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
42 changes: 42 additions & 0 deletions
42
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,42 @@ | ||
import logging | ||
from typing import Type | ||
|
||
import pytest | ||
from django.db import DatabaseError | ||
from pytest_mock import MockerFixture | ||
|
||
from task_processor.threads import TaskRunner | ||
from tests.unit.task_processor.conftest import GetTaskProcessorCaplog | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"exception_class, exception_message", | ||
[(DatabaseError, "Database error"), (Exception, "Generic error")], | ||
) | ||
def test_task_runner_is_resilient_to_errors( | ||
db: None, | ||
mocker: MockerFixture, | ||
get_task_processor_caplog: GetTaskProcessorCaplog, | ||
exception_class: Type[Exception], | ||
exception_message: str, | ||
) -> None: | ||
# Given | ||
caplog = get_task_processor_caplog(logging.DEBUG) | ||
|
||
task_runner = TaskRunner() | ||
mocker.patch( | ||
"task_processor.threads.run_tasks", | ||
side_effect=exception_class(exception_message), | ||
) | ||
|
||
# When | ||
task_runner.run_iteration() | ||
|
||
# Then | ||
assert len(caplog.records) == 1 | ||
|
||
assert caplog.records[0].levelno == logging.ERROR | ||
assert ( | ||
caplog.records[0].message | ||
== f"Received error retrieving tasks: {exception_message}." | ||
) |