001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     * 
010     *      http://www.apache.org/licenses/LICENSE-2.0
011     * 
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.oozie.command.coord;
019    
020    import java.util.Date;
021    import java.util.List;
022    
023    import org.apache.oozie.CoordinatorActionBean;
024    import org.apache.oozie.CoordinatorJobBean;
025    import org.apache.oozie.XException;
026    import org.apache.oozie.client.CoordinatorJob;
027    import org.apache.oozie.command.CommandException;
028    import org.apache.oozie.command.wf.SuspendCommand;
029    import org.apache.oozie.store.CoordinatorStore;
030    import org.apache.oozie.store.StoreException;
031    import org.apache.oozie.util.ParamChecker;
032    import org.apache.oozie.util.XLog;
033    
034    public class CoordSuspendCommand extends CoordinatorCommand<Void> {
035    
036        private String jobId;
037        private final XLog log = XLog.getLog(getClass());
038    
039        public CoordSuspendCommand(String id) {
040            super("coord_suspend", "coord_suspend", 1, XLog.STD);
041            this.jobId = ParamChecker.notEmpty(id, "id");
042        }
043    
044        @Override
045        protected Void call(CoordinatorStore store) throws StoreException, CommandException {
046            try {
047                // CoordinatorJobBean coordJob = store.getCoordinatorJob(jobId,
048                // false);
049                CoordinatorJobBean coordJob = store.getEntityManager().find(CoordinatorJobBean.class, jobId);
050                setLogInfo(coordJob);
051                if (coordJob.getStatus() != CoordinatorJob.Status.SUCCEEDED
052                        && coordJob.getStatus() != CoordinatorJob.Status.FAILED) {
053                    incrJobCounter(1);
054                    coordJob.setStatus(CoordinatorJob.Status.SUSPENDED);
055                    coordJob.setSuspendedTime(new Date());
056                    List<CoordinatorActionBean> actionList = store.getActionsForCoordinatorJob(jobId, false);
057                    for (CoordinatorActionBean action : actionList) {
058                        if (action.getStatus() == CoordinatorActionBean.Status.RUNNING) {
059                            // queue a SuspendCommand
060                            if (action.getExternalId() != null) {
061                                queueCallable(new SuspendCommand(action.getExternalId()));
062                            }
063                        }
064                    }
065                    store.updateCoordinatorJob(coordJob);
066                }
067                // TODO queueCallable(new NotificationCommand(coordJob));
068                else {
069                    log.info("CoordSuspendCommand not suspended - " + "job finished or does not exist " + jobId);
070                }
071                return null;
072            }
073            catch (XException ex) {
074                throw new CommandException(ex);
075            }
076        }
077    
078        @Override
079        protected Void execute(CoordinatorStore store) throws StoreException, CommandException {
080            log.info("STARTED CoordSuspendCommand for jobId=" + jobId);
081            try {
082                if (lock(jobId)) {
083                    call(store);
084                }
085                else {
086                    queueCallable(new CoordSuspendCommand(jobId), LOCK_FAILURE_REQUEUE_INTERVAL);
087                    log.warn("CoordSuspendCommand lock was not acquired - " + " failed " + jobId + ". Requeing the same.");
088                }
089            }
090            catch (InterruptedException e) {
091                queueCallable(new CoordSuspendCommand(jobId), LOCK_FAILURE_REQUEUE_INTERVAL);
092                log.warn("CoordSuspendCommand lock acquiring failed " + " with exception " + e.getMessage()
093                        + " for job id " + jobId + ". Requeing the same.");
094            }
095            finally {
096                log.info("ENDED CoordSuspendCommand for jobId=" + jobId);
097            }
098            return null;
099        }
100    
101    }