Skip to content

Commit b62e10b

Browse files
committed
Improve handling of registration failures
1 parent 8a66bc9 commit b62e10b

File tree

7 files changed

+103
-4
lines changed

7 files changed

+103
-4
lines changed

archetypes/wanaku-provider-archetype/src/main/resources/archetype-resources/src/main/resources/application.properties

+5
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ wanaku.service.provider.base-uri=${name.toLowerCase()}://
4040
# not set by the user
4141
# wanaku.service.provider.service.defaults.noop=true
4242

43+
# Registration options
44+
# wanaku.service.provider.register-delay-seconds=3
45+
# wanaku.service.provider.register-retries=3
46+
# wanaku.service.provider.register-retry-wait-seconds=1
47+
4348
# Valkey configuration
4449

4550
# Address of the Valkey host

archetypes/wanaku-tool-service-archetype/src/main/resources/archetype-resources/src/main/resources/application.properties

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ quarkus.log.category."ai.wanaku".level=INFO
3131
wanaku.service.routing.name=${name.toLowerCase()}
3232
wanaku.service.routing.base-uri=${name.toLowerCase()}://
3333

34+
# Registration options
35+
# wanaku.service.routing.register-delay-seconds=3
36+
# wanaku.service.routing.register-retries=3
37+
# wanaku.service.routing.register-retry-wait-seconds=1
38+
3439
# Valkey configuration
3540

3641
# Address of the Valkey host

core/core-services/src/main/java/ai/wanaku/core/services/config/WanakuServiceConfig.java

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ai.wanaku.core.services.config;
22

3+
import io.smallrye.config.WithDefault;
34
import java.util.Map;
45

56
import ai.wanaku.core.config.WanakuConfig;
@@ -19,4 +20,13 @@ interface Service {
1920
interface Credentials {
2021
Map<String, String> configurations();
2122
}
23+
24+
@WithDefault("3")
25+
int registerRetries();
26+
27+
@WithDefault("3")
28+
int registerDelaySeconds();
29+
30+
@WithDefault("1")
31+
int registerRetryWaitSeconds();
2232
}

core/core-services/src/main/java/ai/wanaku/core/services/provider/AbstractResourceDelegate.java

+36-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import java.util.HashMap;
1515
import java.util.List;
1616
import java.util.Map;
17+
import java.util.concurrent.ScheduledExecutorService;
18+
import java.util.concurrent.TimeUnit;
1719
import org.apache.camel.catalog.CamelCatalog;
1820
import org.apache.camel.catalog.DefaultCamelCatalog;
1921
import org.apache.camel.tooling.model.BaseOptionModel;
@@ -35,6 +37,9 @@ public abstract class AbstractResourceDelegate implements ResourceAcquirerDelega
3537
@Inject
3638
ServiceRegistry serviceRegistry;
3739

40+
@Inject
41+
ScheduledExecutorService executor;
42+
3843
/**
3944
* Gets the endpoint URI.
4045
* Here you build the Camel URI based on the request parameters.
@@ -131,9 +136,39 @@ protected Map<String, String> componentOptions(String name, Map<String, String>
131136
return opt;
132137
}
133138

139+
private void tryRegistering(String service, String address, int port) {
140+
int retries = config.registerRetries();
141+
boolean registered = false;
142+
do {
143+
try {
144+
serviceRegistry.register(ServiceTarget.provider(service, address, port), serviceConfigurations());
145+
registered = true;
146+
} catch (Exception e) {
147+
retries = waitAndRetry(service, e, retries);
148+
}
149+
} while (!registered && (retries > 0));
150+
}
151+
152+
private int waitAndRetry(String service, Exception e, int retries) {
153+
retries--;
154+
if (retries == 0) {
155+
LOG.errorf(e, "Failed to register service %s: %s. No more retries left", service, e.getMessage());
156+
return 0;
157+
} else {
158+
LOG.warnf("Failed to register service %s: %s. Retries left: %d", service, e.getMessage(), retries);
159+
}
160+
try {
161+
int waitSeconds = config.registerRetryWaitSeconds();
162+
Thread.sleep(waitSeconds * 1000);
163+
} catch (InterruptedException ex) {
164+
throw new RuntimeException(ex);
165+
}
166+
return retries;
167+
}
168+
134169
@Override
135170
public void register(String service, String address, int port) {
136-
serviceRegistry.register(ServiceTarget.provider(service, address, port), serviceConfigurations());
171+
executor.schedule(() -> tryRegistering(service, address, port), config.registerDelaySeconds(), TimeUnit.SECONDS);
137172
}
138173

139174
@Override

core/core-services/src/main/java/ai/wanaku/core/services/routing/AbstractRoutingDelegate.java

+35-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import ai.wanaku.core.services.config.WanakuRoutingConfig;
1313
import java.util.List;
1414
import java.util.Map;
15+
import java.util.concurrent.ScheduledExecutorService;
16+
import java.util.concurrent.TimeUnit;
1517
import org.jboss.logging.Logger;
1618

1719
/**
@@ -29,6 +31,8 @@ public abstract class AbstractRoutingDelegate implements InvocationDelegate {
2931
@Inject
3032
ServiceRegistry serviceRegistry;
3133

34+
@Inject
35+
ScheduledExecutorService executor;
3236

3337
/**
3438
* Convert the response in whatever format it is to a String
@@ -81,9 +85,39 @@ public Map<String, String> credentialsConfigurations() {
8185
return config.credentials().configurations();
8286
}
8387

88+
private void tryRegistering(String service, String address, int port) {
89+
int retries = config.registerRetries();
90+
boolean registered = false;
91+
do {
92+
try {
93+
serviceRegistry.register(ServiceTarget.toolInvoker(service, address, port), serviceConfigurations());
94+
registered = true;
95+
} catch (Exception e) {
96+
retries = waitAndRetry(service, e, retries);
97+
}
98+
} while (!registered && (retries > 0));
99+
}
100+
101+
private int waitAndRetry(String service, Exception e, int retries) {
102+
retries--;
103+
if (retries == 0) {
104+
LOG.errorf(e, "Failed to register service %s: %s. No more retries left", service, e.getMessage());
105+
return 0;
106+
} else {
107+
LOG.warnf("Failed to register service %s: %s. Retries left: %d", service, e.getMessage(), retries);
108+
}
109+
try {
110+
int waitSeconds = config.registerRetryWaitSeconds();
111+
Thread.sleep(waitSeconds * 1000);
112+
} catch (InterruptedException ex) {
113+
throw new RuntimeException(ex);
114+
}
115+
return retries;
116+
}
117+
84118
@Override
85119
public void register(String service, String address, int port) {
86-
serviceRegistry.register(ServiceTarget.toolInvoker(service, address, port), serviceConfigurations());
120+
executor.schedule(() -> tryRegistering(service, address, port), config.registerDelaySeconds(), TimeUnit.SECONDS);
87121
}
88122

89123
@Override

services/providers/wanaku-provider-ftp/src/main/resources/application.properties

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ wanaku.service.provider.base-uri=ftp://
1717

1818
wanaku.service.routing.service.configurations.username="Whether to do nothing or do something"
1919

20+
# Registration options
21+
# wanaku.service.provider.register-delay-seconds=3
22+
# wanaku.service.provider.register-retries=3
23+
# wanaku.service.provider.register-retry-wait-seconds=1
24+
2025
# Valkey configuration
2126

2227
# Address of the Valkey host

services/tools/wanaku-routing-tavily-service/src/main/resources/application.properties

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ quarkus.devservices.enabled = false
2020

2121
quarkus.grpc.server.host=0.0.0.0
2222
# If running multiple services on the same host, then you must pick an unique port
23-
%dev.quarkus.grpc.server.port=9004
24-
%test.quarkus.grpc.server.port=9004
23+
%dev.quarkus.grpc.server.port=9006
24+
%test.quarkus.grpc.server.port=9006
2525

2626
wanaku.service.routing.name=tavily
2727

@@ -30,6 +30,11 @@ quarkus.log.category."ai.wanaku".level=INFO
3030
%dev.quarkus.log.category."ai.wanaku".level=INFO
3131
%test.quarkus.log.category."ai.wanaku".level=INFO
3232

33+
# Registration options
34+
# wanaku.service.routing.register-delay-seconds=3
35+
# wanaku.service.routing.register-retries=3
36+
# wanaku.service.routing.register-retry-wait-seconds=1
37+
3338
# Valkey configuration
3439

3540
# Address of the Valkey host

0 commit comments

Comments
 (0)