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