Pull request: 3823 fix rotation check
Merge in DNS/adguard-home from 3823-log-rotation to master Closes #3823. Squashed commit of the following: commit 4075c6994ec71210fa38b3e05021149e0fa13841 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Nov 9 18:27:09 2021 +0500 querylog: fix rotation check
This commit is contained in:
parent
b0b2eb3741
commit
6fd9e72fbb
|
@ -9,7 +9,6 @@ import (
|
||||||
|
|
||||||
"github.com/AdguardTeam/golibs/errors"
|
"github.com/AdguardTeam/golibs/errors"
|
||||||
"github.com/AdguardTeam/golibs/log"
|
"github.com/AdguardTeam/golibs/log"
|
||||||
"github.com/AdguardTeam/golibs/timeutil"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// flushLogBuffer flushes the current buffer to file and resets the current buffer
|
// flushLogBuffer flushes the current buffer to file and resets the current buffer
|
||||||
|
@ -136,36 +135,49 @@ func (l *queryLog) readFileFirstTimeValue() (first time.Time, err error) {
|
||||||
func (l *queryLog) periodicRotate() {
|
func (l *queryLog) periodicRotate() {
|
||||||
defer log.OnPanic("querylog: rotating")
|
defer log.OnPanic("querylog: rotating")
|
||||||
|
|
||||||
rotations := time.NewTicker(1 * timeutil.Day)
|
l.checkAndRotate()
|
||||||
|
|
||||||
|
// rotationCheckIvl is the period of time between checking the need for
|
||||||
|
// rotating log files. It's smaller of any available rotation interval to
|
||||||
|
// increase time accuracy.
|
||||||
|
//
|
||||||
|
// See https://github.com/AdguardTeam/AdGuardHome/issues/3823.
|
||||||
|
const rotationCheckIvl = 1 * time.Hour
|
||||||
|
|
||||||
|
rotations := time.NewTicker(rotationCheckIvl)
|
||||||
defer rotations.Stop()
|
defer rotations.Stop()
|
||||||
|
|
||||||
for range rotations.C {
|
for range rotations.C {
|
||||||
|
l.checkAndRotate()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkAndRotate rotates log files if those are older than the specified
|
||||||
|
// rotation interval.
|
||||||
|
func (l *queryLog) checkAndRotate() {
|
||||||
oldest, err := l.readFileFirstTimeValue()
|
oldest, err := l.readFileFirstTimeValue()
|
||||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||||
log.Error("querylog: reading oldest record for rotation: %s", err)
|
log.Error("querylog: reading oldest record for rotation: %s", err)
|
||||||
|
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
rot := oldest.Add(l.conf.RotationIvl)
|
if rot, now := oldest.Add(l.conf.RotationIvl), time.Now(); rot.After(now) {
|
||||||
now := time.Now()
|
|
||||||
if rot.After(time.Now()) {
|
|
||||||
log.Debug(
|
log.Debug(
|
||||||
"querylog: %s <= %s, not rotating",
|
"querylog: %s <= %s, not rotating",
|
||||||
now.Format(time.RFC3339),
|
now.Format(time.RFC3339),
|
||||||
rot.Format(time.RFC3339),
|
rot.Format(time.RFC3339),
|
||||||
)
|
)
|
||||||
|
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = l.rotate()
|
err = l.rotate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("querylog: rotating: %s", err)
|
log.Error("querylog: rotating: %s", err)
|
||||||
|
|
||||||
continue
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debug("querylog: rotated successfully")
|
log.Debug("querylog: rotated successfully")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue