Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement usermap exponential backoff (INF-1310) #16

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 deletions osg-comanage-project-usermap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""\
Expand All @@ -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:
Expand All @@ -50,6 +55,8 @@ class Options:
outfile = None
authstr = None
filtergrp = None
min_timeout = MINTIMEOUT
max_timeout = MAXTIMEOUT


options = Options()
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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()

Expand All @@ -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)
Expand Down