Skip to content

Commit 3eb8e3c

Browse files
author
innokenty
committed
use beanloader to watch for quota changes
1 parent b3e9474 commit 3eb8e3c

File tree

3 files changed

+32
-133
lines changed

3 files changed

+32
-133
lines changed

proxy/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
<version>${project.version}</version>
2828
</dependency>
2929

30+
<dependency>
31+
<groupId>ru.yandex.qatools.beanloader</groupId>
32+
<artifactId>beanloader</artifactId>
33+
<version>2.0</version>
34+
</dependency>
35+
3036
<!-- Commons -->
3137
<dependency>
3238
<groupId>commons-io</groupId>

proxy/src/main/java/ru/qatools/gridrouter/ConfigRepository.java

Lines changed: 26 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,35 @@
55
import org.slf4j.LoggerFactory;
66
import org.springframework.beans.factory.annotation.Value;
77
import org.springframework.stereotype.Repository;
8+
import ru.qatools.beanloader.BeanChangeListener;
9+
import ru.qatools.beanloader.BeanWatcher;
810
import ru.qatools.gridrouter.config.Browser;
911
import ru.qatools.gridrouter.config.Browsers;
1012
import ru.qatools.gridrouter.config.Version;
1113
import ru.qatools.gridrouter.json.JsonCapabilities;
1214

1315
import javax.annotation.PostConstruct;
14-
import javax.annotation.PreDestroy;
1516
import javax.xml.bind.JAXB;
1617
import javax.xml.bind.JAXBException;
1718
import java.io.File;
1819
import java.io.IOException;
19-
import java.nio.file.DirectoryStream;
2020
import java.nio.file.Path;
2121
import java.util.HashMap;
2222
import java.util.Map;
2323

2424
import static java.nio.file.Files.newDirectoryStream;
25-
import static ru.qatools.gridrouter.utils.DirectoryWatcher.newWatcher;
2625

2726
/**
2827
* @author Alexander Andyashin [email protected]
2928
* @author Dmitry Baev [email protected]
3029
* @author Innokenty Shuvalov [email protected]
3130
*/
3231
@Repository
33-
public class ConfigRepository {
32+
public class ConfigRepository implements BeanChangeListener<Browsers> {
3433

3534
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigRepository.class);
3635

37-
public static final String XML_GLOB = "*.xml";
36+
private static final String XML_GLOB = "*.xml";
3837

3938
@Value("${grid.config.quota.directory}")
4039
private File quotaDirectory;
@@ -46,62 +45,42 @@ public class ConfigRepository {
4645

4746
private Map<String, String> routes = new HashMap<>();
4847

49-
private Thread quotaWatcherThread;
50-
5148
@PostConstruct
5249
public void init() throws JAXBException, IOException {
53-
initBrowsers(getQuotaPath());
5450
if (isQuotaHotReload()) {
5551
startQuotaWatcher();
56-
}
57-
}
58-
59-
@PreDestroy
60-
public void destroy() {
61-
if (isQuotaHotReload()) {
62-
stopQuotaWatcher();
52+
} else {
53+
loadQuotaOnce(getQuotaPath());
6354
}
6455
}
6556

6657
private void startQuotaWatcher() {
6758
LOGGER.debug("Starting quota watcher");
68-
setQuotaWatcherThread(newWatcher(getQuotaPath(), "glob:" + XML_GLOB, (kind, browserPath) -> {
69-
LOGGER.info("Reload configuration [{}] on event [{}]", browserPath, kind.name());
70-
initBrowsers(getQuotaPath());
71-
}));
72-
getQuotaWatcherThread().start();
73-
59+
try {
60+
BeanWatcher.watchFor(Browsers.class, getQuotaPath().toString(), XML_GLOB, this);
61+
} catch (IOException e) {
62+
LOGGER.error("Quota configuration loading failed: \n\n{}", e);
63+
}
7464
}
7565

76-
private void stopQuotaWatcher() {
77-
LOGGER.debug("Stopping quota watcher");
78-
if (getQuotaWatcherThread() != null && getQuotaWatcherThread().isAlive()) {
79-
getQuotaWatcherThread().interrupt();
66+
private void loadQuotaOnce(Path quotaPath) throws IOException {
67+
for (Path filename : newDirectoryStream(quotaPath, XML_GLOB)) {
68+
beanChanged(filename, JAXB.unmarshal(filename.toFile(), Browsers.class));
8069
}
8170
}
8271

83-
public void initBrowsers(Path quotaPath) {
84-
Map<String, Browsers> temporaryUserBrowsers = new HashMap<>(getUserBrowsers());
85-
Map<String, String> temporaryRoutes = new HashMap<>(getRoutes());
86-
try (DirectoryStream<Path> stream = newDirectoryStream(quotaPath, XML_GLOB)) {
87-
for (Path browsersPath : stream) {
88-
LOGGER.info("Load configuration from [{}]", browsersPath);
89-
String user = getFileName(browsersPath);
90-
try {
91-
Browsers browsers = JAXB.unmarshal(browsersPath.toFile(), Browsers.class);
92-
temporaryUserBrowsers.put(user, browsers);
93-
temporaryRoutes.putAll(browsers.getRoutesMap());
94-
95-
LOGGER.info("Loaded configuration for [{}] from [{}]: \n\n{}",
96-
user, browsersPath, browsers.toXml());
97-
} catch (Exception e) {
98-
LOGGER.error("Loaded configuration failed for [{}]: \n\n{}", browsersPath, e);
99-
}
100-
}
101-
setUserBrowsers(temporaryUserBrowsers);
102-
setRoutes(temporaryRoutes);
103-
} catch (IOException e) {
104-
LOGGER.error("Loaded configuration failed: \n\n{}", e);
72+
@Override
73+
public void beanChanged(Path filename, Browsers browsers) {
74+
if (browsers == null) {
75+
LOGGER.info("Configuration file [{}] was deleted. "
76+
+ "It is not purged from the running gridrouter process though.", filename);
77+
} else {
78+
LOGGER.info("Loading quota configuration file [{}]", filename);
79+
String user = getFileName(filename);
80+
userBrowsers.put(user, browsers);
81+
routes.putAll(browsers.getRoutesMap());
82+
LOGGER.info("Loaded quota configuration for [{}] from [{}]: \n\n{}",
83+
user, filename, browsers.toXml());
10584
}
10685
}
10786

@@ -117,10 +96,6 @@ public Map<String, String> getRoutes() {
11796
return routes;
11897
}
11998

120-
protected void setRoutes(Map<String, String> routes) {
121-
this.routes = routes;
122-
}
123-
12499
public Map<String, Browsers> getUserBrowsers() {
125100
return this.userBrowsers;
126101
}
@@ -129,10 +104,6 @@ protected Browsers getUserBrowsers(String user) {
129104
return getUserBrowsers().get(user);
130105
}
131106

132-
protected void setUserBrowsers(Map<String, Browsers> userBrowsers) {
133-
this.userBrowsers = userBrowsers;
134-
}
135-
136107
public Version findVersion(String user, JsonCapabilities caps) {
137108
return userBrowsers.get(user).find(caps.getBrowserName(), caps.getVersion());
138109
}
@@ -150,13 +121,4 @@ public Map<String, Integer> getBrowsersCountMap(String user) {
150121
}
151122
return countMap;
152123
}
153-
154-
public Thread getQuotaWatcherThread() {
155-
return quotaWatcherThread;
156-
}
157-
158-
public void setQuotaWatcherThread(Thread quotaWatcherThread) {
159-
this.quotaWatcherThread = quotaWatcherThread;
160-
}
161-
162124
}

proxy/src/main/java/ru/qatools/gridrouter/utils/DirectoryWatcher.java

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)