comparison env/lib/python3.7/site-packages/ephemeris/sleep.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
1 #!/usr/bin/env python
2 '''Utility to do a blocking sleep until a Galaxy instance is responsive.
3 This is useful in docker images, in RUN steps, where one needs to wait
4 for a currently starting Galaxy to be alive, before API requests can be
5 made successfully.
6
7 The script functions by making repeated requests to
8 ``http(s)://fqdn/api/version``, an API which requires no authentication
9 to access.'''
10
11 import sys
12 import time
13 from argparse import ArgumentParser
14
15 import requests
16 from galaxy.util import unicodify
17
18 from .common_parser import get_common_args
19
20
21 def _parser():
22 '''Constructs the parser object'''
23 parent = get_common_args(login_required=False)
24 parser = ArgumentParser(parents=[parent], usage="usage: %(prog)s <options>",
25 description="Script to sleep and wait for Galaxy to be alive.")
26 parser.add_argument("--timeout",
27 default=0, type=int,
28 help="Galaxy startup timeout in seconds. The default value of 0 waits forever")
29 return parser
30
31
32 def _parse_cli_options():
33 """
34 Parse command line options, returning `parse_args` from `ArgumentParser`.
35 """
36 parser = _parser()
37 return parser.parse_args()
38
39
40 def galaxy_wait(galaxy_url, timeout=600, verbose=False):
41 count = 0
42 while True:
43 try:
44 result = requests.get(galaxy_url + '/api/version')
45 try:
46 result = result.json()
47 if verbose:
48 sys.stdout.write("Galaxy Version: %s\n" % result['version_major'])
49 break
50 except ValueError:
51 if verbose:
52 sys.stdout.write("[%02d] No valid json returned... %s\n" % (count, result.__str__()))
53 sys.stdout.flush()
54 except requests.exceptions.ConnectionError as e:
55 if verbose:
56 sys.stdout.write("[%02d] Galaxy not up yet... %s\n" % (count, unicodify(e)[0:100]))
57 sys.stdout.flush()
58 count += 1
59
60 # If we cannot talk to galaxy and are over the timeout
61 if timeout != 0 and count > timeout:
62 sys.stderr.write("Failed to contact Galaxy\n")
63 sys.exit(1)
64
65 time.sleep(1)
66
67
68 def main():
69 """
70 Main function
71 """
72 options = _parse_cli_options()
73
74 galaxy_wait(options.galaxy, options.timeout, options.verbose)
75
76 sys.exit(0)
77
78
79 if __name__ == "__main__":
80 main()