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