mirror of
https://github.com/datarhei/core.git
synced 2025-09-27 12:22:28 +08:00
Simplify updating cleanup rules
This commit is contained in:
@@ -129,8 +129,6 @@ func (rfs *filesystem) compilePatterns(patterns []Pattern) []Pattern {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (rfs *filesystem) UpdateCleanup(id string, newPatterns []Pattern) {
|
func (rfs *filesystem) UpdateCleanup(id string, newPatterns []Pattern) {
|
||||||
rfs.logger.Debug().WithField("id", id).Log("Update pattern group")
|
|
||||||
|
|
||||||
newPatterns = rfs.compilePatterns(newPatterns)
|
newPatterns = rfs.compilePatterns(newPatterns)
|
||||||
|
|
||||||
rfs.cleanupLock.Lock()
|
rfs.cleanupLock.Lock()
|
||||||
@@ -141,28 +139,22 @@ func (rfs *filesystem) UpdateCleanup(id string, newPatterns []Pattern) {
|
|||||||
|
|
||||||
onlyCurrent, onlyNew := slices.DiffEqualer(currentPatterns, newPatterns)
|
onlyCurrent, onlyNew := slices.DiffEqualer(currentPatterns, newPatterns)
|
||||||
|
|
||||||
patterns := []Pattern{}
|
for _, p := range newPatterns {
|
||||||
|
|
||||||
for _, p := range currentPatterns {
|
|
||||||
found := false
|
found := false
|
||||||
for _, x := range onlyCurrent {
|
for _, x := range onlyNew {
|
||||||
if p.Equal(x) == nil {
|
if p.Equal(x) == nil {
|
||||||
found = true
|
found = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !found {
|
if !found {
|
||||||
patterns = append(patterns, p)
|
|
||||||
rfs.logger.Debug().WithFields(log.Fields{
|
rfs.logger.Debug().WithFields(log.Fields{
|
||||||
"id": id,
|
"id": id,
|
||||||
"pattern": p.Pattern,
|
"pattern": p.Pattern,
|
||||||
"max_files": p.MaxFiles,
|
"max_files": p.MaxFiles,
|
||||||
"max_file_age": p.MaxFileAge.Seconds(),
|
"max_file_age": p.MaxFileAge.Seconds(),
|
||||||
}).Log("Keep pattern")
|
}).Log("Keep pattern")
|
||||||
}
|
} else {
|
||||||
}
|
|
||||||
|
|
||||||
for _, p := range onlyNew {
|
|
||||||
rfs.logger.Debug().WithFields(log.Fields{
|
rfs.logger.Debug().WithFields(log.Fields{
|
||||||
"id": id,
|
"id": id,
|
||||||
"pattern": p.Pattern,
|
"pattern": p.Pattern,
|
||||||
@@ -170,10 +162,9 @@ func (rfs *filesystem) UpdateCleanup(id string, newPatterns []Pattern) {
|
|||||||
"max_file_age": p.MaxFileAge.Seconds(),
|
"max_file_age": p.MaxFileAge.Seconds(),
|
||||||
}).Log("Add pattern")
|
}).Log("Add pattern")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
patterns = append(patterns, onlyNew...)
|
rfs.cleanupPatterns[id] = newPatterns
|
||||||
|
|
||||||
rfs.cleanupPatterns[id] = patterns
|
|
||||||
|
|
||||||
for _, p := range onlyCurrent {
|
for _, p := range onlyCurrent {
|
||||||
rfs.logger.Debug().WithFields(log.Fields{
|
rfs.logger.Debug().WithFields(log.Fields{
|
||||||
@@ -258,10 +249,14 @@ func (rfs *filesystem) purge(patterns []Pattern) int64 {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
_, nfiles := rfs.Filesystem.RemoveList("/", fs.ListOptions{
|
files, nfiles := rfs.Filesystem.RemoveList("/", fs.ListOptions{
|
||||||
Pattern: pattern.Pattern,
|
Pattern: pattern.Pattern,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
for _, file := range files {
|
||||||
|
rfs.logger.Debug().WithField("path", file).Log("Purged file")
|
||||||
|
}
|
||||||
|
|
||||||
nfilesTotal += nfiles
|
nfilesTotal += nfiles
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -48,6 +48,12 @@ func TestUpdateCleanup(t *testing.T) {
|
|||||||
|
|
||||||
require.Equal(t, cleanfs.cleanupPatterns["foobar"], patterns)
|
require.Equal(t, cleanfs.cleanupPatterns["foobar"], patterns)
|
||||||
|
|
||||||
|
patterns[0].MaxFiles = 42
|
||||||
|
|
||||||
|
cleanfs.UpdateCleanup("foobar", patterns)
|
||||||
|
|
||||||
|
require.Equal(t, cleanfs.cleanupPatterns["foobar"], patterns)
|
||||||
|
|
||||||
cleanfs.UpdateCleanup("foobar", patterns[1:])
|
cleanfs.UpdateCleanup("foobar", patterns[1:])
|
||||||
|
|
||||||
require.Equal(t, cleanfs.cleanupPatterns["foobar"], patterns[1:])
|
require.Equal(t, cleanfs.cleanupPatterns["foobar"], patterns[1:])
|
||||||
|
Reference in New Issue
Block a user