Included connection reset handling in execute.py and extended it to handle both data and files in the inputs
This commit is contained in:
+38
-22
@@ -1,12 +1,17 @@
|
||||
import requests
|
||||
import threading
|
||||
|
||||
import os
|
||||
from .request import buildRequest
|
||||
|
||||
#session = requests.Session()
|
||||
_thread_local = threading.local()
|
||||
|
||||
def getSession():
|
||||
#def getSession():
|
||||
def getSession(reset=False):
|
||||
if reset and hasattr(_thread_local, "session"):
|
||||
_thread_local.session.close()
|
||||
del _thread_local.session
|
||||
|
||||
if not hasattr(_thread_local, "session"):
|
||||
session = requests.Session()
|
||||
session.name = threading.current_thread().name
|
||||
@@ -18,30 +23,41 @@ def getSession():
|
||||
def executeRequest(action, outputs, resolved):
|
||||
session = getSession()
|
||||
|
||||
print(
|
||||
f"Session {session.name} used by "
|
||||
f"{threading.current_thread().name} thread"
|
||||
)
|
||||
session_log = bool(os.environ.get("LOGS_SESSION", False))
|
||||
|
||||
request, outputs = buildRequest(action, outputs, resolved)
|
||||
|
||||
response = session.request(
|
||||
method=request["method"],
|
||||
url=request["path"],
|
||||
json=request.get("object"),
|
||||
headers=request.get("headers"),
|
||||
data=request.get("data")
|
||||
if session_log:
|
||||
print(
|
||||
f"Session {session.name} used by "
|
||||
f"{threading.current_thread().name} thread"
|
||||
)
|
||||
|
||||
# if definition hasattr(cookies):
|
||||
# sessioncookies = {}
|
||||
try:
|
||||
request, outputs = buildRequest(action, outputs, resolved)
|
||||
|
||||
# for cookie_name, cookie_key in cookies:
|
||||
response = session.request(
|
||||
method=request["method"],
|
||||
url=request["path"],
|
||||
json=request.get("object"),
|
||||
headers=request.get("headers"),
|
||||
data=request.get("data"),
|
||||
files=request.get("files")
|
||||
)
|
||||
|
||||
# session.cookies.set(
|
||||
# sessioncookies[cookie_name] = response[cookie_key],
|
||||
except requests.ConnectionError as error:
|
||||
print(f"Request failed due to {error} during {action.name}")
|
||||
|
||||
session = getSession(reset=True)
|
||||
|
||||
print(f"Retrying {action.name}")
|
||||
|
||||
response = session.request(
|
||||
method=request["method"],
|
||||
url=request["path"],
|
||||
json=request.get("object"),
|
||||
headers=request.get("headers"),
|
||||
data=request.get("data")
|
||||
)
|
||||
|
||||
return response, outputs
|
||||
|
||||
#print(request)
|
||||
#print(response.status_code)
|
||||
#print(response.text)
|
||||
return response, outputs
|
||||
|
||||
Reference in New Issue
Block a user