Skip to content

Commit

Permalink
异常输出,nginx执行警告等小细节
Browse files Browse the repository at this point in the history
  • Loading branch information
codingmiao committed Jul 18, 2018
1 parent 292cd02 commit ca20403
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

/**
* 运行记录,存放每次心跳的运行信息
*
Expand Down Expand Up @@ -45,6 +50,8 @@ public class Record {
*/
private State state;

private List<String> msgs;

/**
* 异常类型
*/
Expand Down Expand Up @@ -74,8 +81,14 @@ public JSONObject toJson() {
if (null != state) {
jo.put("exceptionType", state.type);
}
if(null!=exception){
jo.put("exception", exception.getMessage());
if (null != exception) {
StringWriter stringWriter = new StringWriter();
exception.printStackTrace(new PrintWriter(stringWriter));
String strException = stringWriter.toString();
jo.put("exception", strException);
}
if (null != msgs && msgs.size() > 0) {
jo.put("msgs", msgs);
}

jo.put("startTimestamp", startTimestamp);
Expand All @@ -86,6 +99,13 @@ public JSONObject toJson() {
return jo;
}

public void addMsg(String msg) {
if (null == msgs) {
msgs = new ArrayList<>(1);
}
msgs.add(msg);
}

public void setException(State exceptionType, Exception exception) {
this.exception = exception;
this.state = exceptionType;
Expand Down Expand Up @@ -146,4 +166,6 @@ public State getState() {
public void setState(State state) {
this.state = state;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

/**
Expand All @@ -32,7 +31,9 @@ public class NginxService {
locationParams = new HashMap<>(0);
} else {
try {
String[] strs = ResourcesReader.readStr(Constant.rootPath + "locationParam.txt").split("\n");
String locationParam = ResourcesReader.readStr(path);
System.out.println("locationParam:\n" + locationParam);
String[] strs = locationParam.split("\n");
HashMap<String, StringBuilder> kv = new HashMap<>();
StringBuilder param = null;
for (String str : strs) {
Expand Down Expand Up @@ -120,7 +121,10 @@ private static void write(String cfg, String name) throws Exception {
private static void reloadCfg(Record record) {
String fileName = "/".equals(File.separator) ? "reload.sh" : "reload.bat";
try {
exeCmd(Constant.rootPath + fileName);
String res = exeCmd(Constant.rootPath + fileName);
if (null != res && res.length() > 0) {
record.addMsg("nginx reload:" + res);
}
} catch (Exception e) {
record.setException(Record.State.ErrOnReloadNginx, e);
throw new RuntimeException(e);
Expand All @@ -141,10 +145,10 @@ private static String exeCmd(String commandStr) {
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
sb.append(line).append("\n");
}

return sb.toString();
return sb.toString().trim();
} catch (Exception e) {
throw new RuntimeException("执行控制台命令异常:" + commandStr, e);
} finally {
Expand Down

0 comments on commit ca20403

Please sign in to comment.