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