Search in sources :

Example 1 with FileTailDTO

use of com.orion.ops.entity.dto.FileTailDTO in project orion-ops by lijiahangmax.

the class FileTailServiceImpl method getTailToken.

@Override
public FileTailVO getTailToken(FileTailType type, Long relId) {
    FileTailVO res;
    // 获取日志路径
    final boolean isLocal = type.isLocal();
    if (isLocal) {
        String path = this.getTailFilePath(type, relId);
        res = new FileTailVO();
        res.setPath(path);
        res.setOffset(machineEnvService.getTailOffset(Const.HOST_MACHINE_ID));
        res.setCharset(machineEnvService.getTailCharset(Const.HOST_MACHINE_ID));
        res.setCommand(machineEnvService.getTailDefaultCommand(Const.HOST_MACHINE_ID));
    } else {
        // tail list
        FileTailListDO fileTail = fileTailListDAO.selectById(relId);
        Valid.notNull(fileTail, MessageConst.UNKNOWN_DATA);
        res = Converts.to(fileTail, FileTailVO.class);
    }
    // 设置命令
    this.replaceTailCommand(res);
    // 设置机器信息
    MachineInfoDO machine = machineInfoService.selectById(isLocal ? Const.HOST_MACHINE_ID : res.getMachineId());
    Valid.notNull(machine, MessageConst.INVALID_MACHINE);
    res.setMachineId(machine.getId());
    res.setMachineName(machine.getMachineName());
    res.setMachineHost(machine.getMachineHost());
    // 设置token
    String token = UUIds.random19();
    res.setToken(token);
    // 设置缓存
    FileTailDTO tail = Converts.to(res, FileTailDTO.class);
    tail.setUserId(Currents.getUserId());
    // 追踪模式
    String tailMode = isLocal ? systemEnvService.getTailMode() : res.getTailMode();
    tail.setMode(tailMode);
    String key = Strings.format(KeyConst.FILE_TAIL_ACCESS_TOKEN, token);
    redisTemplate.opsForValue().set(key, JSON.toJSONString(tail), KeyConst.FILE_TAIL_ACCESS_EXPIRE, TimeUnit.SECONDS);
    // 非列表不返回命令和路径
    if (isLocal) {
        res.setPath(null);
        res.setCommand(null);
    }
    return res;
}
Also used : FileTailListDO(com.orion.ops.entity.domain.FileTailListDO) MachineInfoDO(com.orion.ops.entity.domain.MachineInfoDO) FileTailDTO(com.orion.ops.entity.dto.FileTailDTO) FileTailVO(com.orion.ops.entity.vo.FileTailVO)

Example 2 with FileTailDTO

use of com.orion.ops.entity.dto.FileTailDTO in project orion-ops by lijiahangmax.

the class TailFileHandler method afterConnectionEstablished.

@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
    String id = session.getId();
    String token = getToken(session);
    // 检查token
    String tokenKey = Strings.format(KeyConst.FILE_TAIL_ACCESS_TOKEN, token);
    String tokenValue = redisTemplate.opsForValue().get(tokenKey);
    FileTailDTO fileTail = JSON.parseObject(tokenValue, FileTailDTO.class);
    // 检查bind
    String bindKey = Strings.format(KeyConst.FILE_TAIL_BIND_TOKEN, token);
    String bindValue = redisTemplate.opsForValue().get(bindKey);
    if (bindValue != null) {
        session.close(WsCloseCode.TOKEN_BIND.close());
        return;
    }
    // 删除token
    redisTemplate.delete(tokenKey);
    // token绑定
    redisTemplate.opsForValue().set(bindKey, id, KeyConst.FILE_TAIL_BIND_EXPIRE, TimeUnit.SECONDS);
    log.info("tail 建立ws连接 token: {}, id: {}, fileTail: {}", token, id, JSON.toJSONString(fileTail));
    // 打开日志流
    try {
        this.openTailHandler(session, token, Objects.requireNonNull(fileTail));
    } catch (Exception e) {
        e.printStackTrace();
        log.error("tail 打开处理器-失败 e: {}, token: {}, id: {}", e, token, id);
    }
}
Also used : FileTailDTO(com.orion.ops.entity.dto.FileTailDTO)

Aggregations

FileTailDTO (com.orion.ops.entity.dto.FileTailDTO)2 FileTailListDO (com.orion.ops.entity.domain.FileTailListDO)1 MachineInfoDO (com.orion.ops.entity.domain.MachineInfoDO)1 FileTailVO (com.orion.ops.entity.vo.FileTailVO)1