diff --git a/osg-comanage-project-usermap.py b/osg-comanage-project-usermap.py index 22fa725..879a255 100755 --- a/osg-comanage-project-usermap.py +++ b/osg-comanage-project-usermap.py @@ -12,6 +12,9 @@ SCRIPT = os.path.basename(__file__) ENDPOINT = "https://registry-test.cilogon.org/registry/" OSG_CO_ID = 8 +MINTIMEOUT = 5 +MAXTIMEOUT = 625 +TIMEOUTMULTIPLE = 5 _usage = f"""\ @@ -26,6 +29,8 @@ (default = {ENDPOINT}) -o outfile specify output file (default: write to stdout) -g filter_group filter users by group name (eg, 'ap1-login') + -t minTimeout set minimum timeout, in seconds, for API call (default to {MINTIMEOUT}) + -T maxTimeout set maximum timeout, in seconds, for API call (default to {MAXTIMEOUT}) -h display this help text PASS for USER is taken from the first of: @@ -50,6 +55,8 @@ class Options: outfile = None authstr = None filtergrp = None + min_timeout = MINTIMEOUT + max_timeout = MAXTIMEOUT options = Options() @@ -87,8 +94,20 @@ def mkrequest(target, **kw): def call_api(target, **kw): req = mkrequest(target, **kw) - resp = urllib.request.urlopen(req) - payload = resp.read() + trying = True + currentTimeout = options.min_timeout + while trying: + try: + resp = urllib.request.urlopen(req, timeout=currentTimeout) + payload = resp.read() + trying = False + except urllib.error.URLError as exception: + if (currentTimeout < options.max_timeout): + currentTimeout *= TIMEOUTMULTIPLE + else: + sys.exit(f"Exception raised after maximum timeout {options.max_timeout} seconds reached. " + + f"Exception reason: {exception.reason}.\n Request: {req.full_url}") + return json.loads(payload) if payload else None @@ -147,7 +166,7 @@ def get_co_person_osguser(pid): def parse_options(args): try: - ops, args = getopt.getopt(args, 'u:c:d:f:g:e:o:h') + ops, args = getopt.getopt(args, 'u:c:d:f:g:e:o:t:T:h') except getopt.GetoptError: usage() @@ -159,13 +178,15 @@ def parse_options(args): for op, arg in ops: if op == '-h': usage() - if op == '-u': options.user = arg - if op == '-c': options.osg_co_id = int(arg) - if op == '-d': passfd = int(arg) - if op == '-f': passfile = arg - if op == '-e': options.endpoint = arg - if op == '-o': options.outfile = arg - if op == '-g': options.filtergrp = arg + if op == '-u': options.user = arg + if op == '-c': options.osg_co_id = int(arg) + if op == '-d': passfd = int(arg) + if op == '-f': passfile = arg + if op == '-e': options.endpoint = arg + if op == '-o': options.outfile = arg + if op == '-g': options.filtergrp = arg + if op == '-t': options.min_timeout = float(arg) + if op == '-T': options.max_timeout = float(arg) user, passwd = getpw(options.user, passfd, passfile) options.authstr = mkauthstr(user, passwd)