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 LauncherStatus failed(final Throwable ex) { 047 return new LauncherStatus(State.FAILED, ex); 048 } 049 050 public static 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 @Override 071 public int hashCode() { 072 return state.hashCode(); 073 } 074 075 /** 076 * Has the job completed? 077 * 078 * @return True if the job has been completed, false otherwise. 079 */ 080 public boolean isDone() { 081 switch (this.state) { 082 case FAILED: 083 case COMPLETED: 084 case FORCE_CLOSED: 085 return true; 086 default: 087 return false; 088 } 089 } 090 091 /** 092 * Has the job completed successfully? 093 * 094 * @return True if the job has been completed successfully, false otherwise. 095 */ 096 public boolean isSuccess() { 097 return this.state == State.COMPLETED; 098 } 099 100 /** 101 * Is the job still running? 102 * 103 * @return True if the job is still running, false otherwise. 104 */ 105 public boolean isRunning() { 106 return this.state == State.RUNNING; 107 } 108 109 @Override 110 public String toString() { 111 if (this.error.isPresent()) { 112 return this.state + "(" + this.error.get() + ")"; 113 } else { 114 return this.state.toString(); 115 } 116 } 117 118 /** 119 * The state the computation could be in. 120 */ 121 private enum State { 122 INIT, 123 RUNNING, 124 COMPLETED, 125 FAILED, 126 FORCE_CLOSED 127 } 128}