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 sets a Coordinator action's status to SKIPPED
040 */
041public class CoordActionSkipXCommand extends CoordinatorXCommand<Void> {
042    private CoordinatorActionBean actionBean;
043    private String user;
044    private String appName;
045    private JPAService jpaService = null;
046
047    public CoordActionSkipXCommand(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    @Override
060    protected Void execute() throws CommandException {
061        if (actionBean.getStatus() == CoordinatorAction.Status.WAITING
062                || actionBean.getStatus() == CoordinatorAction.Status.READY) {
063            LOG.info("Setting action [{0}] status to SKIPPED", actionBean.getId());
064            actionBean.setStatus(CoordinatorAction.Status.SKIPPED);
065            try {
066                queue(new CoordActionNotificationXCommand(actionBean), 100);
067                actionBean.setLastModifiedTime(new Date());
068                CoordActionQueryExecutor.getInstance().executeUpdate(
069                        CoordActionQuery.UPDATE_COORD_ACTION_STATUS_PENDING_TIME, actionBean);
070                if (EventHandlerService.isEnabled()) {
071                    generateEvent(actionBean, user, appName, null);
072                }
073            }
074            catch (JPAExecutorException e) {
075                throw new CommandException(e);
076            }
077        }
078        return null;
079    }
080
081    @Override
082    public String getEntityKey() {
083        return actionBean.getJobId();
084    }
085
086    @Override
087    public String getKey() {
088        return getName() + "_" + actionBean.getId();
089    }
090
091    @Override
092    protected boolean isLockRequired() {
093        return true;
094    }
095
096    @Override
097    protected void loadState() throws CommandException {
098        jpaService = Services.get().get(JPAService.class);
099        if (jpaService == null) {
100            throw new CommandException(ErrorCode.E0610);
101        }
102
103        try {
104            actionBean = jpaService.execute(new CoordActionGetForTimeoutJPAExecutor(actionBean.getId()));
105        }
106        catch (JPAExecutorException e) {
107            throw new CommandException(e);
108        }
109        LogUtils.setLogInfo(actionBean);
110    }
111
112    @Override
113    protected void verifyPrecondition() throws CommandException, PreconditionException {
114        if (!(actionBean.getStatus() == CoordinatorAction.Status.WAITING
115                || actionBean.getStatus() == CoordinatorAction.Status.READY)) {
116            throw new PreconditionException(ErrorCode.E1100, "The coord action must have status "
117                    + CoordinatorAction.Status.WAITING + " or " + CoordinatorAction.Status.READY
118                    + " but has status [" + actionBean.getStatus() + "]");
119        }
120    }
121}