001/*
002 * (C) Copyright 2017 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bdelbosc
018 */
019package org.nuxeo.lib.stream.tools.command;
020
021import java.time.Duration;
022
023import org.apache.commons.cli.CommandLine;
024import org.apache.commons.cli.Option;
025import org.apache.commons.cli.Options;
026import org.nuxeo.lib.stream.computation.Record;
027import org.nuxeo.lib.stream.log.LogManager;
028import org.nuxeo.lib.stream.log.LogRecord;
029import org.nuxeo.lib.stream.log.LogTailer;
030import org.nuxeo.lib.stream.tools.renderer.Renderer;
031
032/**
033 * @since 9.3
034 */
035public class TailCommand extends Command {
036
037    protected static final String NAME = "tail";
038
039    @Override
040    public String name() {
041        return NAME;
042    }
043
044    @Override
045    public void updateOptions(Options options) {
046        options.addOption(Option.builder("n")
047                                .longOpt("lines")
048                                .desc("output the last NUM records")
049                                .hasArg()
050                                .argName("NUM")
051                                .build());
052        options.addOption("f", "follow", false, "output appended records");
053        options.addOption(Option.builder("l")
054                                .longOpt("log-name")
055                                .desc("Log name")
056                                .required()
057                                .hasArg()
058                                .argName("LOG_NAME")
059                                .build());
060        options.addOption(
061                Option.builder("g").longOpt("group").desc("Consumer group").hasArg().argName("GROUP").build());
062        options.addOption(
063                Option.builder().longOpt("render").desc("Output rendering").hasArg().argName("FORMAT").build());
064        options.addOption(Option.builder("t")
065                                .longOpt("timeout")
066                                .desc("Timeout on follow in second")
067                                .hasArg()
068                                .argName("TIMEOUT")
069                                .build());
070    }
071
072    @Override
073    public boolean run(LogManager manager, CommandLine cmd) throws InterruptedException {
074        int lines = Integer.parseInt(cmd.getOptionValue("lines", "10"));
075        String name = cmd.getOptionValue("log-name");
076        String render = cmd.getOptionValue("render", "default");
077        String group = cmd.getOptionValue("group", "tools");
078        int timeout = Integer.parseInt(cmd.getOptionValue("timeout", "120"));
079        tail(manager, name, group, lines, getRecordRenderer(render));
080        if (cmd.hasOption("follow")) {
081            follow(manager, name, group, getRecordRenderer(render), timeout);
082        }
083        return true;
084    }
085
086    @SuppressWarnings("unchecked")
087    protected void tail(LogManager manager, String name, String group, int lines, Renderer render)
088            throws InterruptedException {
089        LogRecord<Record>[] records = new LogRecord[lines];
090        render.header();
091        int count = 0;
092        try (LogTailer<Record> tailer = manager.createTailer(group, name)) {
093            LogRecord<Record> record;
094            do {
095                record = tailer.read(Duration.ofMillis(500));
096                if (record != null) {
097                    records[count++ % lines] = record;
098                }
099            } while (record != null);
100        }
101        for (int i = count; i < lines + count; i++) {
102            LogRecord<Record> record = records[i % lines];
103            if (record != null) {
104                render.accept(record);
105            }
106        }
107        render.footer();
108    }
109
110    protected void follow(LogManager manager, String name, String group, Renderer render, int timeout)
111            throws InterruptedException {
112        try (LogTailer<Record> tailer = manager.createTailer(group, name)) {
113            tailer.toEnd();
114            while (true) {
115                LogRecord<Record> record = tailer.read(Duration.ofSeconds(timeout));
116                if (record == null) {
117                    System.err.println("tail timeout");
118                    break;
119                }
120                render.accept(record);
121            }
122        }
123    }
124}