Line data Source code
1 : // Copyright (C) 2009 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.sshd.commands; 16 : 17 : import static com.google.gerrit.common.data.GlobalCapability.KILL_TASK; 18 : import static com.google.gerrit.common.data.GlobalCapability.MAINTAIN_SERVER; 19 : 20 : import com.google.gerrit.extensions.annotations.RequiresAnyCapability; 21 : import com.google.gerrit.extensions.restapi.AuthException; 22 : import com.google.gerrit.extensions.restapi.IdString; 23 : import com.google.gerrit.extensions.restapi.ResourceConflictException; 24 : import com.google.gerrit.extensions.restapi.ResourceNotFoundException; 25 : import com.google.gerrit.server.config.ConfigResource; 26 : import com.google.gerrit.server.config.TaskResource; 27 : import com.google.gerrit.server.permissions.PermissionBackendException; 28 : import com.google.gerrit.server.restapi.config.DeleteTask; 29 : import com.google.gerrit.server.restapi.config.TasksCollection; 30 : import com.google.gerrit.sshd.AdminHighPriorityCommand; 31 : import com.google.gerrit.sshd.SshCommand; 32 : import com.google.inject.Inject; 33 : import java.util.ArrayList; 34 : import java.util.List; 35 : import org.kohsuke.args4j.Argument; 36 : 37 : /** Kill a task in the work queue. */ 38 : @AdminHighPriorityCommand 39 : @RequiresAnyCapability({KILL_TASK, MAINTAIN_SERVER}) 40 1 : final class KillCommand extends SshCommand { 41 : @Inject private TasksCollection tasksCollection; 42 : 43 : @Inject private DeleteTask deleteTask; 44 : 45 1 : @Argument(index = 0, multiValued = true, required = true, metaVar = "ID") 46 : private List<String> taskIds = new ArrayList<>(); 47 : 48 : @Override 49 : protected void run() { 50 0 : enableGracefulStop(); 51 0 : ConfigResource cfgRsrc = new ConfigResource(); 52 0 : for (String id : taskIds) { 53 : try { 54 0 : TaskResource taskRsrc = tasksCollection.parse(cfgRsrc, IdString.fromDecoded(id)); 55 0 : deleteTask.apply(taskRsrc, null); 56 0 : } catch (AuthException 57 : | ResourceNotFoundException 58 : | ResourceConflictException 59 : | PermissionBackendException e) { 60 0 : stderr.print("kill: " + id + ": No such task\n"); 61 0 : } 62 0 : } 63 0 : } 64 : }