Merge branch 'feat/log-shrink-file' into 'master'

Feat: limit size of log file

See merge request sheepitrenderfarm/client!331
This commit is contained in:
Sheepit Renderfarm
2024-10-25 10:06:33 +00:00
2 changed files with 35 additions and 0 deletions

View File

@@ -211,6 +211,8 @@ import okhttp3.HttpUrl;
}
}
this.log.shrink();
step = this.log.newCheckPoint();
try {
Calendar nextRequest = this.nextJobRequest();

View File

@@ -21,7 +21,9 @@ package com.sheepit.client;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.text.DateFormat;
@@ -38,6 +40,9 @@ public final class Log {
private final String LEVEL_INFO = "info";
private final String LEVEL_ERROR = "error";
public final int MAX_SIZE = 10000000; // in B
public final int PERIOD = 3600000; // duration in ms between two shrink
private static Log instance = null;
private Map<Integer, List<String>> checkpoints = new HashMap<>();
@@ -49,6 +54,8 @@ public final class Log {
private String logFile;
private long lastShrink;
private Log(boolean debugLevel_, boolean print_, String logDirectory_) {
this.debugLevel = debugLevel_;
this.printStdOut = print_;
@@ -56,6 +63,7 @@ public final class Log {
this.lastCheckPoint = 0;
this.checkpoints.put(this.lastCheckPoint, new ArrayList<String>());
this.dateFormat = new SimpleDateFormat("dd-MM HH:mm:ss");
this.lastShrink = 0;
}
public void debug(String msg_) {
@@ -137,6 +145,31 @@ public final class Log {
}
}
public synchronized void shrink() {
if (System.currentTimeMillis() > (this.lastShrink + this.PERIOD)) {
try (RandomAccessFile raf = new RandomAccessFile(this.logFile, "r")) {
long fileLength = raf.length();
if (fileLength < MAX_SIZE) { // nothing to do, it's a small file
return;
}
long startPosition = Math.max(0, fileLength - MAX_SIZE);
raf.seek(startPosition);
byte[] newBytes = new byte[(int) (fileLength - startPosition)];
raf.readFully(newBytes);
Path inputPath = Paths.get(this.logFile);
Files.write(inputPath, newBytes);
}
catch (IOException e) {
e.printStackTrace();
}
this.lastShrink = System.currentTimeMillis();
}
}
public static synchronized Log getInstance(Configuration config) {
if (instance == null) {
String path = null;