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