Skip to content

Commit 959b34e

Browse files
committed
move timeout defaults to programs
fixes #3867
1 parent dbfe22a commit 959b34e

File tree

5 files changed

+44
-32
lines changed

5 files changed

+44
-32
lines changed

tools/src/main/python/opengrok_tools/mirror.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def main():
110110
parser.add_argument('-w', '--workers', default=cpu_count(), type=int,
111111
help='Number of worker processes')
112112
add_http_headers(parser)
113-
parser.add_argument('--api_timeout', type=int,
113+
parser.add_argument('--api_timeout', type=int, default=3,
114114
help='Set response timeout in seconds '
115115
'for RESTful API calls')
116116

tools/src/main/python/opengrok_tools/projadm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,9 @@ def main():
266266
default=False, help='Do not delete source code when '
267267
'deleting a project')
268268
add_http_headers(parser)
269-
parser.add_argument('--api_timeout', type=int,
269+
parser.add_argument('--api_timeout', type=int, default=3,
270270
help='Set response timeout in seconds for RESTful API calls')
271-
parser.add_argument('--async_api_timeout', type=int,
271+
parser.add_argument('--async_api_timeout', type=int, default=300,
272272
help='Set timeout in seconds for asynchronous RESTful API calls')
273273

274274
group = parser.add_mutually_exclusive_group()

tools/src/main/python/opengrok_tools/reindex_project.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# CDDL HEADER END
1919

2020
#
21-
# Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.
21+
# Copyright (c) 2009, 2022, Oracle and/or its affiliates. All rights reserved.
2222
#
2323

2424

@@ -43,7 +43,7 @@
4343
"""
4444

4545

46-
def get_logprop_file(logger, template, pattern, project):
46+
def get_logprop_file(template, pattern, project):
4747
"""
4848
Return the filename of file with logging properties specific for given
4949
project.
@@ -93,7 +93,7 @@ def main():
9393
help='URI of the webapp with context path')
9494
parser.add_argument('--printoutput', action='store_true', default=False)
9595
add_http_headers(parser)
96-
parser.add_argument('--api_timeout', type=int,
96+
parser.add_argument('--api_timeout', type=int, default=3,
9797
help='Set response timeout in seconds for RESTful API calls')
9898

9999
cmd_args = sys.argv[1:]
@@ -105,18 +105,17 @@ def main():
105105
logger = get_console_logger(get_class_basename(), args.loglevel)
106106

107107
# Make sure the log directory exists.
108-
if args.directory:
109-
if not os.path.isdir(args.directory):
110-
os.makedirs(args.directory)
108+
if args.directory and not os.path.isdir(args.directory):
109+
os.makedirs(args.directory)
111110

112111
# Get files needed for per-project reindex.
113112
headers = get_headers(args.header)
114-
conf_file = get_config_file(logger, args.uri, headers=headers)
113+
conf_file = get_config_file(logger, args.uri, headers=headers, timeout=args.api_timeout)
115114
if conf_file is None:
116115
fatal("could not get config file to run the indexer")
117116
logprop_file = None
118117
if args.template and args.pattern:
119-
logprop_file = get_logprop_file(logger, args.template, args.pattern,
118+
logprop_file = get_logprop_file(args.template, args.pattern,
120119
args.project)
121120

122121
# Reindex with the modified logging.properties file and read-only config.

tools/src/main/python/opengrok_tools/utils/mirror.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def get_repos_for_project(project_name, uri, source_root,
8080
variables
8181
:param command_timeout: command timeout value in seconds
8282
:param headers: optional HTTP headers dictionary
83+
:param timeout: connect timeout for API calls
8384
:return: list of Repository objects
8485
"""
8586

tools/src/main/python/opengrok_tools/utils/restful.py

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,30 @@
3434
APPLICATION_JSON = 'application/json' # default
3535

3636

37-
def wait_for_async_api(response, api_timeout, headers=None, timeout=None):
37+
def call_finished(location_uri, headers, timeout):
38+
"""
39+
:param location_uri: URI to check the status of API call
40+
:param headers: HTTP headers
41+
:param timeout: connect timeout
42+
"""
43+
logger = logging.getLogger(__name__)
44+
45+
logger.debug(f"GET API call: {location_uri}, timeout {timeout} seconds and headers: {headers}")
46+
response = requests.get(location_uri, headers=headers, proxies=get_proxies(location_uri), timeout=timeout)
47+
if response is None:
48+
raise Exception("API call failed")
49+
50+
response.raise_for_status()
51+
if response.status_code == 202:
52+
return False
53+
else:
54+
return True
55+
56+
57+
def wait_for_async_api(response, api_timeout=None, headers=None, timeout=None):
3858
"""
3959
:param response: request
40-
:param api_timeout: asynchronous API timeout
60+
:param api_timeout: asynchronous API timeout (will wait forever or until error if None)
4161
:param headers: request headers
4262
:param timeout: connect timeout
4363
:return: request
@@ -49,16 +69,16 @@ def wait_for_async_api(response, api_timeout, headers=None, timeout=None):
4969
raise Exception(f"no Location header in {response}")
5070

5171
start_time = time.time()
52-
for _ in range(api_timeout):
53-
logger.debug(f"GET API call: {location_uri}, timeout {timeout} seconds and headers: {headers}")
54-
response = requests.get(location_uri, headers=headers, proxies=get_proxies(location_uri), timeout=timeout)
55-
if response is None:
56-
raise Exception("API call failed")
57-
58-
if response.status_code == 202:
72+
if api_timeout is None:
73+
while True:
74+
if call_finished(location_uri, headers, timeout):
75+
break
76+
time.sleep(1)
77+
else:
78+
for _ in range(api_timeout):
79+
if call_finished(location_uri, headers, timeout):
80+
break
5981
time.sleep(1)
60-
else:
61-
break
6282

6383
if response.status_code == 202:
6484
wait_time = time.time() - start_time
@@ -81,9 +101,7 @@ def do_api_call(verb, uri, params=None, headers=None, data=None, timeout=None, a
81101
:param data: data or None
82102
:param timeout: optional connect timeout in seconds.
83103
Applies also to asynchronous API status calls.
84-
If None, default (60 seconds) will be used.
85104
:param api_timeout: optional timeout for asynchronous API requests in seconds.
86-
If None, default (300 seconds) will be used.
87105
:return: the result of the handler call, can be None
88106
"""
89107
logger = logging.getLogger(__name__)
@@ -92,12 +110,6 @@ def do_api_call(verb, uri, params=None, headers=None, data=None, timeout=None, a
92110
if handler is None or not callable(handler):
93111
raise Exception('Unknown HTTP verb: {}'.format(verb))
94112

95-
if timeout is None:
96-
timeout = 60
97-
98-
if api_timeout is None:
99-
api_timeout = 300
100-
101113
logger.debug("{} API call: {} with data '{}', connect timeout {} seconds, API timeout {} seconds and headers: {}".
102114
format(verb, uri, data, timeout, api_timeout, headers))
103115
r = handler(
@@ -113,7 +125,7 @@ def do_api_call(verb, uri, params=None, headers=None, data=None, timeout=None, a
113125
raise Exception("API call failed")
114126

115127
if r.status_code == 202:
116-
r = wait_for_async_api(r, api_timeout, headers=headers, timeout=timeout)
128+
r = wait_for_async_api(r, api_timeout=api_timeout, headers=headers, timeout=timeout)
117129

118130
r.raise_for_status()
119131

@@ -142,7 +154,7 @@ def call_rest_api(command, substitutions=None, http_headers=None, timeout=None):
142154
data substitution
143155
:param http_headers: optional dictionary of HTTP headers to be appended
144156
:param timeout: optional timeout in seconds for API call response
145-
:return return value from given requests method
157+
:return value from given requests method
146158
"""
147159

148160
logger = logging.getLogger(__name__)

0 commit comments

Comments
 (0)