Skip to content

Commit 3a0a9d0

Browse files
committed
fix retry logic on process validation report
1 parent cdaec70 commit 3a0a9d0

2 files changed

Lines changed: 51 additions & 18 deletions

File tree

functions-python/process_validation_report/src/main.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ def generate_report_entities(
155155
entities.append(validation_report_entity)
156156

157157
dataset = get_dataset(dataset_stable_id, session)
158+
if not dataset:
159+
raise Exception(f"Dataset {dataset_stable_id} not found.")
158160
dataset.validation_reports.append(validation_report_entity)
159161

160162
extracted_timezone = extract_timezone_from_json_validation_report(json_report)
@@ -251,32 +253,35 @@ def create_validation_report_entities(
251253
return str(error), 500
252254

253255
try:
254-
logging.info("Database session started.")
255256
# Generate the database entities required for the report
256-
try:
257-
entities = generate_report_entities(
258-
version,
259-
validated_at,
260-
json_report,
261-
dataset_stable_id,
262-
db_session,
263-
feed_stable_id,
264-
)
265-
except Exception as error:
266-
return str(error), 200 # Report already exists
257+
# If an error is thrown we should let the retry mechanism to do its work
258+
entities = generate_report_entities(
259+
version,
260+
validated_at,
261+
json_report,
262+
dataset_stable_id,
263+
db_session,
264+
feed_stable_id,
265+
)
267266

268-
# Commit the entities to the database
269267
for entity in entities:
270268
db_session.add(entity)
271-
logging.info(f"Committing {len(entities)} entities to the database.")
272-
db_session.commit()
273-
logging.info("Entities committed successfully.")
274-
269+
# In this case the report entities are already in the DB or cannot be saved for other reasons
270+
# In any case, this will fail in any retried event
271+
try:
272+
logging.info("Committing %s entities to the database.", len(entities))
273+
db_session.commit()
274+
logging.info("Entities committed successfully.")
275+
except Exception as error:
276+
logging.warning(
277+
"Could not commit %s entities to the database: %s", entities, error
278+
)
279+
return str(error), 200
275280
update_feed_statuses_query(db_session, [feed_stable_id])
276281

277282
return f"Created {len(entities)} entities.", 200
278283
except Exception as error:
279-
logging.error(f"Error creating validation report entities: {error}")
284+
logging.error("Error creating validation report entities: : %s", error)
280285
return f"Error creating validation report entities: {error}", 500
281286
finally:
282287
pass

functions-python/process_validation_report/tests/test_validation_report.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,34 @@ def test_create_validation_report_entities_json_error2(self, mock_get):
176176
)
177177
self.assertEqual(status, 500)
178178

179+
@mock.patch("requests.get")
180+
def test_create_validation_report_entities_missing_dataset(self, mock_get):
181+
"""Test create_validation_report_entities function."""
182+
mock_get.return_value = MagicMock(
183+
status_code=200,
184+
json=lambda: {
185+
"summary": {
186+
"validatedAt": "2021-01-01T00:00:00Z",
187+
"validatorVersion": "1.0",
188+
"gtfsFeatures": ["stops", "routes"],
189+
},
190+
"notices": [
191+
{"code": "notice_code", "severity": "ERROR", "totalNotices": 1}
192+
],
193+
},
194+
)
195+
feed_stable_id = faker.word()
196+
dataset_stable_id = "MISSING_ID"
197+
198+
message, status = create_validation_report_entities(
199+
feed_stable_id, dataset_stable_id, "1.0"
200+
)
201+
self.assertEqual(500, status)
202+
self.assertEqual(
203+
"Error creating validation report entities: Dataset MISSING_ID not found.",
204+
message,
205+
)
206+
179207
@patch("main.Logger")
180208
@patch("main.create_validation_report_entities")
181209
def test_process_validation_report(self, create_validation_report_entities_mock, _):

0 commit comments

Comments
 (0)