Search in sources :

Example 1 with FileTransferLogDO

use of com.orion.ops.entity.domain.FileTransferLogDO in project orion-ops by lijiahangmax.

the class PackageFileProcessor method run.

@Override
public void run() {
    // 判断是否可以传输
    FileTransferLogDO fileTransferLog = fileTransferLogDAO.selectById(packageFile.getId());
    if (fileTransferLog == null || !SftpTransferStatus.WAIT.getStatus().equals(fileTransferLog.getTransferStatus())) {
        return;
    }
    transferProcessorManager.addProcessor(fileToken, this);
    try {
        // 通知状态runnable
        this.updateStatus(SftpTransferStatus.RUNNABLE);
        // 初始化压缩器
        this.compressor = CompressTypeEnum.ZIP.compressor().get();
        compressor.setAbsoluteCompressPath(compressPath);
        compressor.compressNotify(this::notifyProgress);
        // 添加压缩文件
        this.initCompressFiles();
        // 添加压缩清单
        this.initCompressFileRaw();
        // 二次检查状态 防止在添加文件过程中取消或者删除
        fileTransferLog = fileTransferLogDAO.selectById(packageFile.getId());
        if (fileTransferLog == null || !SftpTransferStatus.RUNNABLE.getStatus().equals(fileTransferLog.getTransferStatus())) {
            return;
        }
        // 开始压缩
        this.compressor.compress();
        // 传输完成通知
        this.updateStatus(SftpTransferStatus.FINISH);
    } catch (Exception e) {
        log.error("sftp压缩文件-出现异常 fileToken: {}, e: {}, message: {}", fileToken, e.getClass().getName(), e.getMessage());
        // 程序错误并非传输错误修改状态
        if (!userCancel) {
            log.error("sftp传输文件-运行异常 fileToken: {}", fileToken, e);
            this.updateStatus(SftpTransferStatus.ERROR);
        }
        e.printStackTrace();
    } finally {
        this.done = true;
        transferProcessorManager.removeProcessor(fileToken);
    }
}
Also used : FileTransferLogDO(com.orion.ops.entity.domain.FileTransferLogDO)

Example 2 with FileTransferLogDO

use of com.orion.ops.entity.domain.FileTransferLogDO in project orion-ops by lijiahangmax.

the class PackageFileProcessor method initCompressFiles.

/**
 * 初始化压缩文件
 */
private void initCompressFiles() {
    this.nameMapping = Maps.newLinkedMap();
    for (int i = 0; i < fileList.size(); i++) {
        FileTransferLogDO fileLog = fileList.get(i);
        String remoteFile = fileLog.getRemoteFile();
        String localFilePath = Files1.getPath(SystemEnvAttr.SWAP_PATH.getValue(), fileLog.getLocalFile());
        if (!Files1.isFile(new File(localFilePath))) {
            continue;
        }
        // 添加mapping
        String remoteFileName;
        if (nameMapping.containsKey(remoteFile)) {
            remoteFileName = remoteFile + "_" + (i + 1);
        } else {
            remoteFileName = remoteFile;
        }
        nameMapping.put(remoteFileName, fileLog);
        compressor.addFile(remoteFileName, localFilePath);
    }
}
Also used : FileTransferLogDO(com.orion.ops.entity.domain.FileTransferLogDO) File(java.io.File)

Example 3 with FileTransferLogDO

use of com.orion.ops.entity.domain.FileTransferLogDO in project orion-ops by lijiahangmax.

the class PackageFileProcessor method updateStatus.

/**
 * 通知状态
 *
 * @param status status
 */
private void updateStatus(SftpTransferStatus status) {
    FileTransferLogDO update = new FileTransferLogDO();
    update.setId(packageFile.getId());
    update.setTransferStatus(status.getStatus());
    if (SftpTransferStatus.FINISH.equals(status)) {
        // 设置压缩文件实际大小
        File compressFile = new File(compressPath);
        if (Files1.isFile(compressFile)) {
            update.setFileSize(compressFile.length());
            update.setCurrentSize(compressFile.length());
        } else {
            update.setCurrentSize(packageFile.getFileSize());
        }
        update.setNowProgress(100D);
    }
    int effect = fileTransferLogDAO.updateById(update);
    log.info("sftp传输压缩-更新状态 fileToken: {}, status: {}, effect: {}", fileToken, status, effect);
    if (SftpTransferStatus.FINISH.equals(status)) {
        // 通知进度
        FileTransferNotifyDTO.FileTransferNotifyProgress notifyProgress = FileTransferNotifyDTO.progress(Strings.EMPTY, Files1.getSize(totalSize), "100");
        transferProcessorManager.notifySessionProgressEvent(userId, machineId, fileToken, notifyProgress);
    }
    // 通知状态
    transferProcessorManager.notifySessionStatusEvent(userId, machineId, fileToken, status.getStatus());
}
Also used : FileTransferLogDO(com.orion.ops.entity.domain.FileTransferLogDO) FileTransferNotifyDTO(com.orion.ops.entity.dto.FileTransferNotifyDTO) File(java.io.File)

Example 4 with FileTransferLogDO

use of com.orion.ops.entity.domain.FileTransferLogDO in project orion-ops by lijiahangmax.

the class PackageFileProcessor method notifyProgress.

/**
 * 通知进度
 *
 * @param name name
 */
private void notifyProgress(String name) {
    if (done) {
        return;
    }
    FileTransferLogDO compressedFile = nameMapping.get(name);
    if (compressedFile == null) {
        return;
    }
    // 计算进度
    long curr = currentSize.addAndGet(compressedFile.getFileSize());
    double progress = this.getProgress();
    String progressRate = Numbers.setScale(progress, 2);
    // 更新进度
    FileTransferLogDO update = new FileTransferLogDO();
    update.setId(packageFile.getId());
    update.setCurrentSize(curr);
    update.setNowProgress(progress);
    fileTransferLogDAO.updateById(update);
    // 通知进度
    FileTransferNotifyDTO.FileTransferNotifyProgress notifyProgress = FileTransferNotifyDTO.progress(Strings.EMPTY, Files1.getSize(curr), progressRate);
    transferProcessorManager.notifySessionProgressEvent(userId, machineId, fileToken, notifyProgress);
}
Also used : FileTransferLogDO(com.orion.ops.entity.domain.FileTransferLogDO) FileTransferNotifyDTO(com.orion.ops.entity.dto.FileTransferNotifyDTO)

Example 5 with FileTransferLogDO

use of com.orion.ops.entity.domain.FileTransferLogDO in project orion-ops by lijiahangmax.

the class FileTransferProcessor method transferDoneCallback.

/**
 * 传输完成回调
 *
 * @param pro progress
 */
protected void transferDoneCallback(ByteTransferProgress pro) {
    try {
        FileTransferLogDO update = new FileTransferLogDO();
        update.setId(record.getId());
        if (progress.isError()) {
            // 非用户取消更新状态
            if (!userCancel) {
                this.updateStatusAndNotify(SftpTransferStatus.ERROR.getStatus());
            }
        } else {
            String transferCurrent = Files1.getSize(progress.getCurrent());
            String transferRate = Files1.getSize(progress.getNowRate());
            // notify progress
            this.notifyProgress(transferRate, transferCurrent, "100");
            // notify status
            this.updateStatusAndNotify(SftpTransferStatus.FINISH.getStatus(), 100D, progress.getEnd());
        }
    } catch (Exception e) {
        log.error("sftp-传输完成回调异常 fileToken: {}, digest: {}", fileToken, Exceptions.getDigest(e));
        e.printStackTrace();
    }
}
Also used : FileTransferLogDO(com.orion.ops.entity.domain.FileTransferLogDO)

Aggregations

FileTransferLogDO (com.orion.ops.entity.domain.FileTransferLogDO)21 IFileTransferProcessor (com.orion.ops.handler.sftp.IFileTransferProcessor)5 FileTransferNotifyDTO (com.orion.ops.entity.dto.FileTransferNotifyDTO)4 LambdaQueryWrapper (com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper)3 UserDTO (com.orion.ops.entity.dto.UserDTO)3 SftpExecutor (com.orion.remote.channel.sftp.SftpExecutor)2 SftpFile (com.orion.remote.channel.sftp.SftpFile)2 Files1 (com.orion.utils.io.Files1)2 File (java.io.File)2 ObjectIds (com.orion.id.ObjectIds)1 UUIds (com.orion.id.UUIds)1 Const (com.orion.ops.consts.Const)1 KeyConst (com.orion.ops.consts.KeyConst)1 MessageConst (com.orion.ops.consts.MessageConst)1 EventKeys (com.orion.ops.consts.event.EventKeys)1 EventParamsHolder (com.orion.ops.consts.event.EventParamsHolder)1 SftpPackageType (com.orion.ops.consts.sftp.SftpPackageType)1 SftpTransferStatus (com.orion.ops.consts.sftp.SftpTransferStatus)1 SftpTransferType (com.orion.ops.consts.sftp.SftpTransferType)1 SystemEnvAttr (com.orion.ops.consts.system.SystemEnvAttr)1