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 */
018package org.apache.oozie.command.coord;
019
020import java.util.Date;
021
022import org.apache.oozie.CoordinatorActionBean;
023import org.apache.oozie.ErrorCode;
024import org.apache.oozie.client.CoordinatorAction;
025import org.apache.oozie.command.CommandException;
026import org.apache.oozie.command.PreconditionException;
027import org.apache.oozie.executor.jpa.CoordActionGetForTimeoutJPAExecutor;
028import org.apache.oozie.executor.jpa.CoordActionQueryExecutor;
029import org.apache.oozie.executor.jpa.JPAExecutorException;
030import org.apache.oozie.executor.jpa.CoordActionQueryExecutor.CoordActionQuery;
031import org.apache.oozie.service.EventHandlerService;
032import org.apache.oozie.service.JPAService;
033import org.apache.oozie.service.Services;
034import org.apache.oozie.util.LogUtils;
035import org.apache.oozie.util.ParamChecker;
036
037/**
038 * This class sets a Coordinator action's status to SKIPPED
039 */
040public class CoordActionSkipXCommand extends CoordinatorXCommand<Void> {
041    private CoordinatorActionBean actionBean;
042    private String user;
043    private String appName;
044    private JPAService jpaService = null;
045
046    public CoordActionSkipXCommand(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    @Override
054    protected Void execute() throws CommandException {
055        if (actionBean.getStatus() == CoordinatorAction.Status.WAITING
056                || actionBean.getStatus() == CoordinatorAction.Status.READY) {
057            LOG.info("Setting action [{0}] status to SKIPPED", actionBean.getId());
058            actionBean.setStatus(CoordinatorAction.Status.SKIPPED);
059            try {
060                queue(new CoordActionNotificationXCommand(actionBean), 100);
061                actionBean.setLastModifiedTime(new Date());
062                CoordActionQueryExecutor.getInstance().executeUpdate(
063                        CoordActionQuery.UPDATE_COORD_ACTION_STATUS_PENDING_TIME, actionBean);
064                if (EventHandlerService.isEnabled()) {
065                    generateEvent(actionBean, user, appName, null);
066                }
067            }
068            catch (JPAExecutorException e) {
069                throw new CommandException(e);
070            }
071        }
072        return null;
073    }
074
075    @Override
076    public String getEntityKey() {
077        return actionBean.getJobId();
078    }
079
080    @Override
081    public String getKey() {
082        return getName() + "_" + actionBean.getId();
083    }
084
085    @Override
086    protected boolean isLockRequired() {
087        return true;
088    }
089
090    @Override
091    protected void loadState() throws CommandException {
092        jpaService = Services.get().get(JPAService.class);
093        if (jpaService == null) {
094            throw new CommandException(ErrorCode.E0610);
095        }
096
097        try {
098            actionBean = jpaService.execute(new CoordActionGetForTimeoutJPAExecutor(actionBean.getId()));
099        }
100        catch (JPAExecutorException e) {
101            throw new CommandException(e);
102        }
103        LogUtils.setLogInfo(actionBean, logInfo);
104    }
105
106    @Override
107    protected void verifyPrecondition() throws CommandException, PreconditionException {
108        if (!(actionBean.getStatus() == CoordinatorAction.Status.WAITING
109                || actionBean.getStatus() == CoordinatorAction.Status.READY)) {
110            throw new PreconditionException(ErrorCode.E1100, "The coord action must have status "
111                    + CoordinatorAction.Status.WAITING + " or " + CoordinatorAction.Status.READY
112                    + " but has status [" + actionBean.getStatus() + "]");
113        }
114    }
115}