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.coord;
020
021import java.util.Date;
022
023import org.apache.oozie.CoordinatorActionBean;
024import org.apache.oozie.ErrorCode;
025import org.apache.oozie.client.CoordinatorAction;
026import org.apache.oozie.command.CommandException;
027import org.apache.oozie.command.PreconditionException;
028import org.apache.oozie.executor.jpa.CoordActionGetForTimeoutJPAExecutor;
029import org.apache.oozie.executor.jpa.CoordActionQueryExecutor;
030import org.apache.oozie.executor.jpa.JPAExecutorException;
031import org.apache.oozie.executor.jpa.CoordActionQueryExecutor.CoordActionQuery;
032import org.apache.oozie.service.EventHandlerService;
033import org.apache.oozie.service.JPAService;
034import org.apache.oozie.service.Services;
035import org.apache.oozie.util.LogUtils;
036import org.apache.oozie.util.ParamChecker;
037
038/**
039 * This class updates the Time out in the action bean
040 */
041public class CoordActionTimeOutXCommand extends CoordinatorXCommand<Void> {
042    private CoordinatorActionBean actionBean;
043    private String user;
044    private String appName;
045    private JPAService jpaService = null;
046
047    public CoordActionTimeOutXCommand(CoordinatorActionBean actionBean, String user, String appName) {
048        super("coord_action_timeout", "coord_action_timeout", 1);
049        this.actionBean = ParamChecker.notNull(actionBean, "ActionBean");
050        this.user = ParamChecker.notEmpty(user, "user");
051        this.appName = ParamChecker.notEmpty(appName, "appName");
052    }
053
054    @Override
055    protected void setLogInfo() {
056        LogUtils.setLogInfo(actionBean.getId());
057    }
058
059    /* (non-Javadoc)
060     * @see org.apache.oozie.command.XCommand#execute()
061     */
062    @Override
063    protected Void execute() throws CommandException {
064        if (actionBean.getStatus() == CoordinatorAction.Status.WAITING) {
065            actionBean.setStatus(CoordinatorAction.Status.TIMEDOUT);
066            try {
067                queue(new CoordActionNotificationXCommand(actionBean), 100);
068                actionBean.setLastModifiedTime(new Date());
069                CoordActionQueryExecutor.getInstance().executeUpdate(
070                        CoordActionQuery.UPDATE_COORD_ACTION_STATUS_PENDING_TIME, actionBean);
071                if (EventHandlerService.isEnabled()) {
072                    generateEvent(actionBean, user, appName, null);
073                }
074            }
075            catch (JPAExecutorException e) {
076                throw new CommandException(e);
077            }
078        }
079        return null;
080    }
081
082    /* (non-Javadoc)
083     * @see org.apache.oozie.command.XCommand#getEntityKey()
084     */
085    @Override
086    public String getEntityKey() {
087        return actionBean.getJobId();
088    }
089
090    @Override
091    public String getKey() {
092        return getName() + "_" + actionBean.getId();
093    }
094
095    /* (non-Javadoc)
096     * @see org.apache.oozie.command.XCommand#isLockRequired()
097     */
098    @Override
099    protected boolean isLockRequired() {
100        return true;
101    }
102
103    /* (non-Javadoc)
104     * @see org.apache.oozie.command.XCommand#loadState()
105     */
106    @Override
107    protected void loadState() throws CommandException {
108        jpaService = Services.get().get(JPAService.class);
109        if (jpaService == null) {
110            throw new CommandException(ErrorCode.E0610);
111        }
112
113        try {
114            actionBean = jpaService.execute(new CoordActionGetForTimeoutJPAExecutor(actionBean.getId()));
115        }
116        catch (JPAExecutorException e) {
117            throw new CommandException(e);
118        }
119        LogUtils.setLogInfo(actionBean);
120    }
121
122    /* (non-Javadoc)
123     * @see org.apache.oozie.command.XCommand#verifyPrecondition()
124     */
125    @Override
126    protected void verifyPrecondition() throws CommandException, PreconditionException {
127        if (actionBean.getStatus() != CoordinatorAction.Status.WAITING) {
128            throw new PreconditionException(ErrorCode.E1100, "The coord action must have status "
129                    + CoordinatorAction.Status.WAITING + " but has status [" + actionBean.getStatus() + "]");
130        }
131    }
132}