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.wf;
020
021import org.apache.oozie.WorkflowActionBean;
022import org.apache.oozie.WorkflowJobBean;
023import org.apache.oozie.action.ActionExecutor;
024import org.apache.oozie.client.Job;
025import org.apache.oozie.client.WorkflowAction;
026import org.apache.oozie.command.CommandException;
027import org.apache.oozie.command.XCommand;
028import org.apache.oozie.command.wf.ActionXCommand.ActionExecutorContext;
029
030public class ForkedActionStartXCommand extends ActionStartXCommand {
031
032    public ForkedActionStartXCommand(String actionId, String type) {
033        super(actionId, type);
034    }
035
036    public ForkedActionStartXCommand(WorkflowJobBean wfJob, String id, String type) {
037        super(wfJob, id, type);
038    }
039
040    protected ActionExecutorContext execute() throws CommandException {
041        super.execute();
042        return context;
043    }
044
045    @Override
046    public String getEntityKey() {
047        return actionId;
048    }
049
050    // In case of requeue follow the old approach.
051    @Override
052    protected void queue(XCommand<?> command, long msDelay) {
053
054        if (command instanceof ForkedActionStartXCommand) {
055            LOG.debug("Queueing ActionStartXCommand command");
056            super.queue(new ActionStartXCommand(wfAction.getId(), wfAction.getType()), msDelay);
057        }
058        else {
059            LOG.debug("Queueing " + command);
060            super.queue(command, msDelay);
061        }
062    }
063
064    // Job will be failed by SignalXcommand, because ForkedActionStartXCommand doesn't have lock on jobId.
065    @Override
066    public void failJob(ActionExecutor.Context context, WorkflowActionBean action) throws CommandException {
067        this.context.setJobStatus(Job.Status.FAILED);
068    }
069
070    @Override
071    protected void updateParentIfNecessary(WorkflowJobBean wfjob, int maxRetries) throws CommandException {
072    }
073
074    @Override
075    protected void handleNonTransient(ActionExecutor.Context context, ActionExecutor executor,
076            WorkflowAction.Status status) throws CommandException {
077        this.context.setJobStatus(Job.Status.SUSPENDED);
078    }
079
080    @Override
081    protected void handleError(ActionExecutorContext context, WorkflowJobBean workflow, WorkflowActionBean action)
082            throws CommandException {
083        this.context.setJobStatus(Job.Status.FAILED);
084    }
085
086    @Override
087    protected void updateJobLastModified() {
088    }
089
090    // Not killing job, setting flag so that signalX can kill the job
091    @Override
092    protected void endWF() {
093        context.setShouldEndWF(true);
094    }
095
096    @Override
097    protected void callActionEnd() {
098        queue(new ActionEndXCommand(wfAction.getId(), wfAction.getType()));
099    }
100
101    @Override
102    protected ActionExecutorContext  getContext(boolean isRetry, boolean isUserRetry){
103        return  new ActionXCommand.ForkedActionExecutorContext(wfJob, wfAction, isRetry, isUserRetry);
104    }
105
106}