Skip to content

Commit eb7284d

Browse files
authored
Transform CoreConfigurationService to immutable SchedulerConfiguration class (#597)
1 parent a9c3792 commit eb7284d

File tree

9 files changed

+89
-105
lines changed

9 files changed

+89
-105
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@ All notable changes to this project will be documented in this file.
66

77
### New Features
88

9-
- Use `iexec-core-library` in `iexec-worker`. (#595)
9+
- Add `iexec-core-library` dependency and use it. (#595)
1010
- Create `ConfigServerClient` instance and use it instead of `BlockchainAdapterApiClient`. (#596)
1111

1212
### Bug fixes
1313

1414
- Fix `LoginServiceTests#shouldLoginOnceOnSimultaneousCalls` test. (#587)
15-
- Alwyas use `WorkerpoolAuhorization` to retrieve JWT on Result Proxy. (#588)
15+
- Always use `WorkerpoolAuhorization` to retrieve JWT on Result Proxy. (#588)
1616

1717
### Quality
1818

1919
- Configure Gradle JVM Test Suite Plugin. (#589)
2020
- Remove `ResponseEntity` wrapper in feign client. (#593)
2121
- Use constructor injection in `Application` class. (#594)
22+
- Transform `CoreConfigurationService` to immutable `SchedulerConfiguration` class. (#597)
2223

2324
### Dependency Upgrades
2425

src/main/java/com/iexec/worker/PingService.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.iexec.worker;
1818

1919
import com.iexec.core.api.SchedulerClient;
20-
import com.iexec.worker.config.CoreConfigurationService;
2120
import com.iexec.worker.feign.LoginService;
2221
import com.iexec.worker.utils.AsyncUtils;
2322
import com.iexec.worker.utils.ExecutorUtils;
@@ -38,19 +37,18 @@ public class PingService {
3837
private static final int PING_RATE_IN_SECONDS = 10;
3938

4039
private final Executor executor;
41-
private final SchedulerClient coreClient;
42-
private final CoreConfigurationService coreConfigurationService;
40+
private final SchedulerClient schedulerClient;
4341
private final LoginService loginService;
4442
private final WorkerService workerService;
4543

46-
public PingService(SchedulerClient coreClient,
47-
CoreConfigurationService coreConfigurationService,
44+
private String coreSessionId;
45+
46+
public PingService(SchedulerClient schedulerClient,
4847
LoginService loginService,
4948
WorkerService workerService) {
5049
executor = ExecutorUtils
5150
.newSingleThreadExecutorWithFixedSizeQueue(1, "ping-");
52-
this.coreClient = coreClient;
53-
this.coreConfigurationService = coreConfigurationService;
51+
this.schedulerClient = schedulerClient;
5452
this.loginService = loginService;
5553
this.workerService = workerService;
5654
}
@@ -78,7 +76,7 @@ void pingScheduler() {
7876
log.debug("Sending ping to scheduler");
7977
final String sessionId;
8078
try {
81-
sessionId = coreClient.ping(loginService.getToken());
79+
sessionId = schedulerClient.ping(loginService.getToken());
8280
} catch (FeignException e) {
8381
log.warn("The worker cannot ping the core [status:{}]", e.status());
8482
if (e instanceof FeignException.Unauthorized) {
@@ -97,16 +95,15 @@ void pingScheduler() {
9795
if (now.getMinute() == 0 && now.getSecond() <= PING_RATE_IN_SECONDS) {
9896
log.info("Sent ping to scheduler [sessionId:{}]", sessionId);
9997
}
100-
String currentSessionId = coreConfigurationService.getCoreSessionId();
101-
if (StringUtils.isEmpty(currentSessionId)) {
98+
if (StringUtils.isEmpty(coreSessionId)) {
10299
log.info("First ping from the worker, setting the sessionId [coreSessionId:{}]", sessionId);
103-
coreConfigurationService.setCoreSessionId(sessionId);
100+
coreSessionId = sessionId;
104101
return;
105102
}
106-
if (!sessionId.equalsIgnoreCase(currentSessionId)) {
103+
if (!sessionId.equalsIgnoreCase(coreSessionId)) {
107104
// need to reconnect to the core by restarting the worker
108105
log.warn("Scheduler seems to have restarted [currentSessionId:{}, coreSessionId:{}]",
109-
currentSessionId, sessionId);
106+
coreSessionId, sessionId);
110107
workerService.restartGracefully();
111108
}
112109
}

src/main/java/com/iexec/worker/config/CoreConfigurationService.java renamed to src/main/java/com/iexec/worker/config/SchedulerConfiguration.java

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,59 +21,29 @@
2121
import feign.Logger;
2222
import org.springframework.beans.factory.annotation.Value;
2323
import org.springframework.context.annotation.Bean;
24-
import org.springframework.stereotype.Service;
24+
import org.springframework.context.annotation.Configuration;
2525

26-
import javax.annotation.PostConstruct;
27-
import java.net.MalformedURLException;
28-
import java.net.URL;
26+
@Configuration
27+
public class SchedulerConfiguration {
2928

30-
@Service
31-
public class CoreConfigurationService {
29+
private final String protocol;
30+
private final String host;
31+
private final String port;
3232

33-
@Value("${core.protocol}")
34-
private String coreProtocol;
35-
36-
@Value("${core.host}")
37-
private String coreHost;
38-
39-
@Value("${core.port}")
40-
private String corePort;
41-
42-
private URL url;
43-
44-
private String coreSessionId;
45-
46-
@PostConstruct
47-
public void run() throws MalformedURLException {
48-
url = new URL(coreProtocol, coreHost, Integer.parseInt(corePort), "");
33+
public SchedulerConfiguration(@Value("${core.protocol}") String protocol,
34+
@Value("${core.host}") String host,
35+
@Value("${core.port}") String port) {
36+
this.protocol = protocol;
37+
this.host = host;
38+
this.port = port;
4939
}
5040

5141
public String getUrl() {
52-
return url.toString();
53-
}
54-
55-
public String getProtocol() {
56-
return url.getProtocol();
57-
}
58-
59-
public String getHost() {
60-
return url.getHost();
61-
}
62-
63-
public int getPort() {
64-
return url.getPort();
65-
}
66-
67-
public String getCoreSessionId() {
68-
return coreSessionId;
69-
}
70-
71-
public void setCoreSessionId(String coreSessionId) {
72-
this.coreSessionId = coreSessionId;
42+
return String.format("%s://%s:%s", protocol, host, port);
7343
}
7444

7545
@Bean
7646
SchedulerClient schedulerClient() {
77-
return SchedulerClientBuilder.getInstance(Logger.Level.FULL, url.toString());
47+
return SchedulerClientBuilder.getInstance(Logger.Level.FULL, getUrl());
7848
}
7949
}

src/main/java/com/iexec/worker/pubsub/StompClientService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 IEXEC BLOCKCHAIN TECH
2+
* Copyright 2020-2024 IEXEC BLOCKCHAIN TECH
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
1616

1717
package com.iexec.worker.pubsub;
1818

19-
import com.iexec.worker.config.CoreConfigurationService;
19+
import com.iexec.worker.config.SchedulerConfiguration;
2020
import com.iexec.worker.utils.AsyncUtils;
2121
import lombok.extern.slf4j.Slf4j;
2222
import org.springframework.context.ApplicationEventPublisher;
@@ -51,10 +51,10 @@ public class StompClientService {
5151
private StompSession stompSession;
5252

5353
public StompClientService(ApplicationEventPublisher applicationEventPublisher,
54-
CoreConfigurationService coreConfigService,
54+
SchedulerConfiguration schedulerConfiguration,
5555
WebSocketStompClient stompClient) {
5656
this.eventPublisher = applicationEventPublisher;
57-
this.webSocketServerUrl = coreConfigService.getUrl() + "/connect";
57+
this.webSocketServerUrl = schedulerConfiguration.getUrl() + "/connect";
5858
this.stompClient = stompClient;
5959
}
6060

src/main/java/com/iexec/worker/worker/WorkerService.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2023 IEXEC BLOCKCHAIN TECH
2+
* Copyright 2020-2024 IEXEC BLOCKCHAIN TECH
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,8 +18,8 @@
1818

1919
import com.iexec.common.config.WorkerModel;
2020
import com.iexec.commons.poco.utils.WaitUtils;
21-
import com.iexec.worker.config.CoreConfigurationService;
2221
import com.iexec.worker.config.PublicConfigurationService;
22+
import com.iexec.worker.config.SchedulerConfiguration;
2323
import com.iexec.worker.config.WorkerConfigurationService;
2424
import com.iexec.worker.docker.DockerService;
2525
import com.iexec.worker.feign.CustomCoreFeignClient;
@@ -33,14 +33,13 @@
3333

3434
import java.util.List;
3535

36-
3736
@Slf4j
3837
@Service
3938
public class WorkerService {
4039

4140
private final String workerWalletAddress;
4241
private final WorkerConfigurationService workerConfigService;
43-
private final CoreConfigurationService coreConfigService;
42+
private final SchedulerConfiguration schedulerConfiguration;
4443
private final PublicConfigurationService publicConfigService;
4544
private final CustomCoreFeignClient customCoreFeignClient;
4645
private final BuildProperties buildProperties;
@@ -50,7 +49,7 @@ public class WorkerService {
5049

5150
public WorkerService(
5251
WorkerConfigurationService workerConfigService,
53-
CoreConfigurationService coreConfigService,
52+
SchedulerConfiguration schedulerConfiguration,
5453
PublicConfigurationService publicConfigService,
5554
CustomCoreFeignClient customCoreFeignClient,
5655
BuildProperties buildProperties,
@@ -59,7 +58,7 @@ public WorkerService(
5958
DockerService dockerService,
6059
String workerWalletAddress) {
6160
this.workerConfigService = workerConfigService;
62-
this.coreConfigService = coreConfigService;
61+
this.schedulerConfiguration = schedulerConfiguration;
6362
this.publicConfigService = publicConfigService;
6463
this.customCoreFeignClient = customCoreFeignClient;
6564
this.buildProperties = buildProperties;
@@ -71,7 +70,7 @@ public WorkerService(
7170

7271
public boolean registerWorker() {
7372
log.info("Number of CPUs [CPUs:{}]", workerConfigService.getCpuCount());
74-
log.info("Core URL [url:{}]", coreConfigService.getUrl());
73+
log.info("Core URL [url:{}]", schedulerConfiguration.getUrl());
7574
log.info("Core version [version:{}]", customCoreFeignClient.getCoreVersion());
7675
log.info("Getting public configuration from the core...");
7776
log.info("Got public configuration from the core [config:{}]", publicConfigService.getPublicConfiguration());

0 commit comments

Comments
 (0)