Skip to content

Commit f510f36

Browse files
committed
Error handling and cleanup
1 parent dfbd920 commit f510f36

File tree

1 file changed

+14
-24
lines changed

1 file changed

+14
-24
lines changed

src/server/api/admin_api.py

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
def __allowed_file(filename):
2525
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
2626

27-
28-
2927
kvt = Table("kv_unique", metadata, autoload=True, autoload_with=engine)
3028

3129

@@ -70,27 +68,31 @@ def execute():
7068
statistics = get_statistics()
7169

7270
last_execution_details = {"executionTime": current_time, "stats": statistics}
73-
7471
last_ex_json = (json.dumps(last_execution_details))
7572

73+
# Write Last Execution stats to DB
7674
with engine.connect() as connection:
7775
ins_stmt = insert(kvt).values(
7876
keycol = 'last_execution_time',
7977
valcol = last_ex_json,
8078
)
81-
79+
# If key already present in DB, do update instead
8280
upsert = ins_stmt.on_conflict_do_update(
8381
constraint='kv_unique_keycol_key',
8482
set_=dict(valcol=last_ex_json)
85-
)
86-
87-
connection.execute(upsert)
83+
)
8884

85+
try:
86+
connection.execute(upsert)
87+
except Exception as e:
88+
current_app.logger.error("Insert/Update failed on Last Execution stats")
89+
current_app.logger.exception(e)
8990

9091
return jsonify(success=True)
9192

9293

9394
def get_statistics():
95+
""" Write Last Execution stats to DB. """
9496
with engine.connect() as connection:
9597
query_matches = text("SELECT count(*) FROM (SELECT distinct matching_id from pdp_contacts) as a;")
9698
query_total_count = text("SELECT count(*) FROM pdp_contacts;")
@@ -108,32 +110,20 @@ def get_statistics():
108110

109111
@admin_api.route("/api/statistics", methods=["GET"])
110112
def list_statistics():
111-
try:
113+
""" Pull Last Execution stats from DB. """
112114

115+
last_execution_details = '{}' # Empty but valid JSON
113116

117+
try:
114118
with engine.connect() as connection:
115119
s = text("select valcol from kv_unique where keycol = 'last_execution_time';")
116120
result = connection.execute(s)
117121
last_execution_details = result.fetchone()[0]
118122

119123

120-
# last_execution_file = open(LOGS_PATH + "last_execution.json", "r")
121-
# last_execution_details = json.loads(last_execution_file.read())
122-
# last_execution_file.close()
123-
124-
except (FileNotFoundError):
125-
current_app.logger.error("last_execution.json file was missing")
126-
return abort(500)
127-
128-
except (json.JSONDecodeError):
129-
current_app.logger.error(
130-
"last_execution.json could not be decoded - possible corruption"
131-
)
132-
return abort(500)
133-
134124
except Exception as e:
135-
current_app.logger.error("Failure reading last_execution.json: ", e)
136-
return abort(500)
125+
current_app.logger.error("Failure reading Last Execution stats from DB")
126+
# return abort(500) # Weird but not worth a 500
137127

138128
return last_execution_details
139129

0 commit comments

Comments
 (0)