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