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 org.apache.commons.cli.CommandLine;
022import org.apache.commons.cli.Option;
023import org.apache.commons.cli.Options;
024import org.nuxeo.lib.stream.computation.Record;
025import org.nuxeo.lib.stream.log.LogLag;
026import org.nuxeo.lib.stream.log.LogManager;
027import org.nuxeo.lib.stream.log.LogTailer;
028
029/**
030 * @since 9.3
031 */
032public class ResetCommand extends Command {
033
034    protected static final String NAME = "reset";
035
036    @Override
037    public String name() {
038        return NAME;
039    }
040
041    @Override
042    public void updateOptions(Options options) {
043        options.addOption(Option.builder("l")
044                                .longOpt("log-name")
045                                .desc("Log name")
046                                .required()
047                                .hasArg()
048                                .argName("LOG_NAME")
049                                .build());
050        options.addOption(Option.builder("g")
051                                .longOpt("group")
052                                .desc("Consumer group")
053                                .required()
054                                .hasArg()
055                                .argName("GROUP")
056                                .build());
057    }
058
059    @Override
060    public boolean run(LogManager manager, CommandLine cmd) throws InterruptedException {
061        String name = cmd.getOptionValue("log-name");
062        String group = cmd.getOptionValue("group");
063        reset(manager, group, name);
064        return true;
065    }
066
067    protected void reset(LogManager manager, String group, String name) {
068        LogLag lag = manager.getLag(name, group);
069        long pos = lag.lower();
070        try (LogTailer<Record> tailer = manager.createTailer(group, name)) {
071            tailer.reset();
072        }
073        System.out.println(String.format("# Reset log %s, group: %s, from: %s to 0", name, group, pos));
074    }
075}