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.sla;
020
021import org.apache.oozie.CoordinatorActionBean;
022import org.apache.oozie.WorkflowJobBean;
023import org.apache.oozie.command.CommandException;
024import org.apache.oozie.executor.jpa.CoordActionQueryExecutor;
025import org.apache.oozie.executor.jpa.CoordActionQueryExecutor.CoordActionQuery;
026import org.apache.oozie.executor.jpa.JPAExecutorException;
027import org.apache.oozie.executor.jpa.WorkflowJobQueryExecutor;
028import org.apache.oozie.executor.jpa.WorkflowJobQueryExecutor.WorkflowJobQuery;
029import org.apache.oozie.sla.SLACalcStatus;
030import org.apache.oozie.util.LogUtils;
031
032public class SLACoordActionJobEventXCommand extends SLAJobEventXCommand {
033    CoordinatorActionBean ca;
034    WorkflowJobBean wf;
035
036    public SLACoordActionJobEventXCommand(SLACalcStatus slaCalc, long lockTimeOut) {
037        super(slaCalc, lockTimeOut);
038    }
039
040    @Override
041    protected void loadState() throws CommandException {
042        try {
043            ca = CoordActionQueryExecutor.getInstance().get(CoordActionQuery.GET_COORD_ACTION_FOR_SLA, slaCalc.getId());
044            if (ca.getExternalId() != null) {
045                wf = WorkflowJobQueryExecutor.getInstance().get(WorkflowJobQuery.GET_WORKFLOW_FOR_SLA, ca.getExternalId());
046            }
047            LogUtils.setLogInfo(ca);
048        }
049        catch (JPAExecutorException e) {
050            throw new CommandException(e);
051        }
052    }
053
054
055    protected void updateJobInfo() {
056        if (ca.isTerminalStatus()) {
057            setEnded(true);
058            setEndMiss(ca.isTerminalWithFailure());
059            slaCalc.setActualEnd(ca.getLastModifiedTime());
060            if (wf != null) {
061                if (wf.getEndTime() != null) {
062                    if (slaCalc.getExpectedEnd() != null
063                            && wf.getEndTime().getTime() > slaCalc.getExpectedEnd().getTime()) {
064                        setEndMiss(true);
065                    }
066                    slaCalc.setActualEnd(wf.getEndTime());
067                }
068                slaCalc.setActualStart(wf.getStartTime());
069            }
070        }
071        else {
072            if (wf != null) {
073                slaCalc.setActualStart(wf.getStartTime());
074            }
075        }
076        slaCalc.setJobStatus(ca.getStatusStr());
077    }
078
079
080}