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.client.Job;
022import org.apache.oozie.executor.jpa.JPAExecutorException;
023
024/**
025 * StatusTransitXCommand is super class for Status Transit Command, it defines layout for Status Transit Commands. It
026 * tries change job status change after acquiring lock with zero timeout. StatusTransit Commands are not requeued.
027 */
028abstract public class StatusTransitXCommand extends XCommand<Void> {
029
030    /**
031     * Instantiates a new status transit x command.
032     *
033     * @param name the name
034     * @param type the type
035     * @param priority the priority
036     */
037    public StatusTransitXCommand(String name, String type, int priority) {
038        super(name, type, priority);
039    }
040
041    @Override
042    final protected long getLockTimeOut() {
043        return 0L;
044    }
045
046    @Override
047    final protected boolean isReQueueRequired() {
048        return false;
049    }
050
051    @Override
052    final protected boolean isLockRequired() {
053        return true;
054    }
055
056    @Override
057    protected Void execute() throws CommandException {
058
059        final Job.Status jobStatus = getJobStatus();
060        try {
061            if (jobStatus != null) {
062                updateJobStatus(jobStatus);
063            }
064        }
065        catch (JPAExecutorException e) {
066            throw new CommandException(e);
067        }
068
069        return null;
070
071    }
072
073    /**
074     * Gets the job status.
075     *
076     * @return the job status
077     * @throws CommandException the command exception
078     */
079    protected Job.Status getJobStatus() throws CommandException {
080        if (isTerminalState()) {
081            return getTerminalStatus();
082        }
083        if (isPausedState()) {
084            return getPausedState();
085        }
086        if (isSuspendedState()) {
087            return getSuspendedStatus();
088        }
089        if (isRunningState()) {
090            return getRunningState();
091        }
092        return null;
093    }
094
095    /**
096     * Checks if job is in terminal state.
097     *
098     * @return true, if is terminal state
099     */
100    protected abstract boolean isTerminalState();
101
102    /**
103     * Gets the job terminal status.
104     *
105     * @return the terminal status
106     */
107    protected abstract Job.Status getTerminalStatus();
108
109    /**
110     * Checks if job is in paused state.
111     *
112     * @return true, if job is in paused state
113     */
114    protected abstract boolean isPausedState();
115
116    /**
117     * Gets the job pause state.
118     *
119     * @return the paused state
120     */
121    protected abstract Job.Status getPausedState();
122
123    /**
124     * Checks if is in suspended state.
125     *
126     * @return true, if job is in suspended state
127     */
128    protected abstract boolean isSuspendedState();
129
130    /**
131     * Gets the suspended status.
132     *
133     * @return the suspended status
134     */
135    protected abstract Job.Status getSuspendedStatus();
136
137    /**
138     * Checks if job is in running state.
139     *
140     * @return true, if job is in running state
141     */
142    protected abstract boolean isRunningState();
143
144    /**
145     * Gets the job running state.
146     *
147     * @return the running state
148     */
149    protected abstract Job.Status getRunningState();
150
151    /**
152     * Update job status.
153     *
154     * @param status the status
155     * @throws JPAExecutorException the JPA executor exception
156     * @throws CommandException the command exception
157     */
158    protected abstract void updateJobStatus(Job.Status status) throws JPAExecutorException, CommandException;
159
160}