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 RUNNING = new LauncherStatus(State.RUNNING); 030 public static final LauncherStatus COMPLETED = new LauncherStatus(State.COMPLETED); 031 public static final LauncherStatus FORCE_CLOSED = new LauncherStatus(State.FORCE_CLOSED); 032 public static final LauncherStatus FAILED = new LauncherStatus(State.FAILED); 033 private final State state; 034 private final Optional<Throwable> error; 035 036 private LauncherStatus(final State state) { 037 this(state, null); 038 } 039 040 041 private LauncherStatus(final State state, final Throwable ex) { 042 this.state = state; 043 this.error = Optional.ofNullable(ex); 044 } 045 046 public static final LauncherStatus FAILED(final Throwable ex) { 047 return new LauncherStatus(State.FAILED, ex); 048 } 049 050 public static final LauncherStatus FAILED(final Optional<Throwable> ex) { 051 return new LauncherStatus(State.FAILED, ex.orElse(null)); 052 } 053 054 public Optional<Throwable> getError() { 055 return this.error; 056 } 057 058 /** 059 * Compare the <b>State</b> of two LauncherStatus objects. 060 * Note that it does NOT compare the exceptions - just the states. 061 * 062 * @return True if both LauncherStatus objects are in the same state. 063 */ 064 @Override 065 public boolean equals(final Object other) { 066 return this == other || 067 (other instanceof LauncherStatus && ((LauncherStatus) other).state == this.state); 068 } 069 070 /** 071 * Has the job completed? 072 * 073 * @return True if the job has been completed, false otherwise. 074 */ 075 public final boolean isDone() { 076 switch (this.state) { 077 case FAILED: 078 case COMPLETED: 079 case FORCE_CLOSED: 080 return true; 081 default: 082 return false; 083 } 084 } 085 086 /** 087 * Has the job completed successfully? 088 * 089 * @return True if the job has been completed successfully, false otherwise. 090 */ 091 public final boolean isSuccess() { 092 return this.state == State.COMPLETED; 093 } 094 095 /** 096 * Is the job still running? 097 * 098 * @return True if the job is still running, false otherwise. 099 */ 100 public final boolean isRunning() { 101 return this.state == State.RUNNING; 102 } 103 104 @Override 105 public String toString() { 106 if (this.error.isPresent()) { 107 return this.state + "(" + this.error.get() + ")"; 108 } else { 109 return this.state.toString(); 110 } 111 } 112 113 /** 114 * The state the computation could be in. 115 */ 116 private enum State { 117 INIT, 118 RUNNING, 119 COMPLETED, 120 FAILED, 121 FORCE_CLOSED 122 } 123}