#68 close responses

This commit is contained in:
Alok Saldanha
2022-06-20 21:29:19 -04:00
parent eaa157079c
commit 8d8a0a3483
+40 -34
View File
@@ -9,7 +9,6 @@
import datetime import datetime
import logging import logging
import re import re
import urllib.parse
from enum import Enum from enum import Enum
import psutil import psutil
@@ -22,6 +21,8 @@ from cellxgene_gateway.cellxgene_exception import CellxgeneException
from cellxgene_gateway.flask_util import querystring from cellxgene_gateway.flask_util import querystring
from cellxgene_gateway.util import current_time_stamp from cellxgene_gateway.util import current_time_stamp
logger = logging.getLogger(__name__)
class CacheEntryStatus(Enum): class CacheEntryStatus(Enum):
loaded = "loaded" loaded = "loaded"
@@ -111,7 +112,7 @@ class CacheEntry:
except psutil.NoSuchProcess: except psutil.NoSuchProcess:
pass pass
logging.getLogger("cellxgene_gateway").info(f"terminated {terminated}") logger.info(f"terminated {terminated}")
self.status = CacheEntryStatus.terminated self.status = CacheEntryStatus.terminated
def rewrite_text_content(self, cellxgene_content): def rewrite_text_content(self, cellxgene_content):
@@ -169,38 +170,43 @@ class CacheEntry:
full_path = self.cellxgene_basepath() + subpath + querystring() full_path = self.cellxgene_basepath() + subpath + querystring()
if request.method in ["GET", "HEAD", "OPTIONS"]: try:
cellxgene_response = get(full_path, headers=headers) cellxgene_response = None
elif request.method == "PUT": if request.method in ["GET", "HEAD", "OPTIONS"]:
cellxgene_response = put( cellxgene_response = get(full_path, headers=headers)
full_path, elif request.method == "PUT":
headers=headers, cellxgene_response = put(
data=request.data, full_path,
) headers=headers,
elif request.method == "POST": data=request.data,
cellxgene_response = post( )
full_path, elif request.method == "POST":
headers=headers, cellxgene_response = post(
data=request.data, full_path,
) headers=headers,
else: data=request.data,
raise CellxgeneException(f"Unexpected method {request.method}", 400) )
content_type = cellxgene_response.headers["content-type"] else:
if "text" in content_type: raise CellxgeneException(f"Unexpected method {request.method}", 400)
gateway_content = self.rewrite_text_content( content_type = cellxgene_response.headers["content-type"]
cellxgene_response.content.decode() if "text" in content_type:
) gateway_content = self.rewrite_text_content(
else: cellxgene_response.content.decode()
gateway_content = cellxgene_response.content )
else:
gateway_content = cellxgene_response.content
resp_headers = {} resp_headers = {}
for h in copy_headers: for h in copy_headers:
if h in cellxgene_response.headers: if h in cellxgene_response.headers:
resp_headers[h] = cellxgene_response.headers[h] resp_headers[h] = cellxgene_response.headers[h]
gateway_response = make_response( gateway_response = make_response(
gateway_content, gateway_content,
cellxgene_response.status_code, cellxgene_response.status_code,
resp_headers, resp_headers,
) )
finally:
if cellxgene_response is not None:
cellxgene_response.close()
return gateway_response return gateway_response