-
Notifications
You must be signed in to change notification settings - Fork 0
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
[BD-32] feat: add async feature to Hooks Extension Framework tooling #8
Changes from 3 commits
003a8dd
349ff19
b8b4993
116d895
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
from django.test import TestCase | ||
|
||
from ..exceptions import HookException | ||
from ..pipeline import run_pipeline | ||
from ..tasks import run_pipeline | ||
|
||
|
||
class TestRunningPipeline(TestCase): | ||
|
@@ -24,7 +24,7 @@ def setUp(self): | |
} | ||
self.pipeline = Mock() | ||
|
||
@patch("edx_django_utils.hooks.pipeline.get_functions_for_pipeline") | ||
@patch("edx_django_utils.hooks.tasks.get_functions_for_pipeline") | ||
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. here just changing the pipeline location from pipeline.py to tasks.py |
||
def test_run_empty_pipeline(self, get_functions_mock): | ||
""" | ||
This method runs an empty pipeline, i.e, a pipeline without defined functions. | ||
|
@@ -39,7 +39,7 @@ def test_run_empty_pipeline(self, get_functions_mock): | |
get_functions_mock.assert_called_once_with([]) | ||
self.assertEqual(result, self.kwargs) | ||
|
||
@patch("edx_django_utils.hooks.pipeline.get_functions_for_pipeline") | ||
@patch("edx_django_utils.hooks.tasks.get_functions_for_pipeline") | ||
def test_raise_hook_exception(self, get_functions_mock): | ||
""" | ||
This method runs a pipeline with a function that raises HookException. | ||
|
@@ -61,7 +61,7 @@ def test_raise_hook_exception(self, get_functions_mock): | |
captured.records[0].getMessage(), log_message, | ||
) | ||
|
||
@patch("edx_django_utils.hooks.pipeline.get_functions_for_pipeline") | ||
@patch("edx_django_utils.hooks.tasks.get_functions_for_pipeline") | ||
def test_not_raise_hook_exception(self, get_functions_mock): | ||
""" | ||
This method runs a pipeline with a function that raises HookException but | ||
|
@@ -85,7 +85,7 @@ def test_not_raise_hook_exception(self, get_functions_mock): | |
self.assertEqual(result, return_value) | ||
function_without_exception.assert_called_once_with(**self.kwargs) | ||
|
||
@patch("edx_django_utils.hooks.pipeline.get_functions_for_pipeline") | ||
@patch("edx_django_utils.hooks.tasks.get_functions_for_pipeline") | ||
def test_not_raise_common_exception(self, get_functions_mock): | ||
""" | ||
This method runs a pipeline with a function that raises a common Exception. | ||
|
@@ -117,7 +117,7 @@ def test_not_raise_common_exception(self, get_functions_mock): | |
self.assertEqual(result, return_value) | ||
function_without_exception.assert_called_once_with(**self.kwargs) | ||
|
||
@patch("edx_django_utils.hooks.pipeline.get_functions_for_pipeline") | ||
@patch("edx_django_utils.hooks.tasks.get_functions_for_pipeline") | ||
def test_getting_pipeline_result(self, get_functions_mock): | ||
""" | ||
This method runs a pipeline with functions defined via configuration. | ||
|
@@ -145,7 +145,7 @@ def test_getting_pipeline_result(self, get_functions_mock): | |
second_function.assert_called_once_with(**return_value_1st) | ||
self.assertDictEqual(result, return_overall_value) | ||
|
||
@patch("edx_django_utils.hooks.pipeline.get_functions_for_pipeline") | ||
@patch("edx_django_utils.hooks.tasks.get_functions_for_pipeline") | ||
def test_partial_pipeline(self, get_functions_mock): | ||
""" | ||
This method runs a pipeline with functions defined via configuration. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
""" | ||
Triggers for actions and filters. | ||
""" | ||
from .pipeline import run_pipeline | ||
from logging import getLogger | ||
|
||
from kombu.exceptions import EncodeError | ||
|
||
from .tasks import run_pipeline | ||
from .utils import get_pipeline_configuration | ||
|
||
log = getLogger(__name__) | ||
|
||
|
||
def trigger_filter(trigger_name, *args, **kwargs): | ||
""" | ||
|
@@ -35,10 +41,17 @@ def trigger_filter(trigger_name, *args, **kwargs): | |
if not pipeline: | ||
return kwargs | ||
|
||
result = kwargs | ||
|
||
if is_async: | ||
result = run_pipeline( | ||
pipeline, *args, raise_exception=True, **kwargs | ||
) # TODO: change to async call. | ||
try: | ||
result = run_pipeline.delay(pipeline, *args, raise_exception=True, **kwargs) | ||
except (TypeError, EncodeError): | ||
log.exception( | ||
"An error ocurred in trigger_filter while executing `run pipeline` with arguments: %s, %s.", | ||
str(args), | ||
str(kwargs), | ||
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. maybe this won't be needed? 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. It makes no difference what the pipeline is at this point 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. Yes, it should always be serialization. I'll add it to the message! |
||
) | ||
else: | ||
result = run_pipeline(pipeline, *args, raise_exception=True, **kwargs) | ||
|
||
|
@@ -70,6 +83,13 @@ def trigger_action(trigger_name, *args, **kwargs): | |
return | ||
|
||
if is_async: | ||
run_pipeline(pipeline, *args, **kwargs) # TODO: change to async call. | ||
try: | ||
run_pipeline.delay(pipeline, *args, **kwargs) | ||
except (TypeError, EncodeError): | ||
log.exception( | ||
"An error ocurred in trigger_action while executing `run_pipeline` with arguments: %s, %s.", | ||
str(args), | ||
str(kwargs), | ||
) | ||
else: | ||
run_pipeline(pipeline, *args, **kwargs) |
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.
Something interesting I found in edx-celeryutils was a BaseTask called LoggedTask, wouldn't be useful to have that extra logging?