Skip to content

Commit

Permalink
wandbox request timeout (#439)
Browse files Browse the repository at this point in the history
* wandbox request timeout

* update version
  • Loading branch information
srz-zumix committed May 21, 2020
1 parent b885107 commit e76abaa
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
24 changes: 2 additions & 22 deletions tools/wandbox/iuwandbox.py
Expand Up @@ -44,7 +44,7 @@ def parse_command_line():
'-v',
'--version',
action='version',
version=u'%(prog)s version 6.2'
version=u'%(prog)s version 6.3'
)
parser.add_argument(
'--list-compiler',
Expand Down Expand Up @@ -446,27 +446,7 @@ def expand_wandbox_options(w, compiler, options):


def wandbox_api_call(callback, retries, retry_wait):
try:
return callback()
except (HTTPError, ConnectionError) as e:

def is_retry(e):
if not e.response:
return True
return e.response.status_code in [504]

if is_retry(e) and retries > 0:
try:
print(e.message)
except:
pass
print('wait {0}sec...'.format(retry_wait))
sleep(retry_wait)
return wandbox_api_call(callback, retries - 1, retry_wait)
else:
raise
except:
raise
return Wandbox.Call(callback, retries, retry_wait)


def wandbox_get_compilerlist():
Expand Down
48 changes: 44 additions & 4 deletions tools/wandbox/wandbox.py
Expand Up @@ -10,6 +10,10 @@
import requests
import json

from time import sleep
from requests.exceptions import HTTPError as RHTTPError
from requests.exceptions import ConnectionError as RConnectionError
from requests.exceptions import ConnectTimeout as RConnectTimeout

#
#
Expand All @@ -18,6 +22,7 @@ class Wandbox:

#api_url = 'http://melpon.org/wandbox/api'
api_url = 'https://wandbox.org/api'
timeout_ = (3.0, 60.0 * 5)

def __init__(self):
self.reset()
Expand All @@ -34,7 +39,7 @@ def GetCompilerList():
"""
get compiler list
"""
response = requests.get(Wandbox.api_url + '/list.json')
response = requests.get(Wandbox.api_url + '/list.json', timeout=3.0)
response.raise_for_status()
return response.json()

Expand All @@ -50,10 +55,18 @@ def GetPermlink(link):
"""
get wandbox permanet link
"""
response = requests.get(Wandbox.api_url + '/permlink/' + link)
response = requests.get(Wandbox.api_url + '/permlink/' + link, timeout=3.0)
response.raise_for_status()
return response.json()

@property
def timeout(self):
return self.timeout_

@timeout.setter
def timeout(self, v):
self.timeout_ = v

def get_permlink(self, link):
"""
get wandbox permanet link
Expand All @@ -67,7 +80,7 @@ def run(self):
"""
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
payload = json.dumps(self.parameter)
response = requests.post(self.api_url + '/compile.json', data=payload, headers=headers)
response = requests.post(self.api_url + '/compile.json', data=payload, headers=headers, timeout=self.timeout_)
response.raise_for_status()
try:
return response.json()
Expand Down Expand Up @@ -149,11 +162,38 @@ def reset(self):
"""
self.parameter = {'code': ''}

@staticmethod
def Call(action, retries, retry_wait):
try:
return action()
except (RHTTPError, RConnectionError, RConnectTimeout) as e:

def is_retry(e):
if e is None:
return False
if e.response is None:
return False
return e.response.status_code in [500, 502, 503, 504]

retries -= 1
if is_retry(e) and retries > 0:
try:
print(e.message)
except:
pass
print('wait {0}sec...'.format(retry_wait))
sleep(retry_wait)
return Wandbox.Call(action, retries, retry_wait)
else:
raise
except:
raise


if __name__ == '__main__':
with Wandbox() as w:
w.compiler('gcc-head')
w.options('warning,gnu++1y')
w.compiler_options('-Dx=hogefuga\n-O3')
w.code('#include <iostream>\nint main() { int x = 0; ::std::cout << "hoge" << ::std::endl; }')
w.code('#include <iostream>\nint main() { int x = 0; std::cout << "hoge" << std::endl; }')
print(w.run())

0 comments on commit e76abaa

Please sign in to comment.