Skip to content

Commit

Permalink
add some transaction on app upgrade and reload
Browse files Browse the repository at this point in the history
  • Loading branch information
blackstar-baba committed Dec 16, 2020
1 parent 5b29f5a commit 267dd14
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.socket.config.annotation.EnableWebSocket;

@SpringBootApplication
@Transactional
@EnableWebSocket
@EnableTransactionManagement
public class SoftwareLabApplication {


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

/**
* @author black-star
*
* <p>
* status constant
*/
public class DbConst {

public static final String COLUMN_ID="id";
public static final String COLUMN_ID = "id";

public static final String COLUMN_STATUS="status";
public static final String COLUMN_STATUS = "status";

public static final String COLUMN_TYPE="type";
public static final String COLUMN_TYPE = "type";

public static final String COLUMN_USER_ID = "user_id";

Expand All @@ -21,7 +21,6 @@ public class DbConst {

public static final String COLUMN_DOWNLOAD_STATUS = "download_status";


public static final int RUNNING_STATUS_START = 1;

public static final int RUNNING_STATUS_STOP = 0;
Expand All @@ -30,6 +29,10 @@ public class DbConst {

public static final int STATUS_NORMAL = 0;

public static final int APP_SOURCE_RELOAD = 1;

public static final int APP_SOURCE_UPGRADE = 2;

public static final int DOWNLOAD_STATUS_FINISH = 2;

public static final int DOWNLOAD_STATUS_BEGIN = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

import com.softwarelab.application.bean.SecurityUser;
import com.softwarelab.application.checker.ImageChecker;
import com.softwarelab.application.common.DbConst;
import com.softwarelab.application.entity.Instance;
import com.softwarelab.application.service.*;
import com.softwarelab.application.websocket.MessageWebSocketHandler;
import com.softwarelab.application.websocket.TerminalWebSocketHandler;
import com.softwarelab.application.common.DbConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator;
Expand All @@ -25,7 +24,6 @@
import java.util.Map;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

public static final String WS_PREFIX = "/api/ws/";
Expand Down Expand Up @@ -105,12 +103,11 @@ public void afterHandshake(ServerHttpRequest request, ServerHttpResponse respons
});
}

@Bean

public TerminalWebSocketHandler newTerminalHandler() {
return new TerminalWebSocketHandler(instanceService, containerService);
}

@Bean
public MessageWebSocketHandler newMessageHandler() {
return new MessageWebSocketHandler(imageChecker, containerService, appService, appVersionService, appSourceService);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,13 @@ public class AppSourceServiceImpl extends ServiceImpl<AppSourceMapper, AppSource


@Override
public boolean upgrade() {
AppSource appSource = list().get(0);
String forObject = restTemplate.getForObject("https://api.github.com/repos/CodeIsBeatiful/softwarelab-source/releases", String.class);
@Transactional
public boolean upgrade(){
try {
AppSource appSource = list().get(0);
appSource.setStatus(DbConst.APP_SOURCE_UPGRADE);
appSourceService.updateById(appSource);
String forObject = restTemplate.getForObject("https://api.github.com/repos/CodeIsBeatiful/softwarelab-source/releases", String.class);
CollectionType collectionType = objectMapper.getTypeFactory().constructCollectionType(List.class, SourceRelease.class);
List<SourceRelease> list = objectMapper.readValue(forObject, collectionType);
SourceRelease latestSourceRelease = list.get(0);
Expand All @@ -91,9 +94,8 @@ public boolean upgrade() {
return loadToDb();
}
}
} catch (IOException e) {
log.error("upgrade apps error", e);
return false;
} catch (Exception e){
throw new RuntimeException(e);
}
return true;
}
Expand All @@ -113,12 +115,15 @@ public boolean load(MultipartFile file) {
}

@Transactional
public boolean loadToDb() {
LocalDateTime now = LocalDateTime.now();
File appsFile = new File(appsDir);
String[] fileNames = appsFile.list();
for (String fileName : fileNames) {
try {
public boolean loadToDb(){
try {
AppSource appSource = list().get(0);
appSource.setStatus(DbConst.APP_SOURCE_RELOAD);
appSourceService.updateById(appSource);
LocalDateTime now = LocalDateTime.now();
File appsFile = new File(appsDir);
String[] fileNames = appsFile.list();
for (String fileName : fileNames) {
File appFile = new File(appsDir + File.separator + fileName);
AppSourceInfo appSourceInfo = objectMapper.readValue(appFile, AppSourceInfo.class);
String appName = fileName.split("\\.")[0];
Expand Down Expand Up @@ -155,27 +160,25 @@ public boolean loadToDb() {
}
}
}

} catch (IOException e) {
log.error("load to db error",e);
return false;
}
}
byte[] versionContext = FileUtil.getContent(targetDir + File.separator + versionFileName);
if (versionContext == null) {
log.error("version file can't find");
return false;
byte[] versionContext = FileUtil.getContent(targetDir + File.separator + versionFileName);
if (versionContext == null) {
log.error("version file can't find");
return false;

}
appSourceService.saveOrUpdate(AppSource.builder()
.id("00000000-0000-0000-0000-000000000000")
//set true version
.version(new String(versionContext, StandardCharsets.UTF_8))
.repository(null)
.createTime(now)
.updateTime(now)
.status(DbConst.STATUS_NORMAL)
.build());
} catch (Exception e){
throw new RuntimeException(e);
}
appSourceService.saveOrUpdate(AppSource.builder()
.id("00000000-0000-0000-0000-000000000000")
//set true version
.version(new String(versionContext, StandardCharsets.UTF_8))
.repository(null)
.createTime(now)
.updateTime(now)
.status(DbConst.STATUS_NORMAL)
.build());
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.softwarelab.application.bean.ContainerSetting;
import com.softwarelab.application.checker.ImageChecker;
import com.softwarelab.application.common.DbConst;
import com.softwarelab.application.entity.App;
import com.softwarelab.application.entity.AppSource;
import com.softwarelab.application.entity.AppVersion;
import com.softwarelab.application.service.ContainerService;
import com.softwarelab.application.service.IAppService;
import com.softwarelab.application.service.IAppSourceService;
import com.softwarelab.application.service.IAppVersionService;
import com.softwarelab.application.common.DbConst;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
Expand Down Expand Up @@ -75,7 +76,7 @@ private void processImageMessage(WebSocketSession session, WebSocketRequestMessa
}
}

private void processAppMessage(WebSocketSession session, WebSocketRequestMessage message) {
private void processAppMessage(WebSocketSession session, WebSocketRequestMessage message) throws Exception{
switch (message.getOperate()) {
case "upgrade":
processAppUpgrade(session, message.getContent());
Expand All @@ -89,7 +90,6 @@ private void processAppMessage(WebSocketSession session, WebSocketRequestMessage
}

private void processImageDownload(WebSocketSession session, String content) {

String[] split = content.split(":");
if (split.length != 2) {
//
Expand Down Expand Up @@ -136,29 +136,30 @@ private void processImageDownload(WebSocketSession session, String content) {

}

private void processAppUpgrade(WebSocketSession session, String content) {
//todo if is upgrading,ignore this time
String detail = appSourceService.upgrade() ? "success" : "failed";
WebSocketResponseMessage responseMessage = new WebSocketResponseMessage("success", "Upgrade " + detail);
try {
session.sendMessage(new TextMessage(objectMapper.writeValueAsBytes(responseMessage)));
} catch (IOException e) {
// do nothing
public void processAppUpgrade(WebSocketSession session, String content) throws Exception{
String detail;
AppSource appSource = appSourceService.list().get(0);
if (appSource.getStatus() > 0) {
detail = "App store is Upgrading or reloading";
} else {
detail = "Upgrade " + (appSourceService.upgrade() ? "success" : "failed");
}
WebSocketResponseMessage responseMessage = new WebSocketResponseMessage("success", detail);
session.sendMessage(new TextMessage(objectMapper.writeValueAsBytes(responseMessage)));
}

private void processAppReload(WebSocketSession session, String content) {
//todo if is reloading,ignore this time
String detail = appSourceService.loadToDb() ? "success" : "failed";
WebSocketResponseMessage responseMessage = new WebSocketResponseMessage("success", "Reload " + detail);
try {
session.sendMessage(new TextMessage(objectMapper.writeValueAsBytes(responseMessage)));
} catch (IOException e) {
// do nothing
public void processAppReload(WebSocketSession session, String content) throws Exception{
String detail;
AppSource appSource = appSourceService.list().get(0);
if (appSource.getStatus() > 0) {
detail = "App store is Upgrading or reloading";
} else {
detail = "Reload " + (appSourceService.loadToDb() ? "success" : "failed");
}
WebSocketResponseMessage responseMessage = new WebSocketResponseMessage("success", detail);
session.sendMessage(new TextMessage(objectMapper.writeValueAsBytes(responseMessage)));
}


@Override
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {
super.handleTransportError(session, exception);
Expand Down

0 comments on commit 267dd14

Please sign in to comment.