Search in sources :

Example 1 with FileUtil

use of cn.hutool.core.io.FileUtil in project Jpom by dromara.

the class JpomApplicationEvent method clearOldJar.

private static void clearOldJar() {
    File oldJars = JpomManifest.getOldJarsPath();
    List<File> files = FileUtil.loopFiles(oldJars, 1, file -> StrUtil.endWith(file.getName(), FileUtil.JAR_FILE_EXT, true));
    if (CollUtil.isEmpty(files)) {
        return;
    }
    // 排序
    files.sort((o1, o2) -> FileUtil.lastModifiedTime(o2).compareTo(FileUtil.lastModifiedTime(o1)));
    // 截取
    int size = CollUtil.size(files);
    files = CollUtil.sub(files, ExtConfigBean.getInstance().getOldJarsCount(), size);
    // 删除文件
    files.forEach(FileUtil::del);
}
Also used : File(java.io.File) FileUtil(cn.hutool.core.io.FileUtil) JsonFileUtil(io.jpom.util.JsonFileUtil)

Example 2 with FileUtil

use of cn.hutool.core.io.FileUtil in project Jpom by dromara.

the class AutoBackLog method checkProject.

private static void checkProject(NodeProjectInfoModel nodeProjectInfoModel, NodeProjectInfoModel.JavaCopyItem javaCopyItem) {
    File file = javaCopyItem == null ? new File(nodeProjectInfoModel.getLog()) : nodeProjectInfoModel.getLog(javaCopyItem);
    if (!file.exists()) {
        return;
    }
    long len = file.length();
    if (len > MAX_SIZE.getSize()) {
        try {
            AbstractProjectCommander.getInstance().backLog(nodeProjectInfoModel, javaCopyItem);
        } catch (Exception ignored) {
        }
    }
    // 清理过期的文件
    File logFile = javaCopyItem == null ? nodeProjectInfoModel.getLogBack() : nodeProjectInfoModel.getLogBack(javaCopyItem);
    DateTime nowTime = DateTime.now();
    List<File> files = FileUtil.loopFiles(logFile, pathname -> {
        DateTime dateTime = DateUtil.date(pathname.lastModified());
        long days = DateUtil.betweenDay(dateTime, nowTime, false);
        long saveDays = AgentExtConfigBean.getInstance().getLogSaveDays();
        return days > saveDays;
    });
    files.forEach(FileUtil::del);
}
Also used : File(java.io.File) FileUtil(cn.hutool.core.io.FileUtil) DateTime(cn.hutool.core.date.DateTime)

Example 3 with FileUtil

use of cn.hutool.core.io.FileUtil in project Jpom by dromara.

the class JpomApplicationEvent method onApplicationEvent.

@Override
public void onApplicationEvent(ApplicationEvent event) {
    // 启动最后的预加载
    if (event instanceof ApplicationReadyEvent) {
        // 
        checkPath();
        JpomManifest jpomManifest = JpomManifest.getInstance();
        ConfigBean instance = ConfigBean.getInstance();
        // 清理旧进程新文件
        File dataDir = FileUtil.file(instance.getDataPath());
        List<File> files = FileUtil.loopFiles(dataDir, 1, pathname -> pathname.getName().startsWith("pid."));
        files.forEach(FileUtil::del);
        try {
            this.lockFile(jpomManifest.getPid());
        } catch (IOException e) {
            DefaultSystemLog.getLog().error("lockFile", e);
        }
        // 写入Jpom 信息 、 写入全局信息
        File appJpomFile = instance.getApplicationJpomInfo(JpomApplication.getAppType());
        FileUtil.writeString(jpomManifest.toString(), appJpomFile, CharsetUtil.CHARSET_UTF_8);
        // 检查更新文件
        checkUpdate();
        // 
        if (ApplicationEventLoad.class.isAssignableFrom(JpomApplication.getAppClass())) {
            ApplicationEventLoad eventLoad = (ApplicationEventLoad) SpringUtil.getBean(JpomApplication.getAppClass());
            eventLoad.applicationLoad();
        }
        Console.log("Jpom Successful start preparation. start loading module");
    } else if (event instanceof ContextClosedEvent) {
        // 应用关闭
        this.unLockFile();
        // 
        ConfigBean instance = ConfigBean.getInstance();
        FileUtil.del(instance.getPidFile());
        // 
        File appJpomFile = instance.getApplicationJpomInfo(JpomApplication.getAppType());
        FileUtil.del(appJpomFile);
    }
}
Also used : ApplicationReadyEvent(org.springframework.boot.context.event.ApplicationReadyEvent) ApplicationEventLoad(cn.jiangzeyin.common.spring.event.ApplicationEventLoad) ExtConfigBean(io.jpom.system.ExtConfigBean) ConfigBean(io.jpom.system.ConfigBean) IOException(java.io.IOException) File(java.io.File) FileUtil(cn.hutool.core.io.FileUtil) JsonFileUtil(io.jpom.util.JsonFileUtil) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent)

Aggregations

FileUtil (cn.hutool.core.io.FileUtil)3 File (java.io.File)3 JsonFileUtil (io.jpom.util.JsonFileUtil)2 DateTime (cn.hutool.core.date.DateTime)1 ApplicationEventLoad (cn.jiangzeyin.common.spring.event.ApplicationEventLoad)1 ConfigBean (io.jpom.system.ConfigBean)1 ExtConfigBean (io.jpom.system.ExtConfigBean)1 IOException (java.io.IOException)1 ApplicationReadyEvent (org.springframework.boot.context.event.ApplicationReadyEvent)1 ContextClosedEvent (org.springframework.context.event.ContextClosedEvent)1