Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: Springboot内置tomcat临时目录优化 #267 #1745

Open
wants to merge 1 commit into
base: 3.5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-JOB蓝鲸智云作业平台 is licensed under the MIT License.
*
* License for BK-JOB蓝鲸智云作业平台:
* --------------------------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
* to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
* THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

package com.tencent.bk.job.common.web.task;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;

/**
* 清理内置tomcat文件
*/
@Slf4j
@EnableScheduling
@Component
@ConditionalOnProperty(value = "tomcat.file.clear-enabled", havingValue = "true")
public class ClearTomcatFileTasks {

@Value("${server.port}")
private int port;

@Value("${server.tomcat.basedir:}")
private String baseDir;

@Value("${tomcat.file.expired.keep-days:10}")
private int days;

/**
* 凌晨两点清理tomcat长时间不使用的临时文件:
* 如果自定义目录,需包含端口信息,如:/tmp/tomcat-job-${spring.application.name}-${server.port}
* 未配置临时目录,则清理默认路径/tmp/tomcat.port.***
*/
@Scheduled(cron = "${tomcat.file.expired.clear-cron:0 0 2 * * ?}")
public void clearTomcatTmpExpiredFile() {
log.info(Thread.currentThread().getId() + ":clearTomcatTmpExpiredFile start");
try {
String path = baseDir;
if (StringUtils.isEmpty(path)) {
path = System.getProperty("catalina.home");
}
if (StringUtils.isNotEmpty(path)) {
String[] dirs = path.split(File.pathSeparator);
if (dirs[dirs.length - 1].contains(port + "")) {
deleteExpiredFile(new File(path));
}
}
log.info(Thread.currentThread().getId() + ":clearTomcatTmpExpiredFile end");
} catch (Exception e) {
log.error("clearTomcatExpiredFile fail", e);
}
}

private void deleteExpiredFile(File file) {
File[] files = file.listFiles();
if (files != null) {
for (int i = 0; i < files.length; i++) {
if (files[i].isFile() && isExpired(files[i].lastModified())) {
files[i].delete();
} else if (files[i].isDirectory()) {
deleteExpiredFile(files[i]);
}
}
}
}

private boolean isExpired(long lastTime) {
long currTime = System.currentTimeMillis();
long diff = currTime - lastTime;
long thDay = days * 24 * 60 * 60 * 1000;
if (diff > thDay) {
return true;
}
return false;
}
}
14 changes: 14 additions & 0 deletions support-files/templates/#etc#job#job-common#application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,17 @@ cmdb:
swagger:
url: swagger.job.com

# 内置tomcat文件清理
# server:
# tomcat:
# 临时文件保存路径,子目录名需包含端口
# basedir: /tmp/tomcat-job-${spring.application.name}-${server.port}
# tomcat:
# file:
# 清理开关
# clear-enabled: false
# expired:
# 保留天数
# keep-days: 10
# 清理策略
# clear-cron: 0 0 2 * * ?