Пример #1
0
def logs(counter: str, status: bool, stderr: bool, stdout: bool, stream: bool, all: bool) -> None:
    """
    Show or stream execution event log.
    """
    project = get_project(require=True)
    assert project
    execution = project.get_execution_from_counter(counter=counter)
    accepted_streams = {v for v in [
        'status' if status else None,
        'stderr' if stderr else None,
        'stdout' if stdout else None,
    ] if v}
    lm = LogManager(execution)
    limit = (0 if all else None)
    while True:
        events_response = lm.fetch_events(limit=limit)
        events = events_response['events']
        if not stream and events_response.get('truncated'):
            warn(
                'There are {total} events, but only the last {n} are shown. Use `--all` to fetch everything.'.format(
                    total=events_response['total'],
                    n=len(events),
                )
            )
        for event in events:
            if event['stream'] not in accepted_streams:
                continue
            message = '{short_time} {text}'.format(
                short_time=(event['time'].split('T')[1][:-4]),
                text=clean_log_line(event['message']),
            )
            style = stream_styles.get(event['stream'], {})
            click.echo(click.style(message, **style))  # type: ignore[arg-type]
        if stream:
            lm.update_execution()
            if lm.execution['status'] in complete_execution_statuses:
                click.echo(
                    'The execution has finished (status {status}); stopping stream.'.format(
                        status=execution['status'],
                    ),
                    err=True
                )
                break
            # Fetch less on subsequent queries
            limit = 100
            time.sleep(1)
        else:
            break
Пример #2
0
 def draw(self) -> None:
     execution = self.log_manager.execution
     events = self.events
     l = Layout()
     l.add(self.get_header_flex(execution))
     l.add(self.get_stat_flex(execution))
     l.add(Divider('='))
     available_height = l.height - len(l.rows) - 2
     if available_height > 0:
         for event in events[-available_height:]:
             l.add(
                 Flex(style=stream_styles.get(event['stream'])).add(
                     event['time'].split('T')[1][:-4] + '  ',
                     flex=0).add(clean_log_line(event['message']), flex=4))
     click.clear()
     l.draw()
Пример #3
0
 def draw(self):
     execution = self.data
     events = execution.get('events', ())
     l = Layout()
     l.add(self.get_header_flex(execution))
     l.add(self.get_stat_flex(execution))
     l.add(Divider('='))
     available_height = l.height - len(l.rows) - 2
     if available_height > 0:
         for event in events[-available_height:]:
             l.add(
                 Flex(style=stream_styles.get(event['stream']))
                 .add(event['time'].split('T')[1][:-4] + '  ', flex=0)
                 .add(event['message'], flex=4)
             )
     click.clear()
     l.draw()
Пример #4
0
def logs(counter, status, stderr, stdout, stream):
    """
    Show or stream execution event log.
    """
    execution = get_project(require=True).get_execution_from_counter(
        counter=counter)
    detail_url = execution['url']

    accepted_streams = set(v for v in [
        'status' if status else None,
        'stderr' if stderr else None,
        'stdout' if stdout else None,
    ] if v)
    seen_events = set()
    while True:
        execution = request('get', detail_url, params={
            'exclude': 'metadata'
        }).json()
        events = execution.get('events', ())
        for event in events:
            event_id = hashlib.md5(
                force_bytes('+'.join((event['stream'], event['time'],
                                      event['message'])))).hexdigest()
            if event_id in seen_events:
                continue
            seen_events.add(event_id)
            if event['stream'] not in accepted_streams:
                continue
            message = '{short_time} {text}'.format(
                short_time=(event['time'].split('T')[1][:-4]),
                text=(event['message']),
            )
            style = stream_styles.get(event['stream'], {})
            click.echo(click.style(message, **style))
        if stream:
            if execution['status'] in complete_execution_statuses:
                click.echo(
                    'The execution has finished (status {status}); stopping stream.'
                    .format(status=execution['status'], ),
                    err=True)
                break
            time.sleep(1)
        else:
            break