Skip to content

Commit

Permalink
fixed logs delete check
Browse files Browse the repository at this point in the history
  • Loading branch information
ganigeorgiev committed Jul 9, 2024
1 parent f9fcea8 commit 1e8e70c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
6 changes: 4 additions & 2 deletions core/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -1256,13 +1256,15 @@ func (app *BaseApp) initLogger() error {
return nil
})

// delete old logs
// @todo replace with cron so that it doesn't rely on the logs write
//
// delete old logs (~ once 1 day)
// ---
logsMaxDays := app.Settings().Logs.MaxDays
now := time.Now()
lastLogsDeletedAt := cast.ToTime(app.Store().Get("lastLogsDeletedAt"))
daysDiff := now.Sub(lastLogsDeletedAt).Hours() / 24
if daysDiff >= float64(logsMaxDays) {
if daysDiff >= 1 {
deleteErr := app.LogsDao().DeleteOldLogs(now.AddDate(0, 0, -1*logsMaxDays))
if deleteErr == nil {
app.Store().Set("lastLogsDeletedAt", now)
Expand Down
52 changes: 41 additions & 11 deletions core/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,37 +357,67 @@ func TestBaseAppLoggerWrites(t *testing.T) {
}
}

// trigger batch write
// trigger batch write (A)
expectedLogs := logsThreshold
for i := 0; i < expectedLogs; i++ {
app.Logger().Error("test")
app.Logger().Error("testA")
}

if total := totalLogs(app, t); total != expectedLogs {
t.Fatalf("[before delete] Expected %d logs, got %d", expectedLogs, total)
t.Fatalf("[batch write A] Expected %d logs, got %d", expectedLogs, total)
}

// mock expired
expiredDate, err := types.ParseDateTime(time.Now().AddDate(0, 0, -4))
// mark the A inserted logs as 2-day expired
aExpiredDate, err := types.ParseDateTime(time.Now().AddDate(0, 0, -2))
if err != nil {
t.Fatal(err)
}
app.Store().Set("lastLogsDeletedAt", expiredDate)
_, err = app.LogsDao().NonconcurrentDB().NewQuery("UPDATE _logs SET created={:date}, updated={:date}").Bind(dbx.Params{
"date": expiredDate.String(),
"date": aExpiredDate.String(),
}).Execute()
if err != nil {
t.Fatalf("Failed to mock logs timestamp fields: %v", err)
}

// trigger batch write (twice)
// simulate recently deleted logs
app.Store().Set("lastLogsDeletedAt", time.Now())

// trigger batch write (B)
for i := 0; i < logsThreshold; i++ {
app.Logger().Error("testB")
}

expectedLogs = 2 * logsThreshold
for i := 0; i < expectedLogs; i++ {
app.Logger().Error("test")

// note: even though there are expired logs it shouldn't perform the delete operation because of the lastLogsDeledAt time
if total := totalLogs(app, t); total != expectedLogs {
t.Fatalf("[batch write B] Expected %d logs, got %d", expectedLogs, total)
}

// mark the B inserted logs as 1-day expired to ensure that they will not be deleted
bExpiredDate, err := types.ParseDateTime(time.Now().AddDate(0, 0, -1))
if err != nil {
t.Fatal(err)
}
_, err = app.LogsDao().NonconcurrentDB().NewQuery("UPDATE _logs SET created={:date}, updated={:date} where message='testB'").Bind(dbx.Params{
"date": bExpiredDate.String(),
}).Execute()
if err != nil {
t.Fatalf("Failed to mock logs timestamp fields: %v", err)
}

// should trigger delete on the next batch write
app.Store().Set("lastLogsDeletedAt", time.Now().AddDate(0, 0, -1))

// trigger batch write (C)
for i := 0; i < logsThreshold; i++ {
app.Logger().Error("testC")
}

expectedLogs = 2 * logsThreshold // only B and C logs should remain

if total := totalLogs(app, t); total != expectedLogs {
t.Fatalf("[after delete] Expected %d logs, got %d", expectedLogs, total)
t.Fatalf("[batch write C] Expected %d logs, got %d", expectedLogs, total)
}

if deleteQueries != 1 {
Expand Down

0 comments on commit 1e8e70c

Please sign in to comment.