fixed logs delete check
This commit is contained in:
		
							parent
							
								
									f9fcea8770
								
							
						
					
					
						commit
						1e8e70c53c
					
				| 
						 | 
					@ -1256,13 +1256,15 @@ func (app *BaseApp) initLogger() error {
 | 
				
			||||||
				return nil
 | 
									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
 | 
								logsMaxDays := app.Settings().Logs.MaxDays
 | 
				
			||||||
			now := time.Now()
 | 
								now := time.Now()
 | 
				
			||||||
			lastLogsDeletedAt := cast.ToTime(app.Store().Get("lastLogsDeletedAt"))
 | 
								lastLogsDeletedAt := cast.ToTime(app.Store().Get("lastLogsDeletedAt"))
 | 
				
			||||||
			daysDiff := now.Sub(lastLogsDeletedAt).Hours() / 24
 | 
								daysDiff := now.Sub(lastLogsDeletedAt).Hours() / 24
 | 
				
			||||||
			if daysDiff >= float64(logsMaxDays) {
 | 
								if daysDiff >= 1 {
 | 
				
			||||||
				deleteErr := app.LogsDao().DeleteOldLogs(now.AddDate(0, 0, -1*logsMaxDays))
 | 
									deleteErr := app.LogsDao().DeleteOldLogs(now.AddDate(0, 0, -1*logsMaxDays))
 | 
				
			||||||
				if deleteErr == nil {
 | 
									if deleteErr == nil {
 | 
				
			||||||
					app.Store().Set("lastLogsDeletedAt", now)
 | 
										app.Store().Set("lastLogsDeletedAt", now)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -357,37 +357,67 @@ func TestBaseAppLoggerWrites(t *testing.T) {
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// trigger batch write
 | 
							// trigger batch write (A)
 | 
				
			||||||
		expectedLogs := logsThreshold
 | 
							expectedLogs := logsThreshold
 | 
				
			||||||
		for i := 0; i < expectedLogs; i++ {
 | 
							for i := 0; i < expectedLogs; i++ {
 | 
				
			||||||
			app.Logger().Error("test")
 | 
								app.Logger().Error("testA")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if total := totalLogs(app, t); total != expectedLogs {
 | 
							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
 | 
							// mark the A inserted logs as 2-day expired
 | 
				
			||||||
		expiredDate, err := types.ParseDateTime(time.Now().AddDate(0, 0, -4))
 | 
							aExpiredDate, err := types.ParseDateTime(time.Now().AddDate(0, 0, -2))
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			t.Fatal(err)
 | 
								t.Fatal(err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		app.Store().Set("lastLogsDeletedAt", expiredDate)
 | 
					 | 
				
			||||||
		_, err = app.LogsDao().NonconcurrentDB().NewQuery("UPDATE _logs SET created={:date}, updated={:date}").Bind(dbx.Params{
 | 
							_, err = app.LogsDao().NonconcurrentDB().NewQuery("UPDATE _logs SET created={:date}, updated={:date}").Bind(dbx.Params{
 | 
				
			||||||
			"date": expiredDate.String(),
 | 
								"date": aExpiredDate.String(),
 | 
				
			||||||
		}).Execute()
 | 
							}).Execute()
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			t.Fatalf("Failed to mock logs timestamp fields: %v", err)
 | 
								t.Fatalf("Failed to mock logs timestamp fields: %v", err)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// trigger batch write (twice)
 | 
							// simulate recently deleted logs
 | 
				
			||||||
		expectedLogs = 2 * logsThreshold
 | 
							app.Store().Set("lastLogsDeletedAt", time.Now())
 | 
				
			||||||
		for i := 0; i < expectedLogs; i++ {
 | 
					
 | 
				
			||||||
			app.Logger().Error("test")
 | 
							// trigger batch write (B)
 | 
				
			||||||
 | 
							for i := 0; i < logsThreshold; i++ {
 | 
				
			||||||
 | 
								app.Logger().Error("testB")
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							expectedLogs = 2 * logsThreshold
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// 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 {
 | 
							if total := totalLogs(app, t); total != expectedLogs {
 | 
				
			||||||
			t.Fatalf("[after delete] Expected %d logs, got %d", expectedLogs, total)
 | 
								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("[batch write C] Expected %d logs, got %d", expectedLogs, total)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if deleteQueries != 1 {
 | 
							if deleteQueries != 1 {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue