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.coord;
016
017 import org.apache.oozie.client.CoordinatorJob;
018 import org.apache.oozie.CoordinatorActionBean;
019 import org.apache.oozie.CoordinatorJobBean;
020 import org.apache.oozie.XException;
021 import org.apache.oozie.command.wf.KillCommand;
022 import org.apache.oozie.command.CommandException;
023 import org.apache.oozie.store.CoordinatorStore;
024 import org.apache.oozie.store.StoreException;
025 import org.apache.oozie.util.ParamChecker;
026 import org.apache.oozie.util.XLog;
027
028 import java.util.Date;
029 import java.util.List;
030
031 public class CoordKillCommand extends CoordinatorCommand<Void> {
032
033 private String jobId;
034 private final XLog log = XLog.getLog(getClass());
035
036 public CoordKillCommand(String id) {
037 super("coord_kill", "coord_kill", 1, XLog.STD);
038 this.jobId = ParamChecker.notEmpty(id, "id");
039 }
040
041 protected Void call(CoordinatorStore store) throws StoreException, CommandException {
042 try {
043 // CoordinatorJobBean coordJob = store.getCoordinatorJob(jobId,
044 // false);
045 CoordinatorJobBean coordJob = store.getEntityManager().find(CoordinatorJobBean.class, jobId);
046 setLogInfo(coordJob);
047 if (coordJob.getStatus() != CoordinatorJob.Status.SUCCEEDED
048 || coordJob.getStatus() != CoordinatorJob.Status.FAILED) {
049 coordJob.setEndTime(new Date());
050 incrJobCounter(1);
051 coordJob.setStatus(CoordinatorJob.Status.KILLED);
052 List<CoordinatorActionBean> actionList = store.getActionsForCoordinatorJob(jobId, false);
053 for (CoordinatorActionBean action : actionList) {
054 if (action.getStatus() != CoordinatorActionBean.Status.FAILED
055 && action.getStatus() != CoordinatorActionBean.Status.TIMEDOUT
056 && action.getStatus() != CoordinatorActionBean.Status.SUCCEEDED
057 && action.getStatus() != CoordinatorActionBean.Status.KILLED) {
058 // queue a KillCommand to delete the workflow job
059 if (action.getExternalId() != null) {
060 queueCallable(new KillCommand(action.getExternalId()));
061 }
062 action.setStatus(CoordinatorActionBean.Status.KILLED);
063 store.updateCoordinatorAction(action);
064 }
065 }
066 store.updateCoordinatorJob(coordJob);
067 // TODO queueCallable(new NotificationCommand(coordJob));
068 }
069 else {
070 log.info("CoordKillCommand not killed - job either " + "finished successfully or does not exist "
071 + jobId);
072 }
073 return null;
074 }
075 catch (XException ex) {
076 throw new CommandException(ex);
077 }
078 }
079
080 @Override
081 protected Void execute(CoordinatorStore store) throws StoreException, CommandException {
082 log.info("STARTED CoordKillCommand for jobId=" + jobId);
083 try {
084 if (lock(jobId)) {
085 call(store);
086 }
087 else {
088 queueCallable(new CoordKillCommand(jobId), LOCK_FAILURE_REQUEUE_INTERVAL);
089 log.warn("CoordKillCommand lock was not acquired - " + " failed " + jobId + ". Requeing the same.");
090 }
091 }
092 catch (InterruptedException e) {
093 queueCallable(new CoordKillCommand(jobId), LOCK_FAILURE_REQUEUE_INTERVAL);
094 log.warn("CoordKillCommand lock acquiring failed " + " with exception " + e.getMessage() + " for job id "
095 + jobId + ". Requeing the same.");
096 }
097 finally {
098 log.info("ENDED CoordKillCommand for jobId=" + jobId);
099 }
100 return null;
101 }
102
103 }