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
56a467c
commit 90b7f96
Showing
22 changed files
with
251 additions
and
136 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
77 changes: 77 additions & 0 deletions
77
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,77 @@ | ||
package org.tron.common.exit; | ||
|
||
import java.util.Arrays; | ||
import java.util.concurrent.ThreadFactory; | ||
|
||
import com.google.common.base.Strings; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j(topic = "Exit") | ||
public class ExitManager { | ||
|
||
private static final ExitManager INSTANCE = new ExitManager(); | ||
private static final String[] CI_ENVIRONMENT_VARIABLES = { | ||
"CI", | ||
"JENKINS_URL", | ||
"TRAVIS", | ||
"CIRCLECI", | ||
"GITHUB_ACTIONS", | ||
"GITLAB_CI" | ||
}; | ||
|
||
private final ThreadFactory exitThreadFactory = r -> { | ||
Thread thread = new Thread(r, "System-Exit-Thread"); | ||
thread.setDaemon(true); | ||
return thread; | ||
}; | ||
|
||
private ExitManager() { | ||
} | ||
|
||
public static ExitManager getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
public void exit(ExitReason reason) { | ||
exit(reason, reason.getDescription()); | ||
} | ||
|
||
public void exit(ExitReason reason, String message) { | ||
exit(reason, message, null); | ||
} | ||
|
||
public void exit(ExitReason reason, Throwable cause) { | ||
exit(reason, cause.getMessage(), cause); | ||
} | ||
|
||
public void exit(ExitReason reason, String message, Throwable cause) { | ||
TronExitException exitException = new TronExitException(reason, message, cause); | ||
if (isRunningInCI()) { | ||
if (exitException.getExitReason() != ExitReason.NORMAL_SHUTDOWN) { | ||
throw exitException; | ||
} | ||
} else { | ||
logAndExit(exitException); | ||
} | ||
} | ||
|
||
private boolean isRunningInCI() { | ||
return Arrays.stream(CI_ENVIRONMENT_VARIABLES).anyMatch(System.getenv()::containsKey); | ||
} | ||
|
||
private void logAndExit(TronExitException exception) { | ||
ExitReason reason = exception.getExitReason(); | ||
String message = exception.getMessage(); | ||
if (reason == ExitReason.NORMAL_SHUTDOWN) { | ||
if (!Strings.isNullOrEmpty(message)) { | ||
logger.info("Exiting with code: {}, {}, {}.", | ||
reason.getCode(), reason.getDescription(), message); | ||
} | ||
} else { | ||
logger.error("Exiting with code: {}, {}, {}.", reason.getCode(), reason.getDescription(), | ||
message, exception); | ||
} | ||
Thread exitThread = exitThreadFactory.newThread(() -> System.exit(reason.getCode())); | ||
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,22 @@ | ||
package org.tron.common.exit; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum ExitReason { | ||
NORMAL_SHUTDOWN(0, "Normal"), | ||
CONFIG_ERROR(1, "Configuration"), | ||
INITIALIZATION_ERROR(2, "Initialization"), | ||
DATABASE_ERROR(3, "Database"), | ||
EVENT_ERROR(4, "Event Trigger"), | ||
FATAL_ERROR(-1, "Fatal"); | ||
|
||
|
||
private final int code; | ||
private final String description; | ||
|
||
ExitReason(int code, String description) { | ||
this.code = code; | ||
this.description = description; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
common/src/main/java/org/tron/common/exit/TronExitException.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,21 @@ | ||
package org.tron.common.exit; | ||
|
||
import lombok.Getter; | ||
|
||
|
||
@Getter | ||
public class TronExitException extends RuntimeException { | ||
|
||
private final ExitReason exitReason; | ||
|
||
public TronExitException(ExitReason exitReason, String message) { | ||
super(message); | ||
this.exitReason = exitReason; | ||
} | ||
|
||
public TronExitException(ExitReason exitReason, String message, Throwable cause) { | ||
super(message, cause); | ||
this.exitReason = exitReason; | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
common/src/main/java/org/tron/core/exception/ConfigException.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,16 @@ | ||
package org.tron.core.exception; | ||
|
||
public class ConfigException extends TronRuntimeException { | ||
|
||
public ConfigException() { | ||
super(); | ||
} | ||
|
||
public ConfigException(String message) { | ||
super(message); | ||
} | ||
|
||
public ConfigException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
common/src/main/java/org/tron/core/exception/EventTriggerException.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,16 @@ | ||
package org.tron.core.exception; | ||
|
||
public class EventTriggerException extends TronRuntimeException { | ||
|
||
public EventTriggerException() { | ||
super(); | ||
} | ||
|
||
public EventTriggerException(String message) { | ||
super(message); | ||
} | ||
|
||
public EventTriggerException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
common/src/main/java/org/tron/core/exception/GenesisBlockException.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,16 @@ | ||
package org.tron.core.exception; | ||
|
||
public class GenesisBlockException extends TronRuntimeException { | ||
|
||
public GenesisBlockException() { | ||
super(); | ||
} | ||
|
||
public GenesisBlockException(String message) { | ||
super(message); | ||
} | ||
|
||
public GenesisBlockException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
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
Oops, something went wrong.