Skip to content

Commit

Permalink
👔 up: rotatefile - update some logic for cleanup, add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jul 8, 2023
1 parent 2a20d47 commit 37f31dd
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 44 deletions.
24 changes: 23 additions & 1 deletion rotatefile/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func main() {
}
```

## Available config options
### Available config options

```go
// Config struct for rotate dispatcher
Expand Down Expand Up @@ -107,3 +107,25 @@ type Config struct {
TimeClock Clocker
}
```

## Files clear

```go
fc := rotatefile.NewFilesClear(func(c *rotatefile.CConfig) {
c.AddPattern("/path/to/some*.log")
c.BackupNum = 2
c.BackupTime = 12 // 12 hours
})

// clear files on daemon
go fc.DaemonClean()

// NOTE: stop daemon before exit
// fc.QuitDaemon()
```

### Configs

```go

```
4 changes: 2 additions & 2 deletions rotatefile/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ func (r *FilesClear) WithConfigFn(fns ...CConfigFunc) *FilesClear {
// ---------------------------------------------------------------------------
//

// QuitDaemon for stop daemon clean
func (r *FilesClear) QuitDaemon() {
// StopDaemon for stop daemon clean
func (r *FilesClear) StopDaemon() {
if r.quitDaemon == nil {
panic("cannot quit daemon, please call DaemonClean() first")
}
Expand Down
12 changes: 6 additions & 6 deletions rotatefile/cleanup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestFilesClear_Clean(t *testing.T) {
fc.WithConfigFn(func(c *rotatefile.CConfig) {
c.AddDirPath("testdata", "not-exist-dir")
c.BackupNum = 1
c.BackupTime = 2
c.BackupTime = 3
c.TimeUnit = time.Second // for test
})

Expand All @@ -31,6 +31,8 @@ func TestFilesClear_Clean(t *testing.T) {
// make files for clean
makeNum := 5
makeWaitCleanFiles("file_clean.log", makeNum)
_, err := fsutil.PutContents("testdata/subdir/some.txt", "test data")
assert.NoErr(t, err)

// do clean
assert.NoErr(t, fc.Clean())
Expand All @@ -56,7 +58,7 @@ func TestFilesClear_DaemonClean(t *testing.T) {
c.BackupTime = 0
})
assert.Panics(t, func() {
fc.QuitDaemon()
fc.StopDaemon()
})
assert.Panics(t, func() {
fc.DaemonClean(nil)
Expand All @@ -65,7 +67,7 @@ func TestFilesClear_DaemonClean(t *testing.T) {

fc := rotatefile.NewFilesClear(func(c *rotatefile.CConfig) {
c.AddPattern("testdata/file_daemon_clean.*")
c.BackupNum = 2
c.BackupNum = 1
c.BackupTime = 3
c.TimeUnit = time.Second // for test
c.CheckInterval = time.Second // for test
Expand All @@ -76,8 +78,6 @@ func TestFilesClear_DaemonClean(t *testing.T) {

// make files for clean
makeNum := 5
_, err := fsutil.PutContents("testdata/subdir/some.txt", "test data")
assert.NoErr(t, err)
makeWaitCleanFiles("file_daemon_clean.log", makeNum)

// test daemon clean
Expand All @@ -94,7 +94,7 @@ func TestFilesClear_DaemonClean(t *testing.T) {
go func() {
time.Sleep(time.Second * 1)
fmt.Println("stop daemon clean")
fc.QuitDaemon()
fc.StopDaemon()
}()

// wait for stop
Expand Down
35 changes: 35 additions & 0 deletions rotatefile/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rotatefile
import (
"compress/gzip"
"io"
"io/fs"
"os"

"github.com/gookit/goutil/fsutil"
Expand Down Expand Up @@ -89,3 +90,37 @@ func findFilesInDir(dir string, handleFn handleFunc, filters ...filterFunc) (err
}
return nil
}

// TODO replace to fsutil.FileInfo
type fileInfo struct {
fs.FileInfo
filePath string
}

// Path get file full path. eg: "/path/to/file.go"
func (fi *fileInfo) Path() string {
return fi.filePath
}

func newFileInfo(filePath string, fi fs.FileInfo) fileInfo {
return fileInfo{filePath: filePath, FileInfo: fi}
}

// modTimeFInfos sorts by oldest time modified in the fileInfo.
// eg: [old_220211, old_220212, old_220213]
type modTimeFInfos []fileInfo

// Less check
func (fis modTimeFInfos) Less(i, j int) bool {
return fis[j].ModTime().After(fis[i].ModTime())
}

// Swap value
func (fis modTimeFInfos) Swap(i, j int) {
fis[i], fis[j] = fis[j], fis[i]
}

// Len get
func (fis modTimeFInfos) Len() int {
return len(fis)
}
35 changes: 0 additions & 35 deletions rotatefile/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package rotatefile

import (
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -395,37 +394,3 @@ func (d *Writer) compressFiles(oldFiles []fileInfo) error {
}
return nil
}

// TODO replace to fsutil.FileInfo
type fileInfo struct {
fs.FileInfo
filePath string
}

// Path get file full path. eg: "/path/to/file.go"
func (fi *fileInfo) Path() string {
return fi.filePath
}

func newFileInfo(filePath string, fi fs.FileInfo) fileInfo {
return fileInfo{filePath: filePath, FileInfo: fi}
}

// modTimeFInfos sorts by oldest time modified in the fileInfo.
// eg: [old_220211, old_220212, old_220213]
type modTimeFInfos []fileInfo

// Less check
func (fis modTimeFInfos) Less(i, j int) bool {
return fis[j].ModTime().After(fis[i].ModTime())
}

// Swap value
func (fis modTimeFInfos) Swap(i, j int) {
fis[i], fis[j] = fis[j], fis[i]
}

// Len get
func (fis modTimeFInfos) Len() int {
return len(fis)
}
2 changes: 2 additions & 0 deletions rotatefile/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ func TestWriter_Clean(t *testing.T) {
_, err = wr.WriteString("hi\n")
assert.NoErr(t, err)

assert.Err(t, wr.Clean())

// test clean and backup
c.BackupNum = 2
c.Compress = true
Expand Down

0 comments on commit 37f31dd

Please sign in to comment.