Skip to content

Commit

Permalink
新增优雅关闭或更新服务器
Browse files Browse the repository at this point in the history
  • Loading branch information
qinming99 committed Jan 11, 2021
1 parent aac7aac commit 5105f9e
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ public ResultVO<String> start(@RequestParam Integer type) throws Exception {
@ResponseBody
public ResultVO<String> stop(@RequestParam Integer type) throws Exception {
log.info("停止服务器,type={}", type);
ResultVO<String> resultVO = homeService.stop(type);
Thread.sleep(2000);
return resultVO;
return homeService.stop(type);
}

@GetMapping("/updateGame")
Expand Down
56 changes: 52 additions & 4 deletions src/main/java/com/tugos/dst/admin/service/HomeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public ResultVO<String> start(Integer type) {


/**
* 停止服务器进程
* 停止服务器进程 优雅关闭
*
* @param type 0 停止所有 1 停止地面 2 停止洞穴
*/
Expand All @@ -90,16 +90,64 @@ public ResultVO<String> stop(Integer type) {
Objects.requireNonNull(typeEnum);
switch (typeEnum) {
case STOP_ALL:
//停止所有
//停止所有,优雅关闭,10秒还未关闭强制关闭
shellService.shutdownMaster();
shellService.shutdownCaves();
if (shellService.getMasterStatus()) {
//运行中 睡眠
int sleep = 1;
while (sleep <= ShellService.MAX_SLEEP_SECOND) {
shellService.sleep(1);
if (shellService.getMasterStatus()) {
break;
}
sleep++;
}
}
if (shellService.getCavesStatus()) {
//运行中 睡眠
int sleep = 1;
while (sleep <= ShellService.MAX_SLEEP_SECOND) {
shellService.sleep(1);
if (shellService.getCavesStatus()) {
break;
}
sleep++;
}
}
shellService.stopMaster();
shellService.stopCaves();
break;
case STOP_MASTER:
//停止地面
//停止地面 优雅关闭,10秒还未关闭强制关闭
shellService.shutdownMaster();
if (shellService.getMasterStatus()) {
//运行中 睡眠
int sleep = 1;
while (sleep <= ShellService.MAX_SLEEP_SECOND) {
shellService.sleep(1);
if (shellService.getMasterStatus()) {
break;
}
sleep++;
}
}
shellService.stopMaster();
break;
case STOP_CAVES:
//停止洞穴
//停止洞穴 优雅关闭,10秒还未关闭强制关闭
shellService.shutdownCaves();
if (shellService.getCavesStatus()) {
//运行中 睡眠
int sleep = 1;
while (sleep <= ShellService.MAX_SLEEP_SECOND) {
shellService.sleep(1);
if (shellService.getCavesStatus()) {
break;
}
sleep++;
}
}
shellService.stopCaves();
break;
default:
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/com/tugos/dst/admin/service/ShellService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
Expand All @@ -24,6 +25,11 @@
@Slf4j
public class ShellService {

/**
* 最大睡眠时间 10秒
*/
public static final int MAX_SLEEP_SECOND = 10;

/**
* 获取地面状态
*
Expand Down Expand Up @@ -151,17 +157,71 @@ public List<String> stopCaves() {
return ShellUtil.runShell(DstConstant.STOP_CAVES_CMD);
}

/**
* 关闭地面服务 将执行保存保证
*/
public void shutdownMaster(){
String shell = "screen -S \""+DstConstant.SCREEN_WORK_MASTER_NAME+"\" -p 0 -X stuff \"c_shutdown(true)\\n\"";
ShellUtil.execShellBin(shell);
}

/**
* 关闭洞穴服务 将执行保存保证
*/
public void shutdownCaves(){
String shell = "screen -S \""+DstConstant.SCREEN_WORK_CAVES_NAME+"\" -p 0 -X stuff \"c_shutdown(true)\\n\"";
ShellUtil.execShellBin(shell);
}

/**
* 更新游戏 需要停止地面和洞穴进程
*
* @return 执行信息
*/
public List<String> updateGame() {
//优雅关闭
this.shutdownMaster();
this.shutdownCaves();
//检查地面与洞穴是否已经关闭,如果10秒之内还没有关闭就强制关闭
if (this.getMasterStatus()) {
//运行中 睡眠
int sleep = 1;
while (sleep <= MAX_SLEEP_SECOND) {
this.sleep(1);
if (this.getMasterStatus()) {
break;
}
sleep++;
}
}
if (this.getCavesStatus()) {
//运行中 睡眠
int sleep = 1;
while (sleep <= MAX_SLEEP_SECOND) {
this.sleep(1);
if (this.getCavesStatus()) {
break;
}
sleep++;
}
}
this.stopMaster();
this.stopCaves();
return ShellUtil.runShell(DstConstant.UPDATE_GAME_CMD);
}

/**
* 睡眠
* @param seconds 秒
*/
public void sleep(int seconds){
try {
TimeUnit.SECONDS.sleep(seconds);
} catch (InterruptedException e) {
log.error("睡眠异常:",e);
}
}

/**
* 检查目前所有的screen作业,并删除已经无法使用的screen作业
*/
Expand Down

0 comments on commit 5105f9e

Please sign in to comment.