|
79 | 79 | import signal
|
80 | 80 | import sys
|
81 | 81 | from datetime import datetime
|
| 82 | +import traceback |
82 | 83 |
|
83 | 84 | from cadcutils import net, util, exceptions
|
84 | 85 | from caom2.obs_reader_writer import ObservationReader, ObservationWriter
|
@@ -709,54 +710,95 @@ def update(self, observation, **kwargs):
|
709 | 710 | '%(funcName)s %(message)s',
|
710 | 711 | level=level, stream=sys.stdout)
|
711 | 712 | logger = logging.getLogger('main_app')
|
712 |
| - client = CAOM2RepoClient(subject, level, args.resource_id, host=host) |
713 |
| - if args.cmd == 'visit': |
714 |
| - print("Visit") |
715 |
| - logger.debug( |
716 |
| - "Call visitor with plugin={}, start={}, end={}". |
717 |
| - format(args.plugin.name, args.start, args.end)) |
718 |
| - logger.debug( |
719 |
| - "collection={}, obs_file={}, threads={}". |
720 |
| - format(args.collection, args.obs_file, args.threads)) |
721 |
| - try: |
722 |
| - (visited, updated, skipped, failed) = \ |
723 |
| - client.visit(args.plugin.name, args.collection, |
724 |
| - start=args.start, |
725 |
| - end=args.end, obs_file=args.obs_file, |
726 |
| - nthreads=args.threads, |
727 |
| - halt_on_error=args.halt_on_error) |
728 |
| - finally: |
729 |
| - if args.obs_file is not None: |
730 |
| - args.obs_file.close() |
731 |
| - logger.info( |
732 |
| - 'Visitor stats: visited/updated/skipped/errors: {}/{}/{}/{}'. |
733 |
| - format(len(visited), len(updated), len(skipped), len(failed))) |
734 |
| - |
735 |
| - elif args.cmd == 'create': |
736 |
| - logger.info("Create") |
737 |
| - obs_reader = ObservationReader() |
738 |
| - client.put_observation(obs_reader.read(args.observation)) |
739 |
| - elif args.cmd == 'read': |
740 |
| - logger.info("Read") |
741 |
| - observation = client.get_observation(args.collection, |
742 |
| - args.observationID) |
743 |
| - observation_writer = ObservationWriter( |
744 |
| - False, False, 'caom2', client.namespace) |
745 |
| - if args.output: |
746 |
| - observation_writer.write(observation, args.output) |
| 713 | + errors = False |
| 714 | + try: |
| 715 | + client = CAOM2RepoClient(subject, level, args.resource_id, host=host) |
| 716 | + if args.cmd == 'visit': |
| 717 | + print("Visit") |
| 718 | + logger.debug( |
| 719 | + "Call visitor with plugin={}, start={}, end={}". |
| 720 | + format(args.plugin.name, args.start, args.end)) |
| 721 | + logger.debug( |
| 722 | + "collection={}, obs_file={}, threads={}". |
| 723 | + format(args.collection, args.obs_file, args.threads)) |
| 724 | + try: |
| 725 | + (visited, updated, skipped, failed) = \ |
| 726 | + client.visit(args.plugin.name, args.collection, |
| 727 | + start=args.start, |
| 728 | + end=args.end, obs_file=args.obs_file, |
| 729 | + nthreads=args.threads, |
| 730 | + halt_on_error=args.halt_on_error) |
| 731 | + finally: |
| 732 | + if args.obs_file is not None: |
| 733 | + args.obs_file.close() |
| 734 | + logger.info( |
| 735 | + 'Visitor stats: visited/updated/skipped/errors: {}/{}/{}/{}'. |
| 736 | + format(len(visited), len(updated), len(skipped), len(failed))) |
| 737 | + |
| 738 | + elif args.cmd == 'create': |
| 739 | + logger.info("Create") |
| 740 | + obs_reader = ObservationReader() |
| 741 | + client.put_observation(obs_reader.read(args.observation)) |
| 742 | + elif args.cmd == 'read': |
| 743 | + logger.info("Read") |
| 744 | + observation = client.get_observation(args.collection, |
| 745 | + args.observationID) |
| 746 | + observation_writer = ObservationWriter( |
| 747 | + False, False, 'caom2', client.namespace) |
| 748 | + if args.output: |
| 749 | + observation_writer.write(observation, args.output) |
| 750 | + else: |
| 751 | + observation_writer.write(observation, sys.stdout) |
| 752 | + elif args.cmd == 'update': |
| 753 | + logger.info("Update") |
| 754 | + obs_reader = ObservationReader() |
| 755 | + # TODO not sure if need to read in string first |
| 756 | + client.post_observation(obs_reader.read(args.observation)) |
747 | 757 | else:
|
748 |
| - observation_writer.write(observation, sys.stdout) |
749 |
| - elif args.cmd == 'update': |
750 |
| - logger.info("Update") |
751 |
| - obs_reader = ObservationReader() |
752 |
| - # TODO not sure if need to read in string first |
753 |
| - client.post_observation(obs_reader.read(args.observation)) |
| 758 | + logger.info("Delete") |
| 759 | + client.delete_observation(collection=args.collection, |
| 760 | + observation_id=args.observationID) |
| 761 | + except Exception as e: |
| 762 | + exit_after = True |
| 763 | + if args.cmd == 'visit' and args.threads: |
| 764 | + exit_after = False |
| 765 | + handle_error(exception=e, logging_level=level, exit_after=exit_after) |
| 766 | + errors = True |
| 767 | + |
| 768 | + if not errors: |
| 769 | + logger.info("DONE") |
| 770 | + else: |
| 771 | + print("Errors encountered") |
| 772 | + sys.exit(-1) |
| 773 | + |
| 774 | + |
| 775 | +def handle_error(exception, logging_level, exit_after=True): |
| 776 | + """ |
| 777 | + Prints error message and exit (by default) |
| 778 | + TODO - this needs to be reviewed and probably moved to cadcutils once |
| 779 | + the user/password mechanism is standardized |
| 780 | + :param msg: error message to print |
| 781 | + :param exit_after: True if log error message and exit, |
| 782 | + False if log error message and return |
| 783 | + :return: |
| 784 | + """ |
| 785 | + |
| 786 | + if isinstance(exception, exceptions.UnauthorizedException): |
| 787 | + print('ERROR: Unauthorized (invalid user/password?)') |
| 788 | + elif isinstance(exception, exceptions.NotFoundException): |
| 789 | + print('ERROR: Not found: {}'.format(str(exception))) |
| 790 | + elif isinstance(exception, exceptions.ForbiddenException): |
| 791 | + print('ERROR: Unauthorized to perform operation') |
| 792 | + elif isinstance(exception, exceptions.UnexpectedException): |
| 793 | + print('ERROR: Unexpected server error: {}'.format(str(exception))) |
754 | 794 | else:
|
755 |
| - logger.info("Delete") |
756 |
| - client.delete_observation(collection=args.collection, |
757 |
| - observation_id=args.observationID) |
| 795 | + print('ERROR: {}'.format(exception)) |
| 796 | + |
| 797 | + if logging_level <= logging.DEBUG: |
| 798 | + traceback.print_stack() |
758 | 799 |
|
759 |
| - logger.info("DONE") |
| 800 | + if exit_after: |
| 801 | + sys.exit(-1) # TODO use different error codes? |
760 | 802 |
|
761 | 803 |
|
762 | 804 | if __name__ == '__main__':
|
|
0 commit comments