Mercurial > repos > shellac > guppy_basecaller
comparison env/lib/python3.7/site-packages/planemo/galaxy/ephemeris_sleep.py @ 2:6af9afd405e9 draft
"planemo upload commit 0a63dd5f4d38a1f6944587f52a8cd79874177fc1"
| author | shellac |
|---|---|
| date | Thu, 14 May 2020 14:56:58 -0400 |
| parents | 26e78fe6e8c4 |
| children |
comparison
equal
deleted
inserted
replaced
| 1:75ca89e9b81c | 2:6af9afd405e9 |
|---|---|
| 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 The script functions by making repeated requests to | |
| 7 ``http(s)://fqdn/api/version``, an API which requires no authentication | |
| 8 to access.''' | |
| 9 | |
| 10 import sys | |
| 11 import time | |
| 12 from argparse import ArgumentParser | |
| 13 | |
| 14 import requests | |
| 15 from galaxy.util import unicodify | |
| 16 | |
| 17 try: | |
| 18 from .common_parser import get_common_args | |
| 19 except ImportError: | |
| 20 # This won't stay in Planemo long, main no longer functional. | |
| 21 get_common_args = None | |
| 22 | |
| 23 | |
| 24 def _parser(): | |
| 25 '''Constructs the parser object''' | |
| 26 parent = get_common_args(login_required=False) | |
| 27 parser = ArgumentParser(parents=[parent], usage="usage: python %(prog)s <options>", | |
| 28 description="Script to sleep and wait for Galaxy to be alive.") | |
| 29 parser.add_argument("--timeout", | |
| 30 default=0, type=int, | |
| 31 help="Galaxy startup timeout in seconds. The default value of 0 waits forever") | |
| 32 return parser | |
| 33 | |
| 34 | |
| 35 def _parse_cli_options(): | |
| 36 """ | |
| 37 Parse command line options, returning `parse_args` from `ArgumentParser`. | |
| 38 """ | |
| 39 parser = _parser() | |
| 40 return parser.parse_args() | |
| 41 | |
| 42 | |
| 43 def sleep(galaxy_url, verbose=False, timeout=0): | |
| 44 count = 0 | |
| 45 while True: | |
| 46 try: | |
| 47 result = requests.get(galaxy_url + '/api/version') | |
| 48 try: | |
| 49 result = result.json() | |
| 50 if verbose: | |
| 51 sys.stdout.write("Galaxy Version: %s\n" % result['version_major']) | |
| 52 break | |
| 53 except ValueError: | |
| 54 if verbose: | |
| 55 sys.stdout.write("[%02d] No valid json returned... %s\n" % (count, result.__str__())) | |
| 56 sys.stdout.flush() | |
| 57 except requests.exceptions.ConnectionError as e: | |
| 58 if verbose: | |
| 59 sys.stdout.write("[%02d] Galaxy not up yet... %s\n" % (count, unicodify(e)[:100])) | |
| 60 sys.stdout.flush() | |
| 61 count += 1 | |
| 62 | |
| 63 # If we cannot talk to galaxy and are over the timeout | |
| 64 if timeout != 0 and count > timeout: | |
| 65 sys.stderr.write("Failed to contact Galaxy\n") | |
| 66 return False | |
| 67 | |
| 68 time.sleep(1) | |
| 69 | |
| 70 return True | |
| 71 | |
| 72 | |
| 73 def main(): | |
| 74 """ | |
| 75 Main function | |
| 76 """ | |
| 77 options = _parse_cli_options() | |
| 78 | |
| 79 galaxy_alive = sleep(galaxy_url=options.galaxy, verbose=options.verbose, timeout=options.timeout) | |
| 80 exit_code = 0 if galaxy_alive else 1 | |
| 81 sys.exit(exit_code) | |
| 82 | |
| 83 | |
| 84 if __name__ == "__main__": | |
| 85 main() |
