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, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.client; 020 021import org.apache.reef.util.Optional; 022 023/** 024 * The status of a reef job spawned using the DriverLauncher class. 025 */ 026public final class LauncherStatus { 027 028 public static final LauncherStatus INIT = new LauncherStatus(State.INIT); 029 public static final LauncherStatus SUBMITTED = new LauncherStatus(State.SUBMITTED); 030 public static final LauncherStatus RUNNING = new LauncherStatus(State.RUNNING); 031 public static final LauncherStatus COMPLETED = new LauncherStatus(State.COMPLETED); 032 public static final LauncherStatus FORCE_CLOSED = new LauncherStatus(State.FORCE_CLOSED); 033 public static final LauncherStatus FAILED = new LauncherStatus(State.FAILED); 034 035 private final State state; 036 private final Optional<Throwable> error; 037 038 private LauncherStatus(final State state) { 039 this(state, null); 040 } 041 042 private LauncherStatus(final State state, final Throwable ex) { 043 this.state = state; 044 this.error = Optional.ofNullable(ex); 045 } 046 047 public static LauncherStatus failed(final Throwable ex) { 048 return new LauncherStatus(State.FAILED, ex); 049 } 050 051 public static LauncherStatus failed(final Optional<Throwable> ex) { 052 return new LauncherStatus(State.FAILED, ex.orElse(null)); 053 } 054 055 public Optional<Throwable> getError() { 056 return this.error; 057 } 058 059 /** 060 * Compare the <b>State</b> of two LauncherStatus objects. 061 * Note that it does NOT compare the exceptions - just the states. 062 * 063 * @return True if both LauncherStatus objects are in the same state. 064 */ 065 @Override 066 public boolean equals(final Object other) { 067 return this == other || 068 other instanceof LauncherStatus && ((LauncherStatus) other).state == this.state; 069 } 070 071 @Override 072 public int hashCode() { 073 return state.hashCode(); 074 } 075 076 /** 077 * Has the job completed? 078 * 079 * @return True if the job has been completed, false otherwise. 080 */ 081 public boolean isDone() { 082 switch (this.state) { 083 case FAILED: 084 case COMPLETED: 085 case FORCE_CLOSED: 086 return true; 087 default: 088 return false; 089 } 090 } 091 092 /** 093 * Has the job completed successfully? 094 * 095 * @return True if the job has been completed successfully, false otherwise. 096 */ 097 public boolean isSuccess() { 098 return this.state == State.COMPLETED; 099 } 100 101 /** 102 * Is the job still running? 103 * 104 * @return True if the job is still running, false otherwise. 105 */ 106 public boolean isRunning() { 107 return this.state == State.RUNNING; 108 } 109 110 @Override 111 public String toString() { 112 if (this.error.isPresent()) { 113 return this.state + "(" + this.error.get() + ")"; 114 } else { 115 return this.state.toString(); 116 } 117 } 118 119 /** 120 * The state the computation could be in. 121 */ 122 private enum State { 123 INIT, 124 SUBMITTED, 125 RUNNING, 126 COMPLETED, 127 FAILED, 128 FORCE_CLOSED 129 } 130}