Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Tencent/bk-job into 3.9.x…
Browse files Browse the repository at this point in the history
…_dev
  • Loading branch information
wangyu096 committed May 14, 2024
2 parents 8845847 + 99d15db commit c542415
Show file tree
Hide file tree
Showing 14 changed files with 416 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -433,12 +433,12 @@ public NodeDTO queryNodeDetail(String projectId, String repoName, String fullPat
return resp.getData();
}

public Boolean deleteProject(String projectId) {
public boolean deleteProject(String projectId) {
log.info("deleteProject:{}", projectId);
throw new NotImplementedException("Not support feature", ErrorCode.NOT_SUPPORT_FEATURE);
}

public Boolean deleteRepo(String projectId, String repoName, Boolean forced) {
public boolean deleteRepo(String projectId, String repoName, Boolean forced) {
DeleteRepoReq req = new DeleteRepoReq();
req.setProjectId(projectId);
req.setRepoName(repoName);
Expand All @@ -449,7 +449,7 @@ public Boolean deleteRepo(String projectId, String repoName, Boolean forced) {
return resp.getCode() == 0;
}

public Boolean deleteNode(String projectId, String repoName, String fullPath) {
public boolean deleteNode(String projectId, String repoName, String fullPath) {
DeleteNodeReq req = new DeleteNodeReq();
req.setProjectId(projectId);
req.setRepoName(repoName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@


import com.tencent.bk.job.common.util.ThreadUtils;
import io.lettuce.core.RedisCommandInterruptedException;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.data.redis.RedisSystemException;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -70,7 +73,16 @@ public void run() {
ThreadUtils.sleep(periodMillis, false);
}
} catch (Throwable t) {
log.error("RedisKeyHeartBeatThread {} quit unexpectedly:", this.getName(), t);
String msg = MessageFormatter.format(
"RedisKeyHeartBeatThread {} quit unexpectedly:",
this.getName()
).getMessage();
// 主动终止线程产生的异常只打印调试级别日志
if (!causeByStopAtOnceInterrupt(t)) {
log.error(msg, t);
} else {
log.debug(msg, t);
}
} finally {
deleteRedisKeySafely();
}
Expand All @@ -81,7 +93,24 @@ private void deleteRedisKeySafely() {
Boolean result = redisTemplate.delete(redisKey);
log.debug("delete redis key:{}, result={}", redisKey, result);
} catch (Throwable e) {
log.error("Delete redis key fail", e);
// 主动终止线程产生的异常只打印调试级别日志
if (!causeByStopAtOnceInterrupt(e)) {
log.error("Delete redis key fail", e);
} else {
log.debug("Delete redis key fail", e);
}
}
}

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
private boolean causeByStopAtOnceInterrupt(Throwable t) {
if (runFlag) {
return false;
}
if (t instanceof RedisSystemException) {
Throwable cause = t.getCause();
return cause instanceof RedisCommandInterruptedException;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package com.tencent.bk.job.common.util.date;

import com.tencent.bk.job.common.util.json.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.helpers.MessageFormatter;
Expand All @@ -41,8 +42,13 @@
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Formatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 时间处理
Expand Down Expand Up @@ -215,6 +221,45 @@ public static LocalDateTime convertFromMillSeconds(long millSeconds) {
ZoneOffset.systemDefault().getRules().getOffset(Instant.now()));
}

/**
* 尝试使用多种格式来解析字符串中的时间,只要有任意一种格式匹配即可成功解析
*
* @param dateTime 日期时间字符串
* @param patterns 多个可能的日期时间格式
* @return 本地日期时间对象
*/
public static LocalDateTime convertFromStringDateByPatterns(String dateTime, String... patterns) {
if (patterns.length == 0) {
throw new IllegalArgumentException("patterns must not be empty");
}
LocalDateTime localDateTime;
Map<String, Exception> exceptionMap = new HashMap<>();
for (String pattern : patterns) {
try {
localDateTime = convertFromStringDate(dateTime, pattern);
if (localDateTime != null) {
return localDateTime;
}
} catch (Exception e) {
exceptionMap.put(pattern, e);
}
}
String patternsInvalidMsg = MessageFormatter.format(
"Fail to convertFromStringDateByPatterns: dateTime={}, patterns={}, exceptions: ",
dateTime,
patterns
).getMessage();
log.warn(patternsInvalidMsg);
exceptionMap.forEach((pattern, e) -> {
String patternInvalidMsg = MessageFormatter.format(
"pattern: {}, exception: ",
pattern
).getMessage();
log.warn(patternInvalidMsg, e);
});
throw new IllegalArgumentException(patternsInvalidMsg);
}

public static LocalDateTime convertFromStringDate(String dateTime, String pattern) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
return LocalDateTime.parse(dateTime, formatter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.time.ZonedDateTime;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class DateUtilsTest {

Expand Down Expand Up @@ -74,4 +75,41 @@ void getUTCDayEndTimestamp() {
// 2023-07-14 00:00:00 UTC
assertThat(endTime4).isEqualTo(1689292800000L);
}

@Test
void convertFromStringDateByPatterns() {
String[] patterns = new String[]{
"yyyy-MM-dd'T'HH:mm:ss.SSS",
"yyyy-MM-dd'T'HH:mm:ss.SS",
"yyyy-MM-dd'T'HH:mm:ss.S",
"yyyy-MM-dd'T'HH:mm:ss.",
"yyyy-MM-dd'T'HH:mm:ss"
};
LocalDateTime localDateTime = DateUtils.convertFromStringDateByPatterns(
"2022-04-21T10:55:54.655", patterns
);
assertThat(localDateTime).isNotNull();
localDateTime = DateUtils.convertFromStringDateByPatterns(
"2022-04-21T10:55:54.65", patterns
);
assertThat(localDateTime).isNotNull();
localDateTime = DateUtils.convertFromStringDateByPatterns(
"2022-04-21T10:55:54.6", patterns
);
assertThat(localDateTime).isNotNull();
localDateTime = DateUtils.convertFromStringDateByPatterns(
"2022-04-21T10:55:54.", patterns
);
assertThat(localDateTime).isNotNull();
localDateTime = DateUtils.convertFromStringDateByPatterns(
"2022-04-21T10:55:54", patterns
);
assertThat(localDateTime).isNotNull();
assertThatThrownBy(() -> DateUtils.convertFromStringDateByPatterns(
"2022-04-21T10:55:54.65"
)).isInstanceOf(IllegalArgumentException.class);
assertThatThrownBy(() -> DateUtils.convertFromStringDateByPatterns(
"2022-04-21 10:55:54.65"
)).isInstanceOf(IllegalArgumentException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class FileUploadSettingReq {
private Integer restrictMode;

@ApiModelProperty("后缀列表")
private List<@Pattern(regexp = "^\\.[A-Za-z0-9_-]{1,24}$",
private List<@Pattern(regexp = "^(\\.[A-Za-z0-9_-]{1,24})+$",
message = "{validation.constraints.InvalidUploadFileSuffix.message}") String> suffixList;

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public class LocalFileConfigForManage {
@Value("${local-file.artifactory.repo:localupload}")
private String localUploadRepo;

@Value("${local-file.expire-delete:true}")
private boolean expireDelete = true;

@Value("${local-file.expire-days:7}")
private Integer expireDays;

Expand Down
Loading

0 comments on commit c542415

Please sign in to comment.