Skip to content

Commit

Permalink
log-exporter: improve deleting trace log dir
Browse files Browse the repository at this point in the history
Signed-off-by: neo <[email protected]>
  • Loading branch information
neowu committed Dec 31, 2024
1 parent f88282f commit 49c7135
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
1 change: 1 addition & 0 deletions core-ng/src/test/java/core/framework/util/FilesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ void tempDir() {
Files.createDir(tempDir.resolve("temp")); // createDir skips if dir already exists

Files.deleteDir(tempDir);
assertThat(tempDir).doesNotExist();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

import core.framework.crypto.Hash;
import core.framework.inject.Inject;
import core.framework.util.Files;
import core.framework.util.Network;
import core.framework.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDate;
import java.time.LocalDateTime;

import static java.nio.file.Files.createDirectories;
import static java.nio.file.Files.exists;

/**
* @author neo
*/
Expand All @@ -32,18 +35,18 @@ public void uploadArchive(LocalDate date) {

String actionLogPath = actionLogPath(date);
Path actionLogFilePath = Path.of(logDir.toString(), actionLogPath);
if (Files.exists(actionLogFilePath)) {
if (exists(actionLogFilePath)) {
uploadService.upload(actionLogFilePath, actionLogPath);
}

String eventPath = eventPath(date);
Path eventFilePath = Path.of(logDir.toString(), eventPath);
if (Files.exists(eventFilePath)) {
if (exists(eventFilePath)) {
uploadService.upload(eventFilePath, eventPath);
}

Path traceLogDirPath = Path.of(logDir.toString(), Strings.format("/trace/{}", date));
if (Files.exists(traceLogDirPath)) {
if (exists(traceLogDirPath)) {
File[] appDirs = traceLogDirPath.toFile().listFiles(File::isDirectory);
if (appDirs != null) {
for (File appDir : appDirs) {
Expand All @@ -69,10 +72,10 @@ public void cleanupArchive(LocalDate date) {
shell.execute("rm", "-f", eventFilePath.toString());

Path traceLogDirPath = Path.of(logDir.toString(), Strings.format("/trace/{}", date));
if (Files.exists(traceLogDirPath)) {
String path = traceLogDirPath.toString();
shell.execute("find", path, "-type", "f", "-delete");
shell.execute("find", path, "-type", "d", "-delete");
if (exists(traceLogDirPath)) {
logger.info("delete trace logs, path={}", traceLogDirPath);
// use shell (rm -rf or find) may cause pod terminate with error code 137 on mounted disk
Files.deleteDir(traceLogDirPath);
}
}

Expand All @@ -91,7 +94,7 @@ public String traceLogPath(LocalDateTime now, String app, String id) {
public Path initializeLogFilePath(String logPath) throws IOException {
Path path = Path.of(logDir.toString(), logPath);
Path parent = path.getParent();
if (parent != null && !Files.exists(parent)) Files.createDirectories(parent);
if (parent != null && !exists(parent)) createDirectories(parent);
return path;
}
}

0 comments on commit 49c7135

Please sign in to comment.