mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 16:53:04 +08:00
refactor(exception): register exception reporters on Agent launcher
This commit is contained in:
@@ -3,8 +3,11 @@ package work.slhaf.partner.framework.agent;
|
||||
import lombok.NonNull;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import work.slhaf.partner.framework.agent.config.ConfigCenter;
|
||||
import work.slhaf.partner.framework.agent.exception.deprecated.AgentLaunchFailedException;
|
||||
import work.slhaf.partner.framework.agent.exception.AgentStartupException;
|
||||
import work.slhaf.partner.framework.agent.exception.ExceptionReporter;
|
||||
import work.slhaf.partner.framework.agent.exception.ExceptionReporterHandler;
|
||||
import work.slhaf.partner.framework.agent.factory.AgentRegisterFactory;
|
||||
import work.slhaf.partner.framework.agent.factory.component.annotation.AgentComponent;
|
||||
import work.slhaf.partner.framework.agent.factory.context.AgentContext;
|
||||
import work.slhaf.partner.framework.agent.interaction.AgentGatewayRegistration;
|
||||
import work.slhaf.partner.framework.agent.interaction.AgentGatewayRegistry;
|
||||
@@ -30,6 +33,7 @@ public final class Agent {
|
||||
|
||||
private final Class<?> applicationClass;
|
||||
private final Set<AgentGatewayRegistration> gatewayRegistrations = new LinkedHashSet<>();
|
||||
private final Set<ExceptionReporter> exceptionReporters = new LinkedHashSet<>();
|
||||
private final Set<LifecycleHook> preShutdownHooks = new LinkedHashSet<>();
|
||||
private final Set<LifecycleHook> postShutdownHooks = new LinkedHashSet<>();
|
||||
|
||||
@@ -42,6 +46,11 @@ public final class Agent {
|
||||
return this;
|
||||
}
|
||||
|
||||
public AgentApp addExceptionReporter(ExceptionReporter... exceptionReporters) {
|
||||
this.exceptionReporters.addAll(Set.of(exceptionReporters));
|
||||
return this;
|
||||
}
|
||||
|
||||
public AgentApp addPreShutdownHook(String name, Runnable action) {
|
||||
return addPreShutdownHook(name, 0, action);
|
||||
}
|
||||
@@ -60,23 +69,43 @@ public final class Agent {
|
||||
return this;
|
||||
}
|
||||
|
||||
public void launch() {
|
||||
public boolean launch() {
|
||||
try {
|
||||
// Keep startup order explicit so registries are ready before component scanning.
|
||||
for (ExceptionReporter exceptionReporter : exceptionReporters) {
|
||||
// AgentComponent will be initialized by factory
|
||||
if (exceptionReporter.getClass().isAnnotationPresent(AgentComponent.class)) {
|
||||
continue;
|
||||
}
|
||||
exceptionReporter.register();
|
||||
}
|
||||
// Load class
|
||||
StateCenter.INSTANCE.toString();
|
||||
// Register into config center
|
||||
ModelRuntimeRegistry.INSTANCE.register();
|
||||
AgentGatewayRegistry.INSTANCE.register();
|
||||
|
||||
for (AgentGatewayRegistration registration : gatewayRegistrations) {
|
||||
registration.register();
|
||||
}
|
||||
|
||||
registerShutdownHooks();
|
||||
|
||||
Path externalModuleDir = ConfigCenter.INSTANCE.getPaths().getResourcesDir().resolve("module");
|
||||
AgentRegisterFactory.addScanDir(externalModuleDir.toString());
|
||||
AgentRegisterFactory.launch(applicationClass.getPackageName());
|
||||
|
||||
// Try to init configurable, and start config listening
|
||||
ConfigCenter.INSTANCE.initAll();
|
||||
ConfigCenter.INSTANCE.start();
|
||||
} catch (Exception e) {
|
||||
throw new AgentLaunchFailedException("Agent 启动失败", e);
|
||||
return true;
|
||||
} catch (AgentStartupException e) {
|
||||
ExceptionReporterHandler.INSTANCE.report(e);
|
||||
return false;
|
||||
} catch (Throwable t) {
|
||||
AgentStartupException wrapped = new AgentStartupException("Unexpected startup failure", "launcher", t);
|
||||
ExceptionReporterHandler.INSTANCE.report(wrapped);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user