|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.nifi.toolkit.cli.impl.command.nifi.pg; |
| 18 | + |
| 19 | +import org.apache.commons.cli.MissingOptionException; |
| 20 | +import org.apache.nifi.toolkit.cli.api.Context; |
| 21 | +import org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClient; |
| 22 | +import org.apache.nifi.toolkit.cli.impl.client.nifi.NiFiClientException; |
| 23 | +import org.apache.nifi.toolkit.cli.impl.client.nifi.ProcessGroupClient; |
| 24 | +import org.apache.nifi.toolkit.cli.impl.command.CommandOption; |
| 25 | +import org.apache.nifi.toolkit.cli.impl.command.nifi.AbstractNiFiCommand; |
| 26 | +import org.apache.nifi.toolkit.cli.impl.result.VoidResult; |
| 27 | +import org.apache.nifi.web.api.entity.DropRequestEntity; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | +import java.util.Properties; |
| 31 | + |
| 32 | +/** |
| 33 | + * Command to stop the components of a process group. |
| 34 | + */ |
| 35 | +public class PGEmptyQueues extends AbstractNiFiCommand<VoidResult> { |
| 36 | + |
| 37 | + public static final int MAX_ITERATIONS = 20; |
| 38 | + public static final long DELAY_MS = 1000; |
| 39 | + |
| 40 | + public PGEmptyQueues() { |
| 41 | + super("pg-empty-queues", VoidResult.class); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public String getDescription() { |
| 46 | + return "Empty all queues, recursively, in the specified Process Group. It is recommended to first use pg-stop."; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected void doInitialize(final Context context) { |
| 51 | + addOption(CommandOption.PG_ID.createOption()); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public VoidResult doExecute(final NiFiClient client, final Properties properties) |
| 56 | + throws NiFiClientException, IOException, MissingOptionException { |
| 57 | + |
| 58 | + final String pgId = getRequiredArg(properties, CommandOption.PG_ID); |
| 59 | + |
| 60 | + final ProcessGroupClient pgClient = client.getProcessGroupClient(); |
| 61 | + DropRequestEntity requestEntity = pgClient.emptyQueues(pgId); |
| 62 | + final String requestId = requestEntity.getDropRequest().getId(); |
| 63 | + |
| 64 | + int iterations = 1; |
| 65 | + while (!requestEntity.getDropRequest().isFinished() && iterations < MAX_ITERATIONS) { |
| 66 | + if (shouldPrint(properties)) { |
| 67 | + println("Emptying queues, currently at " + requestEntity.getDropRequest().getPercentCompleted() + "% (" |
| 68 | + + iterations + " of " + MAX_ITERATIONS + ")..."); |
| 69 | + } |
| 70 | + sleep(DELAY_MS); |
| 71 | + iterations++; |
| 72 | + requestEntity = pgClient.getEmptyQueuesRequest(pgId, requestId); |
| 73 | + } |
| 74 | + |
| 75 | + if (shouldPrint(properties)) { |
| 76 | + if (requestEntity.getDropRequest().isFinished()) { |
| 77 | + println("Drop request completed. Deleted: " + requestEntity.getDropRequest().getDropped()); |
| 78 | + } else { |
| 79 | + println("Drop request didn't complete yet. Thus far, deleted: " + requestEntity.getDropRequest().getDropped()); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return VoidResult.getInstance(); |
| 84 | + } |
| 85 | + |
| 86 | + private boolean shouldPrint(final Properties properties) { |
| 87 | + return isInteractive() || isVerbose(properties); |
| 88 | + } |
| 89 | + |
| 90 | + private void sleep(long millis) { |
| 91 | + try { |
| 92 | + Thread.sleep(millis); |
| 93 | + } catch (InterruptedException e) { |
| 94 | + Thread.interrupted(); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments