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;
019    
020    import org.apache.oozie.ErrorCode;
021    import org.apache.oozie.client.Job;
022    
023    /**
024     * Transition command for rerun the job. The derived class has to override these following functions:
025     * <p/>
026     * updateJob() : update job status and attributes
027     * rerunChildren() : submit or queue commands to rerun children
028     * notifyParent() : update the status to upstream if any
029     *
030     * @param <T>
031     */
032    public abstract class RerunTransitionXCommand<T> extends TransitionXCommand<T> {
033        protected String jobId;
034        protected T ret;
035        protected Job.Status prevStatus;
036    
037        /**
038         * The constructor for abstract class {@link RerunTransitionXCommand}
039         *
040         * @param name the command name
041         * @param type the command type
042         * @param priority the command priority
043         */
044        public RerunTransitionXCommand(String name, String type, int priority) {
045            super(name, type, priority);
046        }
047    
048        /**
049         * The constructor for abstract class {@link RerunTransitionXCommand}
050         *
051         * @param name the command name
052         * @param type the command type
053         * @param priority the command priority
054         * @param dryrun true if dryrun is enable
055         */
056        public RerunTransitionXCommand(String name, String type, int priority, boolean dryrun) {
057            super(name, type, priority, dryrun);
058        }
059    
060        /* (non-Javadoc)
061         * @see org.apache.oozie.command.TransitionXCommand#transitToNext()
062         */
063        @Override
064        public void transitToNext() {
065            if (job == null) {
066                job = this.getJob();
067            }
068            prevStatus = job.getStatus();
069            job.setStatus(Job.Status.RUNNING);
070            job.setPending();
071        }
072    
073        /**
074         * Rerun actions associated with the job
075         *
076         * @throws CommandException thrown if failed to rerun actions
077         */
078        public abstract void rerunChildren() throws CommandException;
079    
080        /* (non-Javadoc)
081         * @see org.apache.oozie.command.TransitionXCommand#execute()
082         */
083        @Override
084        protected T execute() throws CommandException {
085            getLog().info("STARTED " + getClass().getSimpleName() + " for jobId=" + jobId);
086            try {
087                transitToNext();
088                rerunChildren();
089                updateJob();
090            }
091            finally {
092                notifyParent();
093            }
094            getLog().info("ENDED " + getClass().getSimpleName() + " for jobId=" + jobId);
095            return ret;
096        }
097    
098        /* (non-Javadoc)
099         * @see org.apache.oozie.command.XCommand#verifyPrecondition()
100         */
101        @Override
102        protected void verifyPrecondition() throws CommandException, PreconditionException {
103            eagerVerifyPrecondition();
104        }
105    
106        /* (non-Javadoc)
107         * @see org.apache.oozie.command.XCommand#eagerLoadState()
108         */
109        @Override
110        protected void eagerLoadState() throws CommandException {
111            loadState();
112        }
113    
114        /* (non-Javadoc)
115         * @see org.apache.oozie.command.XCommand#eagerVerifyPrecondition()
116         */
117        @Override
118        protected void eagerVerifyPrecondition() throws CommandException, PreconditionException {
119            if (getJob().getStatus() == Job.Status.KILLED || getJob().getStatus() == Job.Status.FAILED
120                    || getJob().getStatus() == Job.Status.PREP || getJob().getStatus() == Job.Status.PREPPAUSED
121                    || getJob().getStatus() == Job.Status.PREPSUSPENDED) {
122                getLog().warn(
123                        "RerunCommand is not able to run because job status=" + getJob().getStatus() + ", jobid="
124                                + getJob().getId());
125                throw new PreconditionException(ErrorCode.E1100, "Not able to rerun the job Id= " + getJob().getId()
126                        + ". job is in wrong state= " + getJob().getStatus());
127            }
128        }
129    
130        /**
131         * This method will return the previous status.
132         *
133         * @return JOB Status
134         */
135        public Job.Status getPrevStatus() {
136            if (prevStatus != null) {
137                return prevStatus;
138            }
139            else {
140                return job.getStatus();
141            }
142        }
143    }