Skip to content

Commit

Permalink
FIX: Allow questions to continue if event store can't be found
Browse files Browse the repository at this point in the history
  • Loading branch information
cortadocodes committed Jun 26, 2024
1 parent bc19236 commit 85ace7f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
11 changes: 10 additions & 1 deletion octue/cloud/deployment/google/cloud_run/flask_app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging

import google.api_core.exceptions
from flask import Flask, request

from octue.cloud.deployment.google.answer_pub_sub_question import answer_question
Expand Down Expand Up @@ -66,7 +67,7 @@ def _acknowledge_and_drop_redelivered_questions(question_uuid, retry_count):

if not service_configuration.event_store_table_id:
logger.warning(
"Cannot check if question has been redelivered as the 'event_store_table_id' key hasn't been set in the "
"Cannot check if question has been redelivered as the 'event_store_table_id' key hasn't been set in the "
"service configuration (`octue.yaml` file)."
)
return
Expand All @@ -78,6 +79,14 @@ def _acknowledge_and_drop_redelivered_questions(question_uuid, retry_count):
kind="question",
)

except google.api_core.exceptions.NotFound:
logger.warning(
"Cannot check if question has been redelivered as no event store table was found with the ID %r; check "
"that the 'event_store_table_id' key in the service configuration (`octue.yaml` file) is correct.",
service_configuration.event_store_table_id,
)
return

# If there are no events for this question UUID, assume this is the first attempt for the question.
except ValueError:
return
Expand Down
53 changes: 50 additions & 3 deletions tests/cloud/deployment/google/cloud_run/test_flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from unittest import TestCase
from unittest.mock import patch

from google.api_core.exceptions import NotFound

from octue.cloud.deployment.google.cloud_run import flask_app
from octue.configuration import ServiceConfiguration
from octue.utils.patches import MultiPatcher
Expand Down Expand Up @@ -44,9 +46,9 @@ def test_post_to_index_with_invalid_payload_results_in_400_error(self):


class TestQuestionRedelivery(TestCase):
def test_warning_logged_if_event_store_cannot_be_checked(self):
def test_warning_logged_if_no_event_store_provided(self):
"""Test that a warning is logged if the event store cannot be checked because one hasn't been specified in the
service configuration.
service configuration, and that the question is allowed to proceed to analysis.
"""
mock_configuration = copy.deepcopy(MOCK_CONFIGURATION)
mock_configuration.event_store_table_id = None
Expand All @@ -73,14 +75,59 @@ def test_warning_logged_if_event_store_cannot_be_checked(self):

self.assertTrue(
logging_context.output[0].endswith(
"Cannot check if question has been redelivered as the 'event_store_table_id' key hasn't been set in "
"Cannot check if question has been redelivered as the 'event_store_table_id' key hasn't been set in "
"the service configuration (`octue.yaml` file)."
)
)

self.assertEqual(response.status_code, 204)
mock_answer_question.assert_called_once()

def test_warning_logged_if_event_store_not_found(self):
"""Test that a warning is logged if the event store cannot be found and that the question is allowed to proceed
to analysis.
"""
mock_configuration = copy.deepcopy(MOCK_CONFIGURATION)
mock_configuration.event_store_table_id = "nonexistent.table"

multi_patcher = MultiPatcher(
patches=[
patch("octue.configuration.ServiceConfiguration.from_file", return_value=mock_configuration),
patch("octue.cloud.deployment.google.cloud_run.flask_app.get_events", side_effect=NotFound("blah")),
]
)

with flask_app.app.test_client() as client:
with patch("octue.cloud.deployment.google.cloud_run.flask_app.answer_question") as mock_answer_question:
with multi_patcher:
with self.assertLogs(level=logging.WARNING) as logging_context:
response = client.post(
"/",
json={
"deliveryAttempt": 1,
"subscription": "projects/my-project/subscriptions/my-subscription",
"message": {
"data": {},
"attributes": {
"question_uuid": str(uuid.uuid4()),
"forward_logs": "1",
"retry_count": 0,
},
},
},
)

self.assertTrue(
logging_context.output[0].endswith(
"Cannot check if question has been redelivered as no event store table was found with the ID "
"'nonexistent.table'; check that the 'event_store_table_id' key in the service configuration "
"(`octue.yaml` file) is correct."
)
)

self.assertEqual(response.status_code, 204)
mock_answer_question.assert_called_once()

def test_new_question(self):
"""Test that a new question is checked against the event store and allowed to proceed to analysis."""
multi_patcher = MultiPatcher(
Expand Down

0 comments on commit 85ace7f

Please sign in to comment.