Example #1
0
def _trace_loop(ctx, app, snapshot):
    """Instance trace loop."""
    trace_printer = printer.AppTracePrinter()

    def on_message(result):
        """Callback to process trace message."""
        event = events.AppTraceEvent.from_dict(result['event'])
        if event is None:
            return False

        trace_printer.process(event)
        if isinstance(event, events.DeletedTraceEvent):
            return False

        return True

    def on_error(result):
        """Callback to process errors."""
        click.echo('Error: %s' % result['_error'], err=True)

    try:
        return ws_client.ws_loop(ctx['wsapi'], {
            'topic': '/trace',
            'filter': app
        }, snapshot, on_message, on_error)
    except ws_client.ConnectionError:
        click.echo('Could not connect to any Websocket APIs', err=True)
        sys.exit(-1)
Example #2
0
def _trace_loop(ctx, app, snapshot):
    """Instance trace loop."""

    trace_printer = printer.AppTracePrinter()

    rc = {'rc': _RC_DEFAULT_EXIT}

    if not snapshot:
        click.echo('# No trace information yet, waiting...\r',
                   nl=False,
                   err=True)

    def on_message(result):
        """Callback to process trace message."""
        _LOGGER.debug('result: %r', result)
        event = events.AppTraceEvent.from_dict(result['event'])
        if event is None:
            return False

        trace_printer.process(event)

        if isinstance(event, events.FinishedTraceEvent):
            rc['rc'] = event.rc

        if isinstance(event, events.KilledTraceEvent):
            rc['rc'] = _RC_KILLED

        if isinstance(event, events.AbortedTraceEvent):
            rc['rc'] = _RC_ABORTED

        if isinstance(event, events.DeletedTraceEvent):
            return False

        return True

    def on_error(result):
        """Callback to process errors."""
        click.echo('Error: %s' % result['_error'], err=True)

    try:
        ws_client.ws_loop(ctx['wsapi'], {
            'topic': '/trace',
            'filter': app
        }, snapshot, on_message, on_error)

        sys.exit(rc['rc'])

    except ws_client.WSConnectionError:
        click.echo('Could not connect to any Websocket APIs', err=True)
        sys.exit(-1)
Example #3
0
    def trace(last, snapshot, app):
        """Trace application events.

        Invoking treadmill_trace with non existing application instance will
        cause the utility to wait for the specified instance to be started.

        Specifying already finished instance of the application will display
        historical trace information and exit status.
        """
        if '#' not in app:
            # Instance is not specified, list matching and exit.
            tasks = zk.list_history(context.GLOBAL.zk.conn, app)
            if not tasks:
                print('# Trace information does not exist.', file=sys.stderr)
                return

            elif not last:
                for task in sorted(tasks):
                    print(task)
                return

            else:
                task = sorted(tasks)[-1]

        else:
            task = app

        trace = zk.AppTrace(
            context.GLOBAL.zk.conn,
            task,
            callback=printer.AppTracePrinter()
        )

        trace.run(snapshot=snapshot)
        try:
            while not trace.wait(timeout=1):
                pass

        except KeyboardInterrupt:
            pass
Example #4
0
    def trace(last, snapshot, app):
        """Trace application events.

        Invoking treadmill_trace with non existing application instance will
        cause the utility to wait for the specified instance to be started.

        Specifying already finished instance of the application will display
        historical trace information and exit status.
        """
        if '#' not in app:
            # Instance is not specified, list matching and exit.
            traces = zk.list_traces(context.GLOBAL.zk.conn, app)
            if not traces:
                click.echo('# Trace information does not exist.', err=True)
                return

            elif not last:
                for instance_id in traces:
                    cli.out(instance_id)
                return

            else:
                instance_id = traces[-1]

        else:
            instance_id = app

        trace = zk.AppTrace(context.GLOBAL.zk.conn,
                            instance_id,
                            callback=printer.AppTracePrinter())

        trace.run(snapshot=snapshot)
        try:
            while not trace.wait(timeout=1):
                pass

        except KeyboardInterrupt:
            pass
Example #5
0
 def setUp(self):
     self.trace_printer = printer.AppTracePrinter()
Example #6
0
    def trace(api, wsapi, last, snapshot, app):
        """Trace application events.

        Invoking treadmill_trace with non existing application instance will
        cause the utility to wait for the specified instance to be started.

        Specifying already finished instance of the application will display
        historical trace information and exit status.

        Specifying only an application name will list all the instance IDs with
        trace information available.
        """
        # Disable too many branches.
        #
        # pylint: disable=R0912

        ctx['api'] = api
        ctx['wsapi'] = wsapi

        if '#' not in app:
            apis = context.GLOBAL.state_api(ctx['api'])
            url = '/trace/{app}'.format(app=urllib.quote(app))

            try:
                response = restclient.get(apis, url)
                trace_info = response.json()

            except restclient.NotFoundError:
                trace_info = {'name': app, 'instances': []}

            if not trace_info['instances']:
                print >> sys.stderr, '# Trace information does not exist.'
                return

            elif not last:
                for instanceid in sorted(trace_info['instances']):
                    cli.out('{app}#{instanceid}'.format(app=trace_info['name'],
                                                        instanceid=instanceid))
                return

            else:
                app = '{app}#{instanceid}'.format(
                    app=trace_info['name'],
                    instanceid=sorted(trace_info['instances'])[-1])

        apis = context.GLOBAL.ws_api(ctx['wsapi'])
        ws = None
        for api in apis:
            try:
                ws = ws_client.create_connection(api)
                _LOGGER.debug('Using API %s', api)
                break
            except socket.error:
                _LOGGER.debug(
                    'Could not connect to %s, trying next SRV '
                    'record', api)
                continue

        if not ws:
            click.echo('Could not connect to any Websocket APIs', err=True)
            sys.exit(-1)

        ws.send(
            json.dumps({
                'topic': '/trace',
                'filter': app,
                'snapshot': snapshot
            }))

        trace_printer = printer.AppTracePrinter()
        while True:
            reply = ws.recv()
            if not reply:
                break
            result = json.loads(reply)
            if '_error' in result:
                click.echo('Error: %s' % result['_error'], err=True)
                break

            event = events.AppTraceEvent.from_dict(result['event'])
            if event is None:
                continue

            trace_printer.process(event)

        ws.close()