I've implemented a web app script that performs various actions, including making changes to a Google Sheet, which typically takes slightly more than 60 seconds.
However, despite setting timeout=360
or timeout=None
, I sometimes encounter a TimeoutError
in less than 30 seconds:
webAppsUrl = "https://script.google.com/macros/s/xxxxxxx/exec"
web_app_response = requests.get(webAppsUrl, headers=headers, timeout=360)
if web_app_response.text == 'Done':
...
else:
print('Try Again!')
My WebApp script in Google Apps Script:
function doGet(e) {
const lock = LockService.getDocumentLock();
if (lock.tryLock(360000)) {
try {
All_Leagues_Funct();
lock.releaseLock();
return ContentService.createTextOutput('Done');
} catch (error) {
lock.releaseLock();
const errorObj = {
message: error.message,
stack: error.stack
};
const folder = DriveApp.getFoldersByName("Error GAS").next();
const file = folder.createFile(
new Date().toString() + '.txt',
JSON.stringify(errorObj, null, 2)
);
return ContentService.createTextOutput(error);
}
} else {
return ContentService.createTextOutput('LockService Limit!');
}
}
The issue is that even though I receive a TimeoutError
, the web app script continues to run and makes changes to the spreadsheet. However, since the request
has already timed out, I am unable to determine whether the web app completed its actions successfully and returned "Done"
, or if an error occurred.
To compound the problem, I am no longer aware of the remaining time for the web app to finish since I have already received a TimeoutError
. This uncertainty makes it difficult to determine whether to make another call to prevent overlapping executions.
What would be the best solution or workaround to address this situation?
Traceback (most recent call last):
File "C:\Users\Computador\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connection.py", line 174, in _new_conn
conn = connection.create_connection(
File "C:\Users\Computador\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\connection.py", line 95, in create_connection
raise err
File "C:\Users\Computador\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\connection.py", line 85, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Computador\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
httplib_response = self._make_request(
File "C:\Users\Computador\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py", line 386, in _make_request
self._validate_conn(conn)
File "C:\Users\Computador\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py", line 1040, in _validate_conn
conn.connect()
File "C:\Users\Computador\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connection.py", line 358, in connect
self.sock = conn = self._new_conn()
File "C:\Users\Computador\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connection.py", line 179, in _new_conn
raise ConnectTimeoutError(
urllib3.exceptions.ConnectTimeoutError: (<urllib3.connection.HTTPSConnection object at 0x000001484196FCD0>, 'Connection to script.googleusercontent.com timed out. (connect timeout=360)')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Computador\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\adapters.py", line 440, in send
resp = conn.urlopen(
File "C:\Users\Computador\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py", line 785, in urlopen
retries = retries.increment(
File "C:\Users\Computador\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\retry.py", line 592, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='script.googleusercontent.com', port=443): Max retries exceeded with url: /macros/echo?user_content_key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x000001484196FCD0>, 'Connection to script.googleusercontent.com timed out. (connect timeout=360)'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\Users\Computador\Desktop\Squads Python\squads_sw.py", line 539, in matches_infos
web_app_response = requests.get(url, headers=headers, timeout=360)
File "C:\Users\Computador\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\Computador\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\Computador\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 529, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\Computador\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 667, in send
history = [resp for resp in gen]
File "C:\Users\Computador\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 667, in <listcomp>
history = [resp for resp in gen]
File "C:\Users\Computador\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 237, in resolve_redirects
resp = self.send(
File "C:\Users\Computador\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 645, in send
r = adapter.send(request, **kwargs)
File "C:\Users\Computador\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\adapters.py", line 507, in send
raise ConnectTimeout(e, request=request)
requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='script.googleusercontent.com', port=443): Max retries exceeded with url: /macros/echo?user_content_key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x000001484196FCD0>, 'Connection to script.googleusercontent.com timed out. (connect timeout=360)'))