001    /**
002     * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
003     * Licensed under the Apache License, Version 2.0 (the "License");
004     * you may not use this file except in compliance with the License.
005     * You may obtain a copy of the License at
006     *
007     *   http://www.apache.org/licenses/LICENSE-2.0
008     *
009     *  Unless required by applicable law or agreed to in writing, software
010     *  distributed under the License is distributed on an "AS IS" BASIS,
011     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012     *  See the License for the specific language governing permissions and
013     *  limitations under the License. See accompanying LICENSE file.
014     */
015    package org.apache.oozie.command.coord;
016    
017    import java.util.Date;
018    
019    import org.apache.oozie.CoordinatorActionBean;
020    import org.apache.oozie.ErrorCode;
021    import org.apache.oozie.client.CoordinatorAction;
022    import org.apache.oozie.command.CommandException;
023    import org.apache.oozie.command.PreconditionException;
024    import org.apache.oozie.executor.jpa.CoordActionGetJPAExecutor;
025    import org.apache.oozie.executor.jpa.JPAExecutorException;
026    import org.apache.oozie.service.JPAService;
027    import org.apache.oozie.service.Services;
028    import org.apache.oozie.util.LogUtils;
029    import org.apache.oozie.util.ParamChecker;
030    
031    /**
032     * This class updates the Time out in the action bean
033     */
034    public class CoordActionTimeOutXCommand extends CoordinatorXCommand<Void> {
035        private CoordinatorActionBean actionBean;
036        private JPAService jpaService = null;
037    
038        public CoordActionTimeOutXCommand(CoordinatorActionBean actionBean) {
039            super("coord_action_timeout", "coord_action_timeout", 1);
040            ParamChecker.notNull(actionBean, "ActionBean");
041            this.actionBean = actionBean;
042        }
043    
044        /* (non-Javadoc)
045         * @see org.apache.oozie.command.XCommand#execute()
046         */
047        @Override
048        protected Void execute() throws CommandException {
049            if (actionBean.getStatus() == CoordinatorAction.Status.WAITING) {
050                actionBean.setStatus(CoordinatorAction.Status.TIMEDOUT);
051                queue(new CoordActionNotificationXCommand(actionBean), 100);
052                actionBean.setLastModifiedTime(new Date());
053                try {
054                    jpaService.execute(new org.apache.oozie.executor.jpa.CoordActionUpdateJPAExecutor(actionBean));
055                }
056                catch (JPAExecutorException e) {
057                    throw new CommandException(e);
058                }
059            }
060            return null;
061        }
062    
063        /* (non-Javadoc)
064         * @see org.apache.oozie.command.XCommand#getEntityKey()
065         */
066        @Override
067        protected String getEntityKey() {
068            return actionBean.getJobId();
069        }
070    
071        /* (non-Javadoc)
072         * @see org.apache.oozie.command.XCommand#isLockRequired()
073         */
074        @Override
075        protected boolean isLockRequired() {
076            return true;
077        }
078    
079        /* (non-Javadoc)
080         * @see org.apache.oozie.command.XCommand#loadState()
081         */
082        @Override
083        protected void loadState() throws CommandException {
084            jpaService = Services.get().get(JPAService.class);
085            if (jpaService == null) {
086                throw new CommandException(ErrorCode.E0610);
087            }
088    
089            try {
090                actionBean = jpaService.execute(new CoordActionGetJPAExecutor(actionBean.getId()));
091            }
092            catch (JPAExecutorException e) {
093                throw new CommandException(e);
094            }
095            LogUtils.setLogInfo(actionBean, logInfo);
096        }
097    
098        /* (non-Javadoc)
099         * @see org.apache.oozie.command.XCommand#verifyPrecondition()
100         */
101        @Override
102        protected void verifyPrecondition() throws CommandException, PreconditionException {
103            if (actionBean.getStatus() != CoordinatorAction.Status.WAITING) {
104                throw new PreconditionException(ErrorCode.E1100);
105            }
106        }
107    }