Skip to content

Commit 95170ac

Browse files
committed
optimize template and repo data repersent class
1 parent 09975cc commit 95170ac

File tree

6 files changed

+25
-76
lines changed

6 files changed

+25
-76
lines changed

core/src/main/java/com/flowci/core/common/config/AppProperties.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.context.annotation.Bean;
2424
import org.springframework.context.annotation.Configuration;
2525
import org.springframework.context.annotation.PropertySource;
26+
import org.springframework.core.io.Resource;
2627
import org.springframework.validation.annotation.Validated;
2728

2829
import javax.validation.constraints.NotBlank;
@@ -106,7 +107,7 @@ public Minio minio() {
106107
@Data
107108
public static class Flow {
108109

109-
private String templatesUrl;
110+
private Resource templatesUrl;
110111
}
111112

112113
@Data
@@ -118,7 +119,7 @@ public static class Job {
118119
@Data
119120
public static class Plugin {
120121

121-
private String defaultRepo;
122+
private Resource defaultRepo;
122123

123124
private Boolean autoUpdate;
124125
}

core/src/main/java/com/flowci/core/common/manager/HttpRequestManager.java

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

core/src/main/java/com/flowci/core/flow/config/FlowConfig.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
import com.fasterxml.jackson.databind.ObjectMapper;
2121
import com.flowci.core.common.config.AppProperties;
2222
import com.flowci.core.common.helper.CacheHelper;
23-
import com.flowci.core.common.manager.HttpRequestManager;
2423
import com.flowci.core.flow.domain.Template;
2524
import com.flowci.tree.NodeTree;
2625
import com.github.benmanes.caffeine.cache.Cache;
2726
import lombok.extern.log4j.Log4j2;
2827
import org.springframework.beans.factory.annotation.Autowired;
2928
import org.springframework.context.annotation.Bean;
3029
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.core.io.Resource;
3131
import org.springframework.scheduling.TaskScheduler;
3232
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
3333

@@ -58,13 +58,13 @@ public Cache<String, List<String>> gitBranchCache() {
5858
}
5959

6060
@Bean("templates")
61-
public List<Template> getTemplates(HttpRequestManager httpManager) throws IOException {
62-
String body = httpManager.get(flowProperties.getTemplatesUrl());
61+
public List<Template> getTemplates() throws IOException {
62+
Resource r = flowProperties.getTemplatesUrl();
6363

6464
TypeReference<List<Template>> typeRef = new TypeReference<List<Template>>() {
6565
};
6666

67-
List<Template> list = objectMapper.readValue(body, typeRef);
67+
List<Template> list = objectMapper.readValue(r.getInputStream(), typeRef);
6868
log.info("Templates is loaded from {}", flowProperties.getTemplatesUrl());
6969
return list;
7070
}

core/src/main/java/com/flowci/core/plugin/service/PluginService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.flowci.core.plugin.domain.Plugin;
2020
import com.flowci.core.plugin.domain.PluginRepoInfo;
2121
import com.flowci.domain.Vars;
22+
import org.springframework.core.io.Resource;
2223

2324
import java.nio.file.Path;
2425
import java.util.Collection;
@@ -71,7 +72,7 @@ public interface PluginService {
7172
/**
7273
* Load plugin repo info
7374
*/
74-
List<PluginRepoInfo> load(String repoUrl);
75+
List<PluginRepoInfo> load(Resource repoUri);
7576

7677
/**
7778
* Git clone plugin repos in Async

core/src/main/java/com/flowci/core/plugin/service/PluginServiceImpl.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.fasterxml.jackson.databind.ObjectMapper;
2121
import com.flowci.core.common.config.AppProperties;
2222
import com.flowci.core.common.git.GitClient;
23-
import com.flowci.core.common.manager.HttpRequestManager;
2423
import com.flowci.core.common.manager.VarManager;
2524
import com.flowci.core.plugin.dao.PluginDao;
2625
import com.flowci.core.plugin.domain.Plugin;
@@ -38,18 +37,17 @@
3837
import org.springframework.beans.factory.annotation.Autowired;
3938
import org.springframework.context.ApplicationContext;
4039
import org.springframework.context.event.EventListener;
40+
import org.springframework.core.io.Resource;
4141
import org.springframework.core.task.TaskExecutor;
4242
import org.springframework.scheduling.annotation.Scheduled;
4343
import org.springframework.stereotype.Component;
4444

4545
import java.io.ByteArrayInputStream;
4646
import java.io.File;
4747
import java.io.IOException;
48-
import java.net.URL;
4948
import java.nio.file.Files;
5049
import java.nio.file.Path;
5150
import java.nio.file.Paths;
52-
import java.time.Instant;
5351
import java.util.*;
5452

5553
/**
@@ -91,9 +89,6 @@ public class PluginServiceImpl implements PluginService {
9189
@Autowired
9290
private VarManager varManager;
9391

94-
@Autowired
95-
private HttpRequestManager httpManager;
96-
9792
private final Object reloadLock = new Object();
9893

9994
@EventListener
@@ -192,13 +187,11 @@ public Path getDir(Plugin plugin) {
192187
}
193188

194189
@Override
195-
public List<PluginRepoInfo> load(String repoUrl) {
190+
public List<PluginRepoInfo> load(Resource repoUri) {
196191
try {
197-
repoUrl = repoUrl + "?t=" + Instant.now().toEpochMilli();
198-
String body = httpManager.get(repoUrl);
199-
return objectMapper.readValue(body, RepoListType);
192+
return objectMapper.readValue(repoUri.getInputStream(), RepoListType);
200193
} catch (Throwable e) {
201-
log.warn("Unable to load plugin repo '{}' : {}", repoUrl, e.getMessage());
194+
log.warn("Unable to load plugin repo '{}' : {}", repoUri, e.getMessage());
202195
return Collections.emptyList();
203196
}
204197
}
@@ -225,9 +218,7 @@ public void clone(List<PluginRepoInfo> repos) {
225218
public void reload() {
226219
synchronized (reloadLock) {
227220
pluginDao.deleteAll();
228-
229-
String repoUrl = pluginProperties.getDefaultRepo();
230-
List<PluginRepoInfo> repos = load(repoUrl);
221+
List<PluginRepoInfo> repos = load(pluginProperties.getDefaultRepo());
231222
clone(repos);
232223
}
233224
}

core/src/test/java/com/flowci/core/test/plugin/PluginServiceTest.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616

1717
package com.flowci.core.test.plugin;
1818

19-
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
20-
import static com.github.tomakehurst.wiremock.client.WireMock.get;
21-
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
22-
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
23-
2419
import com.flowci.core.plugin.domain.Plugin;
2520
import com.flowci.core.plugin.domain.PluginRepoInfo;
2621
import com.flowci.core.plugin.event.RepoCloneEvent;
@@ -30,18 +25,19 @@
3025
import com.flowci.domain.Version;
3126
import com.flowci.util.StringHelper;
3227
import com.github.tomakehurst.wiremock.junit.WireMockRule;
28+
import org.junit.*;
29+
import org.springframework.beans.factory.annotation.Autowired;
30+
import org.springframework.context.ApplicationListener;
31+
import org.springframework.core.io.UrlResource;
32+
3333
import java.io.IOException;
3434
import java.io.InputStream;
35+
import java.net.MalformedURLException;
3536
import java.util.List;
3637
import java.util.concurrent.CountDownLatch;
3738
import java.util.concurrent.TimeUnit;
38-
import org.junit.Assert;
39-
import org.junit.Before;
40-
import org.junit.ClassRule;
41-
import org.junit.Ignore;
42-
import org.junit.Test;
43-
import org.springframework.beans.factory.annotation.Autowired;
44-
import org.springframework.context.ApplicationListener;
39+
40+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
4541

4642
/**
4743
* @author yang
@@ -66,8 +62,8 @@ public void mockPluginRepo() throws IOException {
6662
}
6763

6864
@Test
69-
public void should_load_plugin_repos_from_url() {
70-
List<PluginRepoInfo> repos = pluginService.load(RepoURL);
65+
public void should_load_plugin_repos_from_url() throws MalformedURLException {
66+
List<PluginRepoInfo> repos = pluginService.load(new UrlResource(RepoURL));
7167
Assert.assertEquals(1, repos.size());
7268

7369
PluginRepoInfo repo = repos.get(0);
@@ -82,7 +78,7 @@ public void should_load_plugin_repos_from_url() {
8278
@Test
8379
public void should_clone_plugin_repo() throws Throwable {
8480
// init:
85-
List<PluginRepoInfo> repos = pluginService.load(RepoURL);
81+
List<PluginRepoInfo> repos = pluginService.load(new UrlResource(RepoURL));
8682

8783
// init counter
8884
CountDownLatch counter = new CountDownLatch(1);

0 commit comments

Comments
 (0)