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    protected static final String INSTR_SUCCEEDED_JOBS_COUNTER_NAME = "succeeded";
038    protected static final String INSTR_KILLED_JOBS_COUNTER_NAME = "killed";
039    protected static final String INSTR_FAILED_JOBS_COUNTER_NAME = "failed";
040
041    /**
042     * Base class constructor for workflow commands.
043     *
044     * @param name command name
045     * @param type command type
046     * @param priority command priority
047     */
048    public WorkflowXCommand(String name, String type, int priority) {
049        super(name, type, priority);
050    }
051
052    /**
053     * Base class constructor for workflow commands.
054     *
055     * @param name command name
056     * @param type command type
057     * @param priority command priority
058     * @param dryrun true if rerun is enabled for command
059     */
060    public WorkflowXCommand(String name, String type, int priority, boolean dryrun) {
061        super(name, type, priority, dryrun);
062    }
063
064    protected static void generateEvent(WorkflowJobBean wfJob, String errorCode, String errorMsg) {
065        if (eventService.isSupportedApptype(AppType.WORKFLOW_JOB.name())) {
066            WorkflowJobEvent event = new WorkflowJobEvent(wfJob.getId(), wfJob.getParentId(), wfJob.getStatus(),
067                    wfJob.getUser(), wfJob.getAppName(), wfJob.getStartTime(), wfJob.getEndTime());
068            event.setErrorCode(errorCode);
069            event.setErrorMessage(errorMsg);
070            eventService.queueEvent(event);
071        }
072    }
073
074    protected static void generateEvent(WorkflowJobBean wfJob) {
075        generateEvent(wfJob, null, null);
076    }
077
078    protected void generateEvent(WorkflowActionBean wfAction, String wfUser) {
079        if (eventService.isSupportedApptype(AppType.WORKFLOW_ACTION.name())) {
080            WorkflowActionEvent event = new WorkflowActionEvent(wfAction.getId(), wfAction.getJobId(),
081                    wfAction.getStatus(), wfUser, wfAction.getName(), wfAction.getStartTime(), wfAction.getEndTime());
082            event.setErrorCode(wfAction.getErrorCode());
083            event.setErrorMessage(wfAction.getErrorMessage());
084            eventService.queueEvent(event);
085        }
086    }
087
088    protected void updateParentIfNecessary(WorkflowJobBean wfjob, int maxRetries) throws CommandException {
089        // update coordinator action if the wf was actually started by a coord
090        if (wfjob.getParentId() != null && wfjob.getParentId().contains("-C@")) {
091            new CoordActionUpdateXCommand(wfjob, maxRetries).call();
092        }
093    }
094
095    protected void updateParentIfNecessary(WorkflowJobBean wfjob) throws CommandException {
096        // update coordinator action if the wf was actually started by a coord
097        if (wfjob.getParentId() != null && wfjob.getParentId().contains("-C@")) {
098            new CoordActionUpdateXCommand(wfjob).call();
099        }
100    }
101}