12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- package com.jiayue.ipfcst.spare2.job;
- import cn.hutool.core.collection.CollectionUtil;
- import cn.hutool.core.convert.Convert;
- import cn.hutool.core.io.IoUtil;
- import cn.hutool.http.HttpRequest;
- import cn.hutool.http.HttpResponse;
- import cn.hutool.json.JSONObject;
- import cn.hutool.json.JSONUtil;
- import com.jiayue.ipfcst.common.data.dto.ProcessResult;
- import com.jiayue.ipfcst.common.data.util.ShellUtil;
- import com.jiayue.ipfcst.spare2.base4.BaseService;
- import com.jiayue.ipfcst.spare2.common.service.ImageInfoLoadeService;
- import com.jiayue.ipfcst.spare2.util.FilesUtil;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.scheduling.annotation.EnableScheduling;
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Service;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- @Slf4j
- @Service
- @EnableScheduling
- public class ConsoleCheckUpJob extends BaseService {
- @Autowired
- ImageInfoLoadeService imageInfoLoadeService;
- @Scheduled(cron = "0/30 * * * * *")
- public void MonitConsoleUp() {
- ProcessResult processResult = getPid();
- boolean error = false;
- //确认console进程状态
- if (null == processResult.output || processResult.output.isEmpty()) {
- log.error("console程序进程未找到,将使用spare2生成文件");
- error = true;
- }
- log.info("console程序进程查询结果:{}", CollectionUtil.join(processResult.output, " "));
- //console进程存在,访问console健康接口,确认程序状态
- if (!error) {
- // 向console发送健康接口请求
- HttpRequest request = HttpRequest.get("https://localhost:9001/consoleCheckUp/dshealth");
- HttpResponse response = null;
- try {
- log.info("console程序健康状态:发送请求");
- response = request.timeout(5000).execute();
- String body = response.body();
- // code == 0 健康,code == 1 异常
- log.info("console程序健康状态:{}", body);
- if (JSONUtil.isJson(body)) {
- JSONObject jsonObject = JSONUtil.parseObj(body);
- Object code = jsonObject.get("code");
- Integer status = Convert.toInt(code);
- error = status != 0;
- }
- } catch (Exception ex) {
- log.error("console程序健康状态:{}", ex.getMessage());
- } finally {
- IoUtil.closeIfPosible(response);
- }
- }
- //console进程不存在或者console健康接口返回为异常,启动spare2文件生成逻辑
- if (error) {
- log.info("spare2程序生成文件");
- imageInfoLoadeService.imageLoad();
- } else {
- log.info("console程序生成文件");
- }
- }
- private ProcessResult getPid() {
- List<String> cmds = new ArrayList<>();
- Map<String, String> envs = new HashMap<>();
- envs.put("LC_ALL", "zh_CN.UTF-8");
- envs.put("LANG", "zh_CN.UTF-8");
- cmds.add("sh");
- cmds.add("-c");
- cmds.add("pgrep -fl ipfcst-console");
- return ShellUtil.runProcess(cmds, envs, new File(FilesUtil.getBinPath()));
- }
- }
|