ConsoleCheckUpJob.java 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package com.jiayue.ipfcst.spare2.job;
  2. import cn.hutool.core.collection.CollectionUtil;
  3. import cn.hutool.core.convert.Convert;
  4. import cn.hutool.core.io.IoUtil;
  5. import cn.hutool.http.HttpRequest;
  6. import cn.hutool.http.HttpResponse;
  7. import cn.hutool.json.JSONObject;
  8. import cn.hutool.json.JSONUtil;
  9. import com.jiayue.ipfcst.common.data.dto.ProcessResult;
  10. import com.jiayue.ipfcst.common.data.util.ShellUtil;
  11. import com.jiayue.ipfcst.spare2.base4.BaseService;
  12. import com.jiayue.ipfcst.spare2.common.service.ImageInfoLoadeService;
  13. import com.jiayue.ipfcst.spare2.util.FilesUtil;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.scheduling.annotation.EnableScheduling;
  17. import org.springframework.scheduling.annotation.Scheduled;
  18. import org.springframework.stereotype.Service;
  19. import java.io.File;
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. @Slf4j
  25. @Service
  26. @EnableScheduling
  27. public class ConsoleCheckUpJob extends BaseService {
  28. @Autowired
  29. ImageInfoLoadeService imageInfoLoadeService;
  30. @Scheduled(cron = "0/30 * * * * *")
  31. public void MonitConsoleUp() {
  32. ProcessResult processResult = getPid();
  33. boolean error = false;
  34. //确认console进程状态
  35. if (null == processResult.output || processResult.output.isEmpty()) {
  36. log.error("console程序进程未找到,将使用spare2生成文件");
  37. error = true;
  38. }
  39. log.info("console程序进程查询结果:{}", CollectionUtil.join(processResult.output, " "));
  40. //console进程存在,访问console健康接口,确认程序状态
  41. if (!error) {
  42. // 向console发送健康接口请求
  43. HttpRequest request = HttpRequest.get("https://localhost:9001/consoleCheckUp/dshealth");
  44. HttpResponse response = null;
  45. try {
  46. log.info("console程序健康状态:发送请求");
  47. response = request.timeout(5000).execute();
  48. String body = response.body();
  49. // code == 0 健康,code == 1 异常
  50. log.info("console程序健康状态:{}", body);
  51. if (JSONUtil.isJson(body)) {
  52. JSONObject jsonObject = JSONUtil.parseObj(body);
  53. Object code = jsonObject.get("code");
  54. Integer status = Convert.toInt(code);
  55. error = status != 0;
  56. }
  57. } catch (Exception ex) {
  58. log.error("console程序健康状态:{}", ex.getMessage());
  59. } finally {
  60. IoUtil.closeIfPosible(response);
  61. }
  62. }
  63. //console进程不存在或者console健康接口返回为异常,启动spare2文件生成逻辑
  64. if (error) {
  65. log.info("spare2程序生成文件");
  66. imageInfoLoadeService.imageLoad();
  67. } else {
  68. log.info("console程序生成文件");
  69. }
  70. }
  71. private ProcessResult getPid() {
  72. List<String> cmds = new ArrayList<>();
  73. Map<String, String> envs = new HashMap<>();
  74. envs.put("LC_ALL", "zh_CN.UTF-8");
  75. envs.put("LANG", "zh_CN.UTF-8");
  76. cmds.add("sh");
  77. cmds.add("-c");
  78. cmds.add("pgrep -fl ipfcst-console");
  79. return ShellUtil.runProcess(cmds, envs, new File(FilesUtil.getBinPath()));
  80. }
  81. }