Search in sources :

Example 1 with ServerCollectLog

use of com.ibeiliao.deployment.common.vo.ServerCollectLog in project Corgi by kevinYin.

the class LogSyncService method syncShellLog.

/**
 * 同步shell脚本日志记录, 没有数据时, 休眠一段时间
 */
private void syncShellLog() throws Exception {
    List<ServerCollectLog> logList = new ArrayList<>();
    int syncSize = 100;
    int count = 0;
    // 避免多线程获取同一服务器同一次发布日志的错乱顺序问题, 取出记录时加锁
    try {
        boolean obtainLock = false;
        while (!obtainLock) {
            obtainLock = popLogLock.lock();
            if (obtainLock) {
                while (count < syncSize) {
                    String str = redis.lpop(RedisLogKey.getServerDeploymentKey());
                    if (StringUtils.isNotEmpty(str)) {
                        logger.info("获取redis 日志内容:" + str);
                        ServerCollectLog log = JSONObject.parseObject(str, ServerCollectLog.class);
                        // 处理shell 日志,
                        if (log.getLogType() == LogType.SERVER_SHELL_LOG.getType()) {
                            logList.add(log);
                        } else if (log.getLogType() == LogType.SERVER_DEPLOY_LOG.getType()) {
                            logList.addAll(buildServerDeployLogs(log));
                        } else // 处理编译日志, 重新分类放进redis 里面, 并设置保存时间
                        if (log.getLogType() == LogType.COMPILE_LOG.getType()) {
                            String moduleCompileKey = RedisLogKey.getModuleCompileKey(log.getId());
                            redis.rpush(moduleCompileKey, log.getContent());
                            redis.expire(moduleCompileKey, COMPILE_LOG_EXISTS_TIME);
                        }
                    } else {
                        break;
                    }
                    count++;
                }
            } else {
                // 休眠
                Thread.sleep(100);
            }
        }
    } finally {
        popLogLock.unlock();
    }
    // 获取到数据后调用同步接口
    if (CollectionUtils.isNotEmpty(logList)) {
        logger.info("发送同步内容...");
        Map<String, String> params = new HashMap<>();
        params.put("data", JSONObject.toJSONString(logList));
        // 成功失败均不重试
        HttpUtils.post(LogSyncConfig.getInstance().getSyncShellLogUrl(), params);
    // 没有数据时休息一段时间等待下次调用
    } else {
        Thread.sleep(LogSyncConfig.getInstance().getMaxSyncInteval());
    }
}
Also used : ServerCollectLog(com.ibeiliao.deployment.common.vo.ServerCollectLog) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Example 2 with ServerCollectLog

use of com.ibeiliao.deployment.common.vo.ServerCollectLog in project Corgi by kevinYin.

the class LogSyncService method buildServerDeployLogs.

private List<ServerCollectLog> buildServerDeployLogs(ServerCollectLog log) {
    List<ServerCollectLog> allDeployLogs = Lists.newArrayList();
    String content = log.getContent();
    String[] allLogs = content.split("\n");
    for (String deployLog : allLogs) {
        if (StringUtils.isBlank(deployLog)) {
            continue;
        }
        String[] split = deployLog.split(" ");
        String ip = split[0];
        String logContent = deployLog.substring(ip.length(), deployLog.length());
        ServerCollectLog serverDeployLog = new ServerCollectLog(log.getId(), log.getLogType(), logContent);
        serverDeployLog.setServerIp(ip);
        allDeployLogs.add(serverDeployLog);
    }
    return allDeployLogs;
}
Also used : ServerCollectLog(com.ibeiliao.deployment.common.vo.ServerCollectLog)

Example 3 with ServerCollectLog

use of com.ibeiliao.deployment.common.vo.ServerCollectLog in project Corgi by kevinYin.

the class DeployLogServiceImpl method rebuildServerDeployId.

private void rebuildServerDeployId(List<ServerCollectLog> logs) {
    List<String> ips = Lists.newArrayList();
    for (ServerCollectLog log : logs) {
        if (StringUtils.isNotBlank(log.getServerIp())) {
            ips.add(log.getServerIp().trim());
        }
    }
    List<Integer> historyIds = Lists.newArrayList();
    for (ServerCollectLog log : logs) {
        if (log.getId() >= 0) {
            historyIds.add(log.getId());
        }
    }
    if (CollectionUtils.isEmpty(ips) || CollectionUtils.isEmpty(historyIds)) {
        return;
    }
    List<ServerDeployHistoryPO> deployHistoryPOs = serverDeployHistoryDao.getByHistoryIdsAndIps(historyIds, ips);
    if (CollectionUtils.isEmpty(deployHistoryPOs)) {
        return;
    }
    HashMap<String, Integer> historyIdIp2ServerDeployIdMap = Maps.newHashMap();
    for (ServerDeployHistoryPO po : deployHistoryPOs) {
        historyIdIp2ServerDeployIdMap.put(po.getHistoryId() + "_" + po.getServerIp(), po.getId());
    }
    for (ServerCollectLog log : logs) {
        Integer serverDeployId = historyIdIp2ServerDeployIdMap.get(log.getId() + "_" + log.getServerIp());
        if (serverDeployId != null) {
            log.setId(serverDeployId);
        }
    }
}
Also used : ServerCollectLog(com.ibeiliao.deployment.common.vo.ServerCollectLog) ServerDeployHistoryPO(com.ibeiliao.deployment.admin.po.deploy.ServerDeployHistoryPO)

Example 4 with ServerCollectLog

use of com.ibeiliao.deployment.common.vo.ServerCollectLog in project Corgi by kevinYin.

the class DeployLogServiceImpl method batchSaveServerDeployLog.

@Override
public void batchSaveServerDeployLog(List<ServerCollectLog> logs) {
    if (CollectionUtils.isEmpty(logs)) {
        return;
    }
    rebuildServerDeployId(logs);
    List<ServerDeployLogPO> poList = new ArrayList<>();
    Date now = new Date();
    for (ServerCollectLog log : logs) {
        ServerDeployLogPO po = new ServerDeployLogPO();
        po.setServerDeployId(log.getId());
        po.setShellLog(log.getContent());
        po.setCreateTime(now);
        poList.add(po);
    }
    int rows = serverDeployLogDao.batchInsert(poList);
    logger.info("成功保存{}条shell日志记录", rows);
}
Also used : ServerCollectLog(com.ibeiliao.deployment.common.vo.ServerCollectLog) ServerDeployLogPO(com.ibeiliao.deployment.admin.po.deploy.ServerDeployLogPO)

Example 5 with ServerCollectLog

use of com.ibeiliao.deployment.common.vo.ServerCollectLog in project Corgi by kevinYin.

the class SubscribeLogThread method run.

@Override
public void run() {
    jedisPubSub = new JedisPubSub() {

        @Override
        public void onMessage(String channel, String message) {
            logger.info("接收到通道:{} 发布的信息, message: {}", channel, message);
            try {
                // 向用户推送监控的发布服务器信息
                List<ServerCollectLog> logList = JSONArray.parseArray(message, ServerCollectLog.class);
                // 发布服务器对应的模块id  map
                Map<Integer, Integer> serverDeployId2ModuleIdMap = new HashMap<>();
                for (ServerCollectLog log : logList) {
                    // Set<WebSocketSession> sessions = serverDeployRelates.get(log.getId());
                    Set<WebSocketSession> sessions = LogIdToSessionHolder.getInstance().get(log.getId());
                    if (CollectionUtils.isNotEmpty(sessions)) {
                        // 推送信息
                        ShellLogResponseMessage responseMessage = new ShellLogResponseMessage();
                        // responseMessage.setStepLogs(readStepLogs(serverDeployId2ModuleIdMap, log));
                        ShellLogResponseMessage.ServerShellLog shellLog = new ShellLogResponseMessage.ServerShellLog();
                        shellLog.setServerDeployId(log.getId());
                        shellLog.setLog(log.getContent());
                        List<ShellLogResponseMessage.ServerShellLog> shellLogList = new ArrayList<>();
                        shellLogList.add(shellLog);
                        responseMessage.setServerLogs(shellLogList);
                        responseMessage.setCode(ApiCode.SUCCESS);
                        responseMessage.setType(WebSocketRequestType.DEPLOY_SHELL_LOG.getName());
                        String messageStr = JSONObject.toJSONString(responseMessage);
                        // 向监听用户发送消息
                        for (WebSocketSession session : sessions) {
                            if (session.isOpen()) {
                                try {
                                    session.sendMessage(new TextMessage(messageStr));
                                } catch (Exception e) {
                                    logger.error("发送推送日志消息失败 | msg:{}", e.getMessage(), e);
                                }
                            }
                        }
                    }
                }
            // end for
            } catch (Exception e) {
                logger.error("处理订阅的日志更改信息失败 | msg:{}", e.getMessage(), e);
            }
        }
    };
    logger.info("订阅开始……");
    redis.subscribe(jedisPubSub, RedisKey.getDeploySubscribeChannelKey());
    logger.info("订阅结束 ...");
}
Also used : ServerCollectLog(com.ibeiliao.deployment.common.vo.ServerCollectLog) JedisPubSub(redis.clients.jedis.JedisPubSub) ShellLogResponseMessage(com.ibeiliao.deployment.admin.websocket.request.ShellLogResponseMessage) WebSocketSession(org.springframework.web.socket.WebSocketSession) TextMessage(org.springframework.web.socket.TextMessage)

Aggregations

ServerCollectLog (com.ibeiliao.deployment.common.vo.ServerCollectLog)6 ServerDeployHistoryPO (com.ibeiliao.deployment.admin.po.deploy.ServerDeployHistoryPO)1 ServerDeployLogPO (com.ibeiliao.deployment.admin.po.deploy.ServerDeployLogPO)1 ShellLogResponseMessage (com.ibeiliao.deployment.admin.websocket.request.ShellLogResponseMessage)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 TextMessage (org.springframework.web.socket.TextMessage)1 WebSocketSession (org.springframework.web.socket.WebSocketSession)1 JedisPubSub (redis.clients.jedis.JedisPubSub)1