package com.ningdatech.carapi.log.task;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.ningdatech.carapi.log.constant.LogBackupConstant;
import com.ningdatech.carapi.log.manage.LogManage;

import cn.hutool.core.date.StopWatch;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 事故责任认定书信息
 * 
 * @author CMM
 * @description
 * @since 2022/12/02 16:28
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class LogBackupTask {
    @Value("${task.switch.is-open}")
    private boolean flag;

    private final LogManage logManage;


    /**
     * 定时备份服务器系统日志 每天23点的55分钟开始执行一次
     *
     */
    @Scheduled(cron = "0 55 23 * * ?")
    public void doTask() throws UnknownHostException {
        if (flag && LogBackupConstant.HOST.equals(InetAddress.getLocalHost().getHostName())) {
            log.info("========== 更新服务器备份日志 ==========任务开始");
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            String result = logManage.backupLog();
            log.info(result);
            log.info("本机IP为:" + InetAddress.getLocalHost().getHostAddress());
            stopWatch.stop();
            log.info("========== 更新服务器备份日志 ==========任务结束 耗时 {}s", stopWatch.getTotalTimeSeconds());
        }
       
    }
}
