ImportMap Havedone mit allen Einträgen älter als 60 Minuten bereinigen

This commit is contained in:
Marco Kittel 2025-06-29 21:12:42 +02:00
parent 3393adf5f8
commit 28cf9d7acd
1 changed files with 33 additions and 7 deletions

View File

@ -28,17 +28,19 @@ type CountryCsvData struct {
type ElioHandleFunc func(ctx context.Context, filename string, data chan<- CountryCsvData) bool
type ElioDateiFoo struct {
process ElioHandleFunc
lookUpDir string
processedDir string
mu sync.RWMutex
haveDone map[string]time.Time
process ElioHandleFunc
lookUpDir string
processedDir string
mu sync.RWMutex
haveDone map[string]time.Time
clearHaveDoneRunning bool
}
func NewElioDateiFoo(lookUpDir string, process ElioHandleFunc) *ElioDateiFoo {
df := ElioDateiFoo{lookUpDir: lookUpDir,
process: process,
haveDone: make(map[string]time.Time),
process: process,
haveDone: make(map[string]time.Time),
clearHaveDoneRunning: false,
}
return &df
}
@ -49,6 +51,30 @@ func NewElioDateiFoo(lookUpDir string, process ElioHandleFunc) *ElioDateiFoo {
// Veraltete Dateien mit Präfix no-import und Postifx .old benennen.
// Gültige Dateinamen zur Extraktion in Closure übergeben.
func (f *ElioDateiFoo) ScanCsv(ctx context.Context, data chan<- CountryCsvData) error {
//Importnamen die vor einer Stunde importiert wurden aus der Map aufräumen.
if !f.clearHaveDoneRunning {
f.clearHaveDoneRunning = true
go func() {
for {
select {
case <-ctx.Done():
default:
time.Sleep(time.Minute * 1)
f.mu.RLock()
jetzt := time.Now()
for k, v := range f.haveDone {
if jetzt.Unix() > v.Add(time.Minute*60).Unix() {
f.mu.Lock()
delete(f.haveDone, k)
f.mu.Unlock()
}
}
f.mu.RUnlock()
}
}
}()
}
files, err := os.ReadDir(f.lookUpDir)
if err != nil {
return err