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
019package org.apache.oozie.command;
020
021import org.apache.oozie.ErrorCode;
022import org.apache.oozie.client.Job;
023
024/**
025 * Transition command for unpause the job. The derived class has to override these following functions:
026 * <p>
027 * updateJob() : update job status and attributes
028 * unpauseChildren() : submit or queue commands to unpause children
029 * notifyParent() : update the status to upstream if any
030 */
031public abstract class UnpauseTransitionXCommand extends TransitionXCommand<Void> {
032    /**
033     * The constructor for abstract class {@link UnpauseTransitionXCommand}
034     *
035     * @param name the command name
036     * @param type the command type
037     * @param priority the command priority
038     */
039    public UnpauseTransitionXCommand(String name, String type, int priority) {
040        super(name, type, priority);
041    }
042
043    /**
044     * Unpause actions associated with the job
045     *
046     * @throws CommandException thrown if failed to unpause actions
047     */
048    public abstract void unpauseChildren() throws CommandException;
049
050    /* (non-Javadoc)
051     * @see org.apache.oozie.command.TransitionXCommand#transitToNext()
052     */
053    @Override
054    public final void transitToNext() throws CommandException {
055        if (job == null) {
056            job = this.getJob();
057        }
058
059        if (job.getStatus() == Job.Status.PAUSED) {
060            job.setStatus(Job.Status.RUNNING);
061        }
062        else if (job.getStatus() == Job.Status.PAUSEDWITHERROR) {
063            job.setStatus(Job.Status.RUNNINGWITHERROR);
064        }
065        else if (job.getStatus() == Job.Status.PREPPAUSED) {
066            job.setStatus(Job.Status.PREP);
067        }
068        else {
069            throw new CommandException(ErrorCode.E1316, job.getId());
070        }
071
072        //TODO: to be revisited;
073        //job.setPending();
074    }
075
076    /* (non-Javadoc)
077     * @see org.apache.oozie.command.TransitionXCommand#execute()
078     */
079    @Override
080    protected Void execute() throws CommandException {
081        try {
082            transitToNext();
083            updateJob();
084            unpauseChildren();
085        }
086        finally {
087            notifyParent();
088        }
089        return null;
090    }
091}