Authored by jack

update nginxsync

  1 +package com.monitor.nginxsync.model;
  2 +
  3 +import lombok.Data;
  4 +import org.apache.commons.lang.StringUtils;
  5 +
  6 +
  7 +/**
  8 + * Created by yoho on 2016/8/25.
  9 + */
  10 +@Data
  11 +public class LogMsgResp {
  12 + String taskId = StringUtils.EMPTY;
  13 +
  14 + int isFinished;
  15 +
  16 + String log;
  17 +}
@@ -6,12 +6,16 @@ import com.monitor.nginxsync.nio.service.LogService; @@ -6,12 +6,16 @@ import com.monitor.nginxsync.nio.service.LogService;
6 import io.netty.buffer.ByteBuf; 6 import io.netty.buffer.ByteBuf;
7 import io.netty.channel.ChannelHandlerContext; 7 import io.netty.channel.ChannelHandlerContext;
8 import io.netty.channel.SimpleChannelInboundHandler; 8 import io.netty.channel.SimpleChannelInboundHandler;
  9 +import org.slf4j.Logger;
  10 +import org.slf4j.LoggerFactory;
9 11
10 /** 12 /**
11 * Created by yoho on 2016/8/23. 13 * Created by yoho on 2016/8/23.
12 */ 14 */
13 public class LogMsgHandler extends SimpleChannelInboundHandler { 15 public class LogMsgHandler extends SimpleChannelInboundHandler {
14 16
  17 + public static final Logger DEBUG = LoggerFactory.getLogger(LogMsgHandler.class);
  18 +
15 public static final LogService LOG_SERVICE = new LogService(); 19 public static final LogService LOG_SERVICE = new LogService();
16 20
17 @Override 21 @Override
@@ -25,13 +29,19 @@ public class LogMsgHandler extends SimpleChannelInboundHandler { @@ -25,13 +29,19 @@ public class LogMsgHandler extends SimpleChannelInboundHandler {
25 result.readBytes(resultWarp); 29 result.readBytes(resultWarp);
26 30
27 31
  32 + DEBUG.info("recv log msg {}", new String(resultWarp));
  33 +
28 LogMsg logMsg = Constants.OBJECT_MAPPER.readValue(resultWarp, LogMsg.class); 34 LogMsg logMsg = Constants.OBJECT_MAPPER.readValue(resultWarp, LogMsg.class);
29 35
30 // 插入日志管理 36 // 插入日志管理
31 -  
32 LOG_SERVICE.inLogMsg(logMsg); 37 LOG_SERVICE.inLogMsg(logMsg);
33 38
34 // 释放资源,这行很关键 39 // 释放资源,这行很关键
35 result.release(); 40 result.release();
36 } 41 }
  42 +
  43 + @Override
  44 + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
  45 + DEBUG.warn("Catch exception {}", cause.getMessage());
  46 + }
37 } 47 }
@@ -52,6 +52,7 @@ public class LogService { @@ -52,6 +52,7 @@ public class LogService {
52 List<String> savedList = LOGMAPPER.get(taskId); 52 List<String> savedList = LOGMAPPER.get(taskId);
53 53
54 if (null != savedList && !savedList.isEmpty()) { 54 if (null != savedList && !savedList.isEmpty()) {
  55 +
55 CollectionUtils.addAll(logList, new String[savedList.size()]); 56 CollectionUtils.addAll(logList, new String[savedList.size()]);
56 57
57 Collections.copy(logList, savedList); 58 Collections.copy(logList, savedList);
@@ -68,6 +69,12 @@ public class LogService { @@ -68,6 +69,12 @@ public class LogService {
68 logMsg.setIsFinished(0); 69 logMsg.setIsFinished(0);
69 } else { 70 } else {
70 logMsg.setIsFinished(FINISHMAPPER.get(taskId)); 71 logMsg.setIsFinished(FINISHMAPPER.get(taskId));
  72 + if (1 == FINISHMAPPER.get(taskId)) {
  73 +
  74 + FINISHMAPPER.remove(taskId);
  75 +
  76 + LOGMAPPER.remove(taskId);
  77 + }
71 } 78 }
72 79
73 } 80 }
@@ -4,9 +4,11 @@ import com.google.common.base.Preconditions; @@ -4,9 +4,11 @@ import com.google.common.base.Preconditions;
4 import com.monitor.model.response.BaseResponse; 4 import com.monitor.model.response.BaseResponse;
5 import com.monitor.nginxsync.constant.Constants; 5 import com.monitor.nginxsync.constant.Constants;
6 import com.monitor.nginxsync.model.CmdTaskInfo; 6 import com.monitor.nginxsync.model.CmdTaskInfo;
  7 +import com.monitor.nginxsync.model.LogMsgResp;
7 import com.monitor.nginxsync.model.TaskInfo; 8 import com.monitor.nginxsync.model.TaskInfo;
8 import com.monitor.nginxsync.nio.model.LogMsg; 9 import com.monitor.nginxsync.nio.model.LogMsg;
9 import com.monitor.nginxsync.nio.service.LogService; 10 import com.monitor.nginxsync.nio.service.LogService;
  11 +import org.apache.commons.lang.StringUtils;
10 import org.springframework.beans.factory.annotation.Autowired; 12 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.beans.factory.annotation.Value; 13 import org.springframework.beans.factory.annotation.Value;
12 import org.springframework.web.bind.annotation.RequestBody; 14 import org.springframework.web.bind.annotation.RequestBody;
@@ -97,8 +99,36 @@ public class NginxSyncService { @@ -97,8 +99,36 @@ public class NginxSyncService {
97 99
98 BaseResponse baseResponse = new BaseResponse(); 100 BaseResponse baseResponse = new BaseResponse();
99 101
100 - baseResponse.setData(logService.outLogMsg(taskInfo.getTaskId())); 102 + LogMsg logMsg = logService.outLogMsg(taskInfo.getTaskId());
  103 +
  104 + baseResponse.setData(convertLogMsg(logMsg));
101 105
102 return baseResponse; 106 return baseResponse;
103 } 107 }
  108 +
  109 +
  110 + private LogMsgResp convertLogMsg(LogMsg logMsg) {
  111 + LogMsgResp logMsgResp = new LogMsgResp();
  112 +
  113 + logMsgResp.setIsFinished(logMsg.getIsFinished());
  114 +
  115 + logMsgResp.setTaskId(logMsg.getTaskId());
  116 +
  117 + String str = StringUtils.EMPTY;
  118 +
  119 + if (null != logMsg.getMsgList()) {
  120 + for (String logItem : logMsg.getMsgList()) {
  121 +
  122 + if(StringUtils.isEmpty(logItem))
  123 + {
  124 + continue;
  125 + }
  126 + str += "\r\n" + logItem;
  127 + }
  128 + }
  129 +
  130 + logMsgResp.setLog(str);
  131 +
  132 + return logMsgResp;
  133 + }
104 } 134 }