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 2 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
42 changes: 32 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 = 300
TIMEOUTMULTIPLE = 1.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
minTimeout = MINTIMEOUT
maxTimeout = MAXTIMEOUT
williamnswanson marked this conversation as resolved.
Show resolved Hide resolved


options = Options()
Expand Down Expand Up @@ -87,8 +94,21 @@ 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.minTimeout
while trying:
try:
resp = urllib.request.urlopen(req, timeout=currentTimeout)
payload = resp.read()
trying = False
except urllib.error.URLError as exception:
if (currentTimeout < options.maxTimeout):
currentTimeout *= TIMEOUTMULTIPLE
else:
print("Exception raised after maximum timeout " + str(options.maxTimeout) + " seconds reached. Exception reason: " + str(exception.reason) + ".\n Request: " + str(req.full_url))
williamnswanson marked this conversation as resolved.
Show resolved Hide resolved
trying = False
quit()
williamnswanson marked this conversation as resolved.
Show resolved Hide resolved

return json.loads(payload) if payload else None


Expand Down Expand Up @@ -147,7 +167,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 +179,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.minTimeout = float(arg)
if op == '-T': options.maxTimeout = float(arg)
williamnswanson marked this conversation as resolved.
Show resolved Hide resolved

user, passwd = getpw(options.user, passfd, passfile)
options.authstr = mkauthstr(user, passwd)
Expand Down
Loading