fix(agent): run system exit after agent launch failed

This commit is contained in:
2026-04-19 18:52:10 +08:00
parent a9e41eb4c7
commit b4d6be849b
2 changed files with 8 additions and 3 deletions

View File

@@ -6,9 +6,12 @@ import work.slhaf.partner.runtime.gateway.WebSocketGatewayRegistration;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Agent.newAgent(Main.class) boolean launched = Agent.newAgent(Main.class)
.addGatewayRegistration(WebSocketGatewayRegistration.INSTANCE) .addGatewayRegistration(WebSocketGatewayRegistration.INSTANCE)
.addConfigurable(new VectorClientRegistry()) .addConfigurable(new VectorClientRegistry())
.launch(); .launch();
if (!launched) {
System.exit(1);
}
} }
} }

View File

@@ -13,6 +13,8 @@ import java.time.ZonedDateTime
object AgentContext { object AgentContext {
private val log = LoggerFactory.getLogger(this::class.java)
private val _modules = private val _modules =
mutableMapOf<String, ModuleContextData<AbstractAgentModule>>() mutableMapOf<String, ModuleContextData<AbstractAgentModule>>()
@@ -156,7 +158,6 @@ object AgentContext {
} }
fun trigger(hooks: List<ShutdownHookDesc>, instances: Instances) { fun trigger(hooks: List<ShutdownHookDesc>, instances: Instances) {
val log = LoggerFactory.getLogger(AgentContext::class.java)
hooks.sortedBy { it.order } hooks.sortedBy { it.order }
.forEach { .forEach {
try { try {
@@ -168,7 +169,6 @@ object AgentContext {
} }
fun triggerLifecycleHooks(hooks: List<LifecycleShutdownHookDesc>) { fun triggerLifecycleHooks(hooks: List<LifecycleShutdownHookDesc>) {
val log = LoggerFactory.getLogger(AgentContext::class.java)
hooks.sortedBy { it.order } hooks.sortedBy { it.order }
.forEach { .forEach {
try { try {
@@ -180,6 +180,7 @@ object AgentContext {
} }
Runtime.getRuntime().addShutdownHook(Thread { Runtime.getRuntime().addShutdownHook(Thread {
log.info("Shutdown hooks triggering...")
val instances = computeInstances() val instances = computeInstances()
triggerLifecycleHooks(preShutdownHooks) triggerLifecycleHooks(preShutdownHooks)
shutdownHooks[ShutdownHookDesc.Type.RUNNING]?.let { trigger(it, instances) } shutdownHooks[ShutdownHookDesc.Type.RUNNING]?.let { trigger(it, instances) }
@@ -188,6 +189,7 @@ object AgentContext {
shutdownHooks[ShutdownHookDesc.Type.SUB]?.let { trigger(it, instances) } shutdownHooks[ShutdownHookDesc.Type.SUB]?.let { trigger(it, instances) }
shutdownHooks[ShutdownHookDesc.Type.CAPABILITY]?.let { trigger(it, instances) } shutdownHooks[ShutdownHookDesc.Type.CAPABILITY]?.let { trigger(it, instances) }
triggerLifecycleHooks(postShutdownHooks) triggerLifecycleHooks(postShutdownHooks)
log.info("Shutdown hooks triggered...")
}) })
} }