forked from tronprotocol/java-tron
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f16c3f8
commit 94f1cd4
Showing
22 changed files
with
276 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
common/src/main/java/org/tron/common/exit/ExitManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package org.tron.common.exit; | ||
|
||
import java.util.Arrays; | ||
import java.util.Objects; | ||
import java.util.concurrent.ThreadFactory; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.tron.core.exception.TronExitException; | ||
|
||
@Slf4j(topic = "Exit") | ||
public class ExitManager { | ||
|
||
private static final String[] CI_ENVIRONMENT_VARIABLES = { | ||
"CI", | ||
"JENKINS_URL", | ||
"TRAVIS", | ||
"CIRCLECI", | ||
"GITHUB_ACTIONS", | ||
"GITLAB_CI" | ||
}; | ||
|
||
private static final int EXIT_CODE_NORMAL = 0; | ||
|
||
private static final ThreadFactory exitThreadFactory = r -> { | ||
Thread thread = new Thread(r, "System-Exit-Thread"); | ||
thread.setDaemon(true); | ||
return thread; | ||
}; | ||
|
||
private ExitManager() { | ||
} | ||
|
||
public static void exit() { | ||
exit((String) null); | ||
} | ||
|
||
public static void exit(String msg) { | ||
exit(msg, null); | ||
} | ||
|
||
public static void exit(TronExitException cause) { | ||
exit(cause.getMessage(), cause); | ||
} | ||
|
||
public static void exit(String msg, TronExitException cause) { | ||
TronExit exit = new TronExit(msg, cause); | ||
if (isRunningInCI()) { | ||
if (Objects.nonNull(cause)) { | ||
throw cause; | ||
} else if (Objects.nonNull(msg)) { | ||
logger.info("{}", msg); | ||
} | ||
} else { | ||
logAndExit(exit); | ||
} | ||
} | ||
|
||
private static boolean isRunningInCI() { | ||
return Arrays.stream(CI_ENVIRONMENT_VARIABLES).anyMatch(System.getenv()::containsKey); | ||
} | ||
|
||
private static void logAndExit(TronExit exit) { | ||
String msg = exit.getMsg(); | ||
TronExitException cause = exit.getException(); | ||
final int code = Objects.isNull(cause) ? EXIT_CODE_NORMAL : cause.getExitCode(); | ||
if (code == EXIT_CODE_NORMAL) { | ||
if (Objects.nonNull(msg)) { | ||
logger.info("Exiting, {}.", msg); | ||
} | ||
} else { | ||
if (Objects.isNull(msg)) { | ||
logger.error("Exiting with code: {}.", code, cause); | ||
} else { | ||
logger.error("Exiting with code: {}, {}.", code, msg, cause); | ||
} | ||
} | ||
Thread exitThread = exitThreadFactory.newThread(() -> System.exit(code)); | ||
exitThread.start(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.tron.common.exit; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.tron.core.exception.TronExitException; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class TronExit { | ||
|
||
private String msg; | ||
private TronExitException exception; | ||
} |
19 changes: 19 additions & 0 deletions
19
common/src/main/java/org/tron/core/exception/ConfigExitException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.tron.core.exception; | ||
|
||
public class ConfigExitException extends TronExitException { | ||
|
||
public ConfigExitException(String message) { | ||
super(message); | ||
setExitCode(1); | ||
} | ||
|
||
public ConfigExitException(String message, Throwable cause) { | ||
super(message, cause); | ||
setExitCode(1); | ||
} | ||
|
||
public ConfigExitException(Throwable cause) { | ||
super(cause); | ||
setExitCode(1); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
common/src/main/java/org/tron/core/exception/DatabaseExitException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.tron.core.exception; | ||
|
||
public class DatabaseExitException extends TronExitException { | ||
|
||
public DatabaseExitException(String message) { | ||
super(message); | ||
setExitCode(2); | ||
} | ||
|
||
public DatabaseExitException(String message, Throwable cause) { | ||
super(message, cause); | ||
setExitCode(2); | ||
} | ||
|
||
public DatabaseExitException(Throwable cause) { | ||
super(cause); | ||
setExitCode(2); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
common/src/main/java/org/tron/core/exception/EventExitException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.tron.core.exception; | ||
|
||
public class EventExitException extends TronExitException { | ||
|
||
public EventExitException(String message) { | ||
super(message); | ||
setExitCode(3); | ||
} | ||
|
||
public EventExitException(String message, Throwable cause) { | ||
super(message, cause); | ||
setExitCode(3); | ||
} | ||
|
||
public EventExitException(Throwable cause) { | ||
super(cause); | ||
setExitCode(3); | ||
} | ||
} |
Oops, something went wrong.