@@ -13,8 +13,6 @@ import (
1313 "go.opentelemetry.io/otel/trace"
1414)
1515
16- const logFileSizeThreshold int64 = 50 * 1024 * 1024 // 50 MB
17-
1816var GCCommand * cli.Command = & cli.Command {
1917 Name : "gc" ,
2018 Usage : "clean internal storage" ,
@@ -34,52 +32,6 @@ var GCCommand *cli.Command = &cli.Command{
3432 Action : commandGC ,
3533}
3634
37- // cleanLogFile removes a log file if it exceeds the threshold or if force is true.
38- // Returns the size of the deleted file (0 if not deleted or file doesn't exist).
39- func cleanLogFile (filePath string , threshold int64 , force bool ) (int64 , error ) {
40- info , err := os .Stat (filePath )
41- if os .IsNotExist (err ) {
42- return 0 , nil
43- }
44- if err != nil {
45- return 0 , fmt .Errorf ("failed to stat file %s: %w" , filePath , err )
46- }
47-
48- fileSize := info .Size ()
49- if ! force && fileSize < threshold {
50- return 0 , nil
51- }
52-
53- if err := os .Remove (filePath ); err != nil {
54- return 0 , fmt .Errorf ("failed to remove file %s: %w" , filePath , err )
55- }
56-
57- slog .Info ("cleaned log file" , slog .String ("file" , filePath ), slog .Int64 ("size_bytes" , fileSize ))
58- return fileSize , nil
59- }
60-
61- // cleanLargeLogFiles checks all log files and removes those exceeding the size threshold.
62- // If force is true, removes all log files regardless of size.
63- func cleanLargeLogFiles (force bool ) (int64 , error ) {
64- logFiles := []string {
65- model .GetLogFilePath (),
66- model .GetHeartbeatLogFilePath (),
67- model .GetSyncPendingFilePath (),
68- }
69-
70- var totalFreed int64
71- for _ , filePath := range logFiles {
72- freed , err := cleanLogFile (filePath , logFileSizeThreshold , force )
73- if err != nil {
74- slog .Warn ("failed to clean log file" , slog .String ("file" , filePath ), slog .Any ("err" , err ))
75- continue
76- }
77- totalFreed += freed
78- }
79-
80- return totalFreed , nil
81- }
82-
8335// backupAndWriteFile backs up the existing file and writes new content.
8436func backupAndWriteFile (filePath string , content []byte ) error {
8537 backupFile := filePath + ".bak"
@@ -231,9 +183,17 @@ func commandGC(c *cli.Context) error {
231183 return nil
232184 }
233185
186+ // Get config for threshold
187+ cfg , err := configService .ReadConfigFile (ctx )
188+ if err != nil {
189+ slog .Warn ("failed to read config, using default threshold" , slog .Any ("err" , err ))
190+ cfg .LogCleanup = & model.LogCleanup {ThresholdMB : 100 }
191+ }
192+ thresholdBytes := cfg .LogCleanup .ThresholdMB * 1024 * 1024
193+
234194 // Clean log files: force clean if --withLog, otherwise only clean large files
235195 forceCleanLogs := c .Bool ("withLog" )
236- freedBytes , err := cleanLargeLogFiles ( forceCleanLogs )
196+ freedBytes , err := model . CleanLargeLogFiles ( thresholdBytes , forceCleanLogs )
237197 if err != nil {
238198 slog .Warn ("error during log cleanup" , slog .Any ("err" , err ))
239199 }
0 commit comments