001 /**
002 * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
003 * Licensed under the Apache License, Version 2.0 (the "License");
004 * you may not use this file except in compliance with the License.
005 * You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software
010 * distributed under the License is distributed on an "AS IS" BASIS,
011 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 * See the License for the specific language governing permissions and
013 * limitations under the License. See accompanying LICENSE file.
014 */
015 package org.apache.oozie.command.wf;
016
017 import org.apache.oozie.client.WorkflowJob;
018 import org.apache.oozie.client.SLAEvent.SlaAppType;
019 import org.apache.oozie.client.SLAEvent.Status;
020 import org.apache.oozie.WorkflowActionBean;
021 import org.apache.oozie.WorkflowJobBean;
022 import org.apache.oozie.command.Command;
023 import org.apache.oozie.command.CommandException;
024 import org.apache.oozie.store.StoreException;
025 import org.apache.oozie.store.WorkflowStore;
026 import org.apache.oozie.store.Store;
027 import org.apache.oozie.workflow.WorkflowException;
028 import org.apache.oozie.workflow.WorkflowInstance;
029 import org.apache.oozie.workflow.lite.LiteWorkflowInstance;
030 import org.apache.oozie.util.ParamChecker;
031 import org.apache.oozie.util.XLog;
032 import org.apache.oozie.util.db.SLADbOperations;
033
034 import java.util.Date;
035
036 public class KillCommand extends WorkflowCommand<Void> {
037
038 private String id;
039 private final XLog log = XLog.getLog(getClass());
040
041 public KillCommand(String id) {
042 super("kill", "kill", 1, XLog.STD);
043 this.id = ParamChecker.notEmpty(id, "id");
044 }
045
046 @Override
047 protected Void call(WorkflowStore store) throws StoreException, CommandException {
048 try {
049 log.info("In Workflow KillCommand.call() for jobId=" + id);
050 WorkflowJobBean workflow = store.getWorkflow(id, false);
051 setLogInfo(workflow);
052
053 if (workflow.getStatus() == WorkflowJob.Status.PREP || workflow.getStatus() == WorkflowJob.Status.RUNNING
054 || workflow.getStatus() == WorkflowJob.Status.SUSPENDED
055 || workflow.getStatus() == WorkflowJob.Status.FAILED) {
056 workflow.setEndTime(new Date());
057
058 if (workflow.getStatus() != WorkflowJob.Status.FAILED) {
059 incrJobCounter(1);
060 workflow.setStatus(WorkflowJob.Status.KILLED);
061 SLADbOperations.writeStausEvent(workflow.getSlaXml(), workflow.getId(), store, Status.KILLED,
062 SlaAppType.WORKFLOW_JOB);
063 workflow.getWorkflowInstance().kill();
064 WorkflowInstance wfInstance = workflow.getWorkflowInstance();
065 ((LiteWorkflowInstance) wfInstance).setStatus(WorkflowInstance.Status.KILLED);
066 workflow.setWorkflowInstance(wfInstance);
067 }
068 for (WorkflowActionBean action : store.getActionsForWorkflow(id, true)) {
069 if (action.getStatus() == WorkflowActionBean.Status.RUNNING
070 || action.getStatus() == WorkflowActionBean.Status.DONE) {
071 action.setPending();
072 action.setStatus(WorkflowActionBean.Status.KILLED);
073 store.updateAction(action);
074 queueCallable(new ActionKillCommand(action.getId(), action.getType()));
075 }
076 if (action.getStatus() == WorkflowActionBean.Status.PREP
077 || action.getStatus() == WorkflowActionBean.Status.START_RETRY
078 || action.getStatus() == WorkflowActionBean.Status.START_MANUAL
079 || action.getStatus() == WorkflowActionBean.Status.END_RETRY
080 || action.getStatus() == WorkflowActionBean.Status.END_MANUAL) {
081 action.setStatus(WorkflowActionBean.Status.KILLED);
082 action.resetPending();
083 SLADbOperations.writeStausEvent(action.getSlaXml(), action.getId(), store, Status.KILLED,
084 SlaAppType.WORKFLOW_ACTION);
085 store.updateAction(action);
086 }
087 }
088 store.updateWorkflow(workflow);
089 queueCallable(new NotificationCommand(workflow));
090 }
091 return null;
092 }
093 catch (WorkflowException ex) {
094 throw new CommandException(ex);
095 }
096 }
097
098 @Override
099 protected Void execute(WorkflowStore store) throws CommandException, StoreException {
100 try {
101 XLog.getLog(getClass()).debug("STARTED KillCommand for job " + id);
102 if (lock(id)) {
103 call(store);
104 }
105 else {
106 queueCallable(new KillCommand(id), LOCK_FAILURE_REQUEUE_INTERVAL);
107 XLog.getLog(getClass()).warn("KillCommand lock was not acquired - failed {0}", id);
108 }
109 }
110 catch (InterruptedException e) {
111 queueCallable(new KillCommand(id), LOCK_FAILURE_REQUEUE_INTERVAL);
112 XLog.getLog(getClass()).warn("KillCommand lock was not acquired - interrupted exception failed {0}", id);
113 }
114 XLog.getLog(getClass()).debug("ENDED KillCommand for job " + id);
115 return null;
116 }
117 }