Skip to content
Snippets Groups Projects
Commit 1765a21c authored by Thomas Kluyver's avatar Thomas Kluyver
Browse files

Catch sqlite timeout & forward queries to CalCat

parent 3baadcd5
No related branches found
No related tags found
1 merge request!3Catch sqlite timeout & forward queries to CalCat
...@@ -7,6 +7,7 @@ constants. ...@@ -7,6 +7,7 @@ constants.
import asyncio import asyncio
import logging import logging
import os import os
import sqlite3
from dataclasses import dataclass from dataclasses import dataclass
from secrets import token_hex from secrets import token_hex
from urllib.parse import parse_qs, urlencode from urllib.parse import parse_qs, urlencode
...@@ -140,7 +141,20 @@ class ProxyHandler(RequestHandler): ...@@ -140,7 +141,20 @@ class ProxyHandler(RequestHandler):
) )
async def _get_with_cache(self, req_path) -> ProxyResponse: 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': if os.environ.get('CALPARROT_NEW_QUERY', '').lower() == 'warn':
log.warning( log.warning(
"CalParrot received a new query (for %s) when it was told to " "CalParrot received a new query (for %s) when it was told to "
...@@ -150,9 +164,11 @@ class ProxyHandler(RequestHandler): ...@@ -150,9 +164,11 @@ class ProxyHandler(RequestHandler):
"New CalCat queries made while reproducing correction" "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) 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: else:
# This URL is already in the database # This URL is already in the database
try: try:
...@@ -169,7 +185,8 @@ class ProxyHandler(RequestHandler): ...@@ -169,7 +185,8 @@ class ProxyHandler(RequestHandler):
"forwarding request for %s", req_path "forwarding request for %s", req_path
) )
resp = await self._get_upstream(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 return resp
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment