From 1765a21c9787a6c9283e145c883f1ec07e24bb15 Mon Sep 17 00:00:00 2001
From: Thomas Kluyver <thomas@kluyver.me.uk>
Date: Thu, 16 Nov 2023 11:11:36 +0000
Subject: [PATCH] Catch sqlite timeout & forward queries to CalCat

---
 calparrot/proxy.py | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)

diff --git a/calparrot/proxy.py b/calparrot/proxy.py
index ede55c2..ea88f10 100644
--- a/calparrot/proxy.py
+++ b/calparrot/proxy.py
@@ -7,6 +7,7 @@ constants.
 import asyncio
 import logging
 import os
+import sqlite3
 from dataclasses import dataclass
 from secrets import token_hex
 from urllib.parse import parse_qs, urlencode
@@ -140,7 +141,20 @@ class ProxyHandler(RequestHandler):
             )
 
     async def _get_with_cache(self, req_path) -> ProxyResponse:
-        if self.response_store.insert_pending(req_path, self.request.body):
+        store_response = True
+        try:
+            new_query = self.response_store.insert_pending(req_path, self.request.body)
+        except sqlite3.OperationalError:
+            log.warning(
+                "Could not check for query (%s) in CalParrot database. "
+                "Will treat this as new & forward to CalCat.",
+                exc_info=True,
+            )
+            new_query = True
+            # Don't try to write the response if there are issues accessing the DB
+            store_response = False
+
+        if new_query:
             if os.environ.get('CALPARROT_NEW_QUERY', '').lower() == 'warn':
                 log.warning(
                     "CalParrot received a new query (for %s) when it was told to "
@@ -150,9 +164,11 @@ class ProxyHandler(RequestHandler):
                     "New CalCat queries made while reproducing correction"
                 )
 
-            # We put a pending response in the database: now query upstream
+            # Send query upstream
             resp = await self._get_upstream(req_path)
-            self._record_response(req_path, resp)
+            if store_response:
+                # We put a pending record in the database; add the response
+                self._record_response(req_path, resp)
         else:
             # This URL is already in the database
             try:
@@ -169,7 +185,8 @@ class ProxyHandler(RequestHandler):
                         "forwarding request for %s", req_path
                 )
                 resp = await self._get_upstream(req_path)
-                self._record_response(req_path, resp)
+                if store_response:
+                    self._record_response(req_path, resp)
 
         return resp
 
-- 
GitLab