Skip to content

Commit

Permalink
Merge pull request #2059 from jsonwan/github_master
Browse files Browse the repository at this point in the history
perf: 定时任务启动失败打印异常堆栈信息 #2028
  • Loading branch information
jsonwan authored May 15, 2023
2 parents 57f8df0 + c635b72 commit e25624d
Show file tree
Hide file tree
Showing 41 changed files with 442 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.message.BasicHeader;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.web.util.UriUtils;

import java.io.File;
Expand Down Expand Up @@ -286,7 +287,15 @@ private <R> R getArtifactoryRespByReq(
status = "ok";
return result;
} catch (Exception e) {
log.error("Fail to request ARTIFACTORY data|method={}|url={}|reqStr={}", method, url, reqStr, e);
String msg = MessageFormatter.arrayFormat(
"Fail to request ARTIFACTORY data|method={}|url={}|reqStr={}",
new String[]{
method,
url,
reqStr
}
).getMessage();
log.error(msg, e);
status = "error";
throw new InternalException("Fail to request ARTIFACTORY data", ErrorCode.ARTIFACTORY_API_DATA_ERROR);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.tencent.bk.job.common.util.FlowController;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;

Expand Down Expand Up @@ -195,7 +196,14 @@ public int updateConfig(Map<String, Long> configMap) {
redisTemplate.opsForValue().set(redisKey, redisValue);
count += 1;
} catch (Exception e) {
log.error("Fail to write redis:key={},value={}", redisKey, redisValue, e);
String msg = MessageFormatter.arrayFormat(
"Fail to write redis:key={},value={}",
new String[]{
redisKey,
redisValue
}
).getMessage();
log.error(msg, e);
}
}
return count;
Expand All @@ -209,7 +217,14 @@ public Long getCurrentConfig(String resourceId) {
try {
return Long.parseLong(stringValue);
} catch (Exception e) {
log.warn("Fail to parse maxRate, resourceId={},stringValue={}", resourceId, stringValue, e);
String msg = MessageFormatter.arrayFormat(
"Fail to parse maxRate, resourceId={},stringValue={}",
new String[]{
resourceId,
stringValue
}
).getMessage();
log.error(msg, e);
return null;
}
}
Expand All @@ -227,7 +242,11 @@ public Map<String, Long> getCurrentConfig() {
map.put(maxRateKey.replace(REDIS_PREFIX_MAX_RATE, ""), Long.parseLong(value));
}
} catch (Exception e) {
log.error("Fail to get redis value:key={}", maxRateKey, e);
String msg = MessageFormatter.format(
"Fail to get redis value:key={}",
maxRateKey
).getMessage();
log.error(msg, e);
}
}
return map;
Expand All @@ -252,7 +271,11 @@ public Map<String, Long> getCurrentRateMap() {
try {
map.put(resourceId, getCurrentRate(resourceId));
} catch (Exception e) {
log.error("Fail to get current rate:resourceId={}", resourceId, e);
String msg = MessageFormatter.format(
"Fail to get current rate:resourceId={}",
resourceId
).getMessage();
log.error(msg, e);
}
}
return map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.helpers.MessageFormatter;

import java.io.File;
import java.io.FileInputStream;
Expand Down Expand Up @@ -165,10 +166,18 @@ public static String writeInsToFile(InputStream ins, String targetPath) throws I
fis = new FileInputStream(targetPath);
return DigestUtils.md5Hex(fis);
} catch (FileNotFoundException e) {
log.error("File not found:{}", targetPath, e);
String msg = MessageFormatter.format(
"File not found:{}",
targetPath
).getMessage();
log.error(msg, e);
throw new RuntimeException(e);
} catch (IOException e) {
log.error("IOException occurred:{}", targetPath, e);
String msg = MessageFormatter.format(
"IOException occurred:{}",
targetPath
).getMessage();
log.error(msg, e);
throw new RuntimeException(e);
} catch (InterruptedException e) {
log.info("Download interrupted, targetPath:{}", targetPath);
Expand Down Expand Up @@ -236,10 +245,18 @@ public static String writeInsToFile(InputStream ins, String targetPath, Long fil
fis = new FileInputStream(targetPath);
return DigestUtils.md5Hex(fis);
} catch (FileNotFoundException e) {
log.error("File not found:{}", targetPath, e);
String msg = MessageFormatter.format(
"File not found:{}",
targetPath
).getMessage();
log.error(msg, e);
throw new RuntimeException(e);
} catch (IOException e) {
log.error("IOException occurred:{}", targetPath, e);
String msg = MessageFormatter.format(
"IOException occurred:{}",
targetPath
).getMessage();
log.error(msg, e);
throw new RuntimeException(e);
} catch (InterruptedException e) {
log.info("Download interrupted, targetPath:{}", targetPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.helpers.MessageFormatter;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
Expand Down Expand Up @@ -301,7 +307,15 @@ public static String getPreviousDateStr(String dateStr, String pattern, int prev
Date previousDay = getPreviousDate(date, previousDays);
return sdf.format(previousDay);
} catch (ParseException e) {
log.error("Fail to calc date:{} - {}days, pattern={}", dateStr, previousDays, pattern, e);
String msg = MessageFormatter.arrayFormat(
"Fail to calc date:{} - {}days, pattern={}",
new String[]{
dateStr,
String.valueOf(previousDays),
pattern
}
).getMessage();
log.error(msg, e);
return null;
}
}
Expand All @@ -327,7 +341,15 @@ public static Long calcDaysBetween(String startDateStr, String endDateStr, Strin
Date endDate = sdf.parse(endDateStr);
return (endDate.getTime() - startDate.getTime()) / (24 * 60 * 60 * 1000);
} catch (ParseException e) {
log.error("Fail to calcDaysBetween:{},{},pattern={}", startDateStr, endDateStr, pattern, e);
String msg = MessageFormatter.arrayFormat(
"Fail to calcDaysBetween:{},{},pattern={}",
new String[]{
startDateStr,
endDateStr,
pattern
}
).getMessage();
log.error(msg, e);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.Tracer;
Expand Down Expand Up @@ -300,7 +301,11 @@ private Map<String, String> parseMultiValueFromQueryStringOrBody(HttpServletRequ
}
return params;
} catch (Exception e) {
log.warn("Fail to parse keys: {} from request", keys, e);
String msg = MessageFormatter.format(
"Fail to parse keys: {} from request",
keys
).getMessage();
log.warn(msg, e);
}
return params;
}
Expand Down Expand Up @@ -330,7 +335,11 @@ private String parseValueFromQueryStringOrBody(HttpServletRequest request, Strin
return value;
}
} catch (Exception e) {
log.warn("Fail to parse {} from request", key, e);
String msg = MessageFormatter.format(
"Fail to parse {} from request",
key
).getMessage();
log.warn(msg, e);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.helpers.MessageFormatter;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -85,7 +86,11 @@ public static List<Long> decodeDbTag(String tags) {
} catch (NumberFormatException e) {
log.warn("Tag must be number {}!", tagString);
} catch (Exception e) {
log.error("Exception while processing tag {}!", tagString, e);
String msg = MessageFormatter.format(
"Exception while processing tag {}!",
tagString
).getMessage();
log.error(msg, e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
Expand Down Expand Up @@ -213,7 +214,11 @@ public void sendMsg(
} catch (PaasException e) {
throw e;
} catch (Exception e) {
log.error("Fail to request {}", uri, e);
String msg = MessageFormatter.format(
"Fail to request {}",
uri
).getMessage();
log.error(msg, e);
status = EsbMetricTags.VALUE_STATUS_ERROR;
throw new PaasException(e, ErrorType.INTERNAL, ErrorCode.CMSI_API_ACCESS_ERROR, new Object[]{});
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.jooq.TableRecord;
import org.slf4j.helpers.MessageFormatter;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -102,7 +103,11 @@ public void archive(ArchiveConfig archiveConfig, Long maxNeedArchiveId, CountDow
delete(maxNeedArchiveId, archiveConfig);
}
} catch (Throwable e) {
log.error("Error while archiving {}", tableName, e);
String msg = MessageFormatter.format(
"Error while archiving {}",
tableName
).getMessage();
log.error(msg, e);
} finally {
archiveSummary.setArchiveEnabled(archiveConfig.isArchiveEnabled());
archiveSummary.setDeleteEnabled(archiveConfig.isDeleteEnabled());
Expand Down Expand Up @@ -191,7 +196,11 @@ private boolean archive(Long maxNeedArchiveId) {
}
return true;
} catch (Throwable e) {
log.error("Error while archiving {}", tableName, e);
String msg = MessageFormatter.format(
"Error while archiving {}",
tableName
).getMessage();
log.error(msg, e);
return false;
} finally {
archiveSummary.setArchiveIdStart(minNeedArchiveId);
Expand Down Expand Up @@ -287,7 +296,11 @@ private void delete(Long maxNeedArchiveId, ArchiveConfig archiveConfig) {
tableName, minNeedDeleteId, maxNeedDeleteId, lastDeletedId, deletedRows,
System.currentTimeMillis() - startTime);
} catch (Throwable e) {
log.error("Error while deleting {}", tableName, e);
String msg = MessageFormatter.format(
"Error while deleting {}",
tableName
).getMessage();
log.error(msg, e);
} finally {
archiveSummary.setDeleteCost(System.currentTimeMillis() - startTime);
archiveSummary.setDeleteIdStart(minNeedDeleteId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import com.tencent.bk.job.backup.service.ArchiveProgressService;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.context.SmartLifecycle;
import org.springframework.scheduling.annotation.Scheduled;

Expand Down Expand Up @@ -248,7 +249,11 @@ private void doArchive(Long endTime) throws InterruptedException {
} catch (InterruptedException e) {
throw e;
} catch (Throwable e) {
log.error("Error while do archive!|{}", endTime, e);
String msg = MessageFormatter.format(
"Error while do archive!|{}",
endTime
).getMessage();
log.error(msg, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -612,7 +613,11 @@ public void run() {
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (Exception e) {
log.error("{}|Error while processing export job!", uuid, e);
String msg = MessageFormatter.format(
"{}|Error while processing export job!",
uuid
).getMessage();
log.error(msg, e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.http.client.methods.HttpRequestBase;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -713,7 +714,11 @@ public void run() {
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (Exception e) {
log.error("{}|Error while processing import job!", uuid, e);
String msg = MessageFormatter.format(
"{}|Error while processing import job!",
uuid
).getMessage();
log.error(msg, e);
}
}
}
Expand Down
Loading

0 comments on commit e25624d

Please sign in to comment.