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.AppType;
022import org.apache.oozie.WorkflowActionBean;
023import org.apache.oozie.WorkflowJobBean;
024import org.apache.oozie.command.CommandException;
025import org.apache.oozie.command.XCommand;
026import org.apache.oozie.command.coord.CoordActionUpdateXCommand;
027import org.apache.oozie.event.WorkflowActionEvent;
028import org.apache.oozie.event.WorkflowJobEvent;
029
030/**
031 * Abstract coordinator command class derived from XCommand
032 *
033 * @param <T>
034 */
035public abstract class WorkflowXCommand<T> extends XCommand<T> {
036
037    // Configuration on whether or not workflow and action directory will be deleted
038    // after workflow is done.
039    public static final String KEEP_WF_ACTION_DIR = "oozie.action.keep.action.dir";
040
041    protected static final String INSTR_SUCCEEDED_JOBS_COUNTER_NAME = "succeeded";
042    protected static final String INSTR_KILLED_JOBS_COUNTER_NAME = "killed";
043    protected static final String INSTR_FAILED_JOBS_COUNTER_NAME = "failed";
044
045    /**
046     * Base class constructor for workflow commands.
047     *
048     * @param name command name
049     * @param type command type
050     * @param priority command priority
051     */
052    public WorkflowXCommand(String name, String type, int priority) {
053        super(name, type, priority);
054    }
055
056    /**
057     * Base class constructor for workflow commands.
058     *
059     * @param name command name
060     * @param type command type
061     * @param priority command priority
062     * @param dryrun true if rerun is enabled for command
063     */
064    public WorkflowXCommand(String name, String type, int priority, boolean dryrun) {
065        super(name, type, priority, dryrun);
066    }
067
068    protected static void generateEvent(WorkflowJobBean wfJob, String errorCode, String errorMsg) {
069        if (eventService.isSupportedApptype(AppType.WORKFLOW_JOB.name())) {
070            WorkflowJobEvent event = new WorkflowJobEvent(wfJob.getId(), wfJob.getParentId(), wfJob.getStatus(),
071                    wfJob.getUser(), wfJob.getAppName(), wfJob.getStartTime(), wfJob.getEndTime());
072            event.setErrorCode(errorCode);
073            event.setErrorMessage(errorMsg);
074            eventService.queueEvent(event);
075        }
076    }
077
078    protected static void generateEvent(WorkflowJobBean wfJob) {
079        generateEvent(wfJob, null, null);
080    }
081
082    protected void generateEvent(WorkflowActionBean wfAction, String wfUser) {
083        if (eventService.isSupportedApptype(AppType.WORKFLOW_ACTION.name())) {
084            WorkflowActionEvent event = new WorkflowActionEvent(wfAction.getId(), wfAction.getJobId(),
085                    wfAction.getStatus(), wfUser, wfAction.getName(), wfAction.getStartTime(), wfAction.getEndTime());
086            event.setErrorCode(wfAction.getErrorCode());
087            event.setErrorMessage(wfAction.getErrorMessage());
088            eventService.queueEvent(event);
089        }
090    }
091
092    protected void updateParentIfNecessary(WorkflowJobBean wfjob, int maxRetries) throws CommandException {
093        // update coordinator action if the wf was actually started by a coord
094        if (wfjob.getParentId() != null && wfjob.getParentId().contains("-C@")) {
095            new CoordActionUpdateXCommand(wfjob, maxRetries).call();
096        }
097    }
098
099    protected void updateParentIfNecessary(WorkflowJobBean wfjob) throws CommandException {
100        // update coordinator action if the wf was actually started by a coord
101        if (wfjob.getParentId() != null && wfjob.getParentId().contains("-C@")) {
102            new CoordActionUpdateXCommand(wfjob).call();
103        }
104    }
105}