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

@ -33,12 +33,14 @@ type ElioDateiFoo struct {
processedDir string processedDir string
mu sync.RWMutex mu sync.RWMutex
haveDone map[string]time.Time haveDone map[string]time.Time
clearHaveDoneRunning bool
} }
func NewElioDateiFoo(lookUpDir string, process ElioHandleFunc) *ElioDateiFoo { func NewElioDateiFoo(lookUpDir string, process ElioHandleFunc) *ElioDateiFoo {
df := ElioDateiFoo{lookUpDir: lookUpDir, df := ElioDateiFoo{lookUpDir: lookUpDir,
process: process, process: process,
haveDone: make(map[string]time.Time), haveDone: make(map[string]time.Time),
clearHaveDoneRunning: false,
} }
return &df return &df
} }
@ -49,6 +51,30 @@ func NewElioDateiFoo(lookUpDir string, process ElioHandleFunc) *ElioDateiFoo {
// Veraltete Dateien mit Präfix no-import und Postifx .old benennen. // Veraltete Dateien mit Präfix no-import und Postifx .old benennen.
// Gültige Dateinamen zur Extraktion in Closure übergeben. // Gültige Dateinamen zur Extraktion in Closure übergeben.
func (f *ElioDateiFoo) ScanCsv(ctx context.Context, data chan<- CountryCsvData) error { 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) files, err := os.ReadDir(f.lookUpDir)
if err != nil { if err != nil {
return err return err