Skip to content

Commit

Permalink
support app config inherit
Browse files Browse the repository at this point in the history
  • Loading branch information
ihaolin committed Oct 25, 2016
1 parent e9205e3 commit e7a68ee
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 35 deletions.
4 changes: 2 additions & 2 deletions diablo-client-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>diablo</artifactId>
<groupId>me.hao0</groupId>
<version>1.1.1</version>
<version>1.2.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -22,7 +22,7 @@
<dependency>
<groupId>me.hao0</groupId>
<artifactId>diablo-client</artifactId>
<version>1.1.1</version>
<version>1.2.0</version>
</dependency>

<dependency>
Expand Down
4 changes: 2 additions & 2 deletions diablo-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>diablo</artifactId>
<groupId>me.hao0</groupId>
<version>1.1.1</version>
<version>1.2.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -22,7 +22,7 @@
<dependency>
<groupId>me.hao0</groupId>
<artifactId>diablo-common</artifactId>
<version>1.1.1</version>
<version>1.2.0</version>
</dependency>

<!-- use slf4j-api for logging -->
Expand Down
2 changes: 1 addition & 1 deletion diablo-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>diablo</artifactId>
<groupId>me.hao0</groupId>
<version>1.1.1</version>
<version>1.2.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
4 changes: 2 additions & 2 deletions diablo-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<artifactId>diablo</artifactId>
<groupId>me.hao0</groupId>
<version>1.1.1</version>
<version>1.2.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand All @@ -22,7 +22,7 @@
<dependency>
<groupId>me.hao0</groupId>
<artifactId>diablo-common</artifactId>
<version>1.1.1</version>
<version>1.2.0</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
import me.hao0.diablo.server.util.TowerUris;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
Expand Down Expand Up @@ -66,7 +63,8 @@ public JsonResponse pageApps(
public JsonResponse saveApp(
@RequestParam("appName") String appName,
@RequestParam("appKey") String appKey,
@RequestParam("appDesc") String appDesc){
@RequestParam("appDesc") String appDesc,
@RequestParam(value = "inheritAppId" ,defaultValue = "") Long inheritAppId){

App app = new App();
app.setAppName(appName);
Expand All @@ -79,6 +77,8 @@ public JsonResponse saveApp(
return JsonResponse.notOk(saveResp.getErr());
}

appService.inheritConfigs(inheritAppId, saveResp.getData());

return JsonResponse.ok(saveResp.getData());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,12 @@ public interface AppService {
* @return return true if delete successfully, or false
*/
Response<Boolean> delete(String appName);

/**
* Inherit the configs from one app (asynchronously)
* @param srcAppId the source app id
* @param destAppId the dest app id
* @return return true if inherit successfully
*/
Response<Boolean> inheritConfigs(Long srcAppId, Long destAppId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@
import com.google.common.collect.Lists;
import me.hao0.common.model.Page;
import me.hao0.common.util.Strings;
import me.hao0.common.xml.XmlWriters;
import me.hao0.diablo.common.util.CollectionUtil;
import me.hao0.diablo.server.dao.AppDao;
import me.hao0.diablo.server.dao.mgr.AppManager;
import me.hao0.diablo.server.model.App;
import me.hao0.diablo.server.model.Config;
import me.hao0.diablo.server.model.Response;
import me.hao0.diablo.server.service.AppService;
import me.hao0.diablo.server.service.ConfigService;
import me.hao0.diablo.server.support.Messages;
import me.hao0.diablo.server.util.Logs;
import me.hao0.diablo.server.util.Paging;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
Expand All @@ -37,6 +41,13 @@ public class AppServiceImpl implements AppService {
@Autowired
private AppManager appManager;

@Autowired
private ConfigService configService;

private static final ExecutorService APP_INHERITOR = Executors.newSingleThreadExecutor();

private static final Integer CONFIG_INHERIT_BATCH_SIZE = 100;

/**
* App cache for 5 mins
*/
Expand Down Expand Up @@ -127,4 +138,63 @@ public Response<Boolean> delete(String appName) {
return Response.notOk(messages.get("app.delete.failed"));
}
}

@Override
public Response<Boolean> inheritConfigs(Long srcAppId, Long destAppId) {

if (srcAppId != null && destAppId != null){
APP_INHERITOR.submit(new AppConfigInheritTask(srcAppId, destAppId));
}

return Response.ok(Boolean.TRUE);
}

private class AppConfigInheritTask implements Runnable{

private Long srcAppId;

private Long destAppId;

AppConfigInheritTask(Long srcAppId, Long destAppId){
this.srcAppId = srcAppId;
this.destAppId = destAppId;
}

@Override
public void run() {
if (appDao.findById(srcAppId) != null
&& appDao.findById(destAppId) != null){
Response<Page<Config>> pageResp;
List<Config> configs;
Response<Long> saveResp;
for(Integer pageNo = 1;;pageNo++){

pageResp = configService.pagingConfig(srcAppId, null, pageNo, CONFIG_INHERIT_BATCH_SIZE);
if (!pageResp.isSuccess()){
Logs.error("failed to paging config(srcAppId={}, destAppId={}, pageNo={}, pageSize={}) when inherit config, cause: {}",
srcAppId, destAppId, pageNo, CONFIG_INHERIT_BATCH_SIZE, pageResp.getErr());
continue;
}

configs = pageResp.getData().getData();
if (CollectionUtil.isEmpty(configs)){
return;
}

for (Config config : configs){
saveResp = configService.save(destAppId, config.getName(), config.getValue());
if (!saveResp.isSuccess()){
Logs.error("failed to save config(srcAppId={}, destAppId={}, name={}) when inherit config, cause: {}",
srcAppId, destAppId, config.getName(), saveResp.getErr());
}
}

if (configs.size() < CONFIG_INHERIT_BATCH_SIZE){
// the last page
return;
}
}
}
}
}
}
44 changes: 22 additions & 22 deletions diablo-server/src/main/resources/public/app.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<groupId>me.hao0</groupId>
<artifactId>diablo</artifactId>
<version>1.1.1</version>
<version>1.2.0</version>
<modules>
<module>diablo-common</module>
<module>diablo-server</module>
Expand Down

0 comments on commit e7a68ee

Please sign in to comment.