Skip to content

Commit b7224e7

Browse files
author
Tom Reitz
committed
fix to ensure results-file is output even in fail-fast condition
1 parent 36cbac5 commit b7224e7

2 files changed

Lines changed: 23 additions & 11 deletions

File tree

lightbeam/lightbeam.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ def __init__(self, config_file, logger=None, selector="*", exclude="", keep_keys
133133
def replace_linebreaks(self, m):
134134
return re.sub(r"\s+", '', m.group(0))
135135

136+
def shutdown(self, method):
137+
# this is called before any CRITICAL errors;
138+
# any cleanup tasks should go here:
139+
self.write_structured_output(method)
140+
136141
def write_structured_output(self, command):
137142
### Create structured output results_file if necessary
138143
self.end_timestamp = datetime.now()

lightbeam/validate.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,24 +143,27 @@ def load_references_structure(self, swagger, definition):
143143
else:
144144
self.logger.critical(f"Swagger contains neither `definitions` nor `components.schemas` - check that the Swagger is valid.")
145145
references = {}
146+
prefixes_to_remove = ["#/definitions/", "#/components/schemas/"]
146147
for k in schema["properties"].keys():
147-
prefixes_to_remove = (("#/definitions/", ""), ("#/components/schemas/", ""))
148-
149148
if k.endswith("Reference"):
150149
original_endpoint = util.pluralize_endpoint(k.replace("Reference", ""))
151150

152151
# this deals with the fact that an educationOrganizationReference may be to a school, LEA, etc.:
153152
endpoints_to_check = self.EDFI_GENERICS_TO_RESOURCES_MAPPING.get(original_endpoint, [original_endpoint])
154153

155154
for endpoint in endpoints_to_check:
156-
ref_definition = schema["properties"][k]["$ref"].replace(*prefixes_to_remove)
155+
ref_definition = schema["properties"][k]["$ref"]
156+
for prefix_to_remove in prefixes_to_remove:
157+
ref_definition = ref_definition.replace(prefix_to_remove,"")
157158
# look up (in swagger) the required fields for any reference
158159
ref_properties = self.load_reference(swagger, ref_definition)
159160
references[endpoint] = ref_properties
160161
elif "items" in schema["properties"][k].keys():
161162
# this deals with a property which is a list of items which themselves contain References
162163
# (example: studentAssessment.studentObjectiveAssessments contain an objectiveAssessmentReference)
163-
nested_definition = schema["properties"][k]["items"]["$ref"].replace(*prefixes_to_remove)
164+
nested_definition = schema["properties"][k]["items"]["$ref"]
165+
for prefix_to_remove in prefixes_to_remove:
166+
nested_definition = nested_definition.replace(prefix_to_remove,"")
164167
nested_references = self.load_references_structure(swagger, nested_definition)
165168
references.update(nested_references)
166169
return references
@@ -189,6 +192,11 @@ async def validate_endpoint(self, endpoint):
189192
tasks = []
190193
total_counter = 0
191194
self.lightbeam.num_errors = 0
195+
self.lightbeam.metadata["resources"][endpoint].update({
196+
"records_processed": 0,
197+
"records_skipped": 0,
198+
"records_failed": 0
199+
})
192200
for file_name in data_files:
193201
self.logger.info(f"validating {file_name} against {definition} schema...")
194202
with open(file_name) as file:
@@ -205,20 +213,19 @@ async def validate_endpoint(self, endpoint):
205213
if total_counter%1000==0:
206214
self.logger.info(f"(processed {total_counter}...)")
207215

216+
# update metadata counts
217+
self.lightbeam.metadata["resources"][endpoint]["records_processed"] = total_counter
218+
self.lightbeam.metadata["resources"][endpoint]["records_skipped"] = self.lightbeam.num_skipped
219+
self.lightbeam.metadata["resources"][endpoint]["records_failed"] = self.lightbeam.num_errors
220+
208221
# implement "fail fast" feature:
209222
if self.lightbeam.num_errors >= self.fail_fast_threshold:
223+
self.lightbeam.shutdown("validate")
210224
self.logger.critical(f"... STOPPING; found {self.lightbeam.num_errors} >= validate.references.max_failures={self.fail_fast_threshold} VALIDATION ERRORS.")
211225
break
212226

213227
if len(tasks)>0: await self.lightbeam.do_tasks(tasks, total_counter, log_status_counts=False)
214228

215-
# update metadata counts for this endpoint
216-
self.lightbeam.metadata["resources"][endpoint].update({
217-
"records_processed": total_counter,
218-
"records_skipped": self.lightbeam.num_skipped,
219-
"records_failed": self.lightbeam.num_errors
220-
})
221-
222229
if self.lightbeam.num_errors==0: self.logger.info(f"... all lines validate ok!")
223230
else:
224231
num_others = self.lightbeam.num_errors - self.MAX_VALIDATION_ERRORS_TO_DISPLAY

0 commit comments

Comments
 (0)