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