129 lines
3.4 KiB
Go
129 lines
3.4 KiB
Go
package dataservice
|
|
|
|
import (
|
|
"context"
|
|
"io/fs"
|
|
"os"
|
|
"sync"
|
|
"time"
|
|
|
|
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/dbwriter"
|
|
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/event"
|
|
"gittea.marcokittel.de/elio/eliotools/eliofile"
|
|
"gittea.marcokittel.de/elio/eliotools/logger"
|
|
"gittea.marcokittel.de/elio/eliotools/tools"
|
|
)
|
|
|
|
func DataServiceDebugHandler(log logger.Logger, processedDir string) func(filename string) bool {
|
|
return func(filename string) bool {
|
|
if tools.IsFilenameValid(filename) {
|
|
t, err := tools.ExtractDateAndConvertToDate(filename)
|
|
if err != nil {
|
|
log.Warning(filename + "\n" + err.Error())
|
|
return false
|
|
}
|
|
log.Info("Datei gefunden.: " + filename + " " + t.String() + "\n")
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
}
|
|
|
|
type DataService struct {
|
|
listener []event.EventListener
|
|
log logger.Logger
|
|
ef eliofile.ElioDateiFoo
|
|
newFolderPath string
|
|
processedFolderPath string
|
|
dirCreationRights int
|
|
}
|
|
|
|
func (d *DataService) ListenerCount() int {
|
|
return len(d.listener)
|
|
}
|
|
|
|
func (d *DataService) Run(ctx context.Context, wg *sync.WaitGroup) {
|
|
defer wg.Done()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
default:
|
|
//Hier nach neuen Dateien suchen
|
|
for _, l := range d.listener {
|
|
d.ef.ScanCsv()
|
|
err := l.HandleData(ctx, dbwriter.MyStruct{A: "dkl", B: 3})
|
|
if err != nil {
|
|
//todo
|
|
}
|
|
}
|
|
time.Sleep(time.Second * 5)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (d *DataService) AddListener(ev event.EventListener) {
|
|
d.listener = append(d.listener, ev)
|
|
}
|
|
|
|
type Option func(*DataService)
|
|
|
|
func WithCustomCsvProcesser(process eliofile.ElioHandleFunc) Option {
|
|
return func(ds *DataService) {
|
|
ds.ef = *eliofile.NewElioDateiFoo(ds.newFolderPath, process)
|
|
}
|
|
}
|
|
|
|
// func WithCustomLookupDirectory(lookupDirectory string) Option {
|
|
// return func(ds *DataService) {
|
|
// ds.newFolderPath = lookupDirectory
|
|
// }
|
|
// }
|
|
|
|
// func WithCustomProcessedDirectory(processedDirectory string) Option {
|
|
// return func(ds *DataService) {
|
|
// ds.processedFolderPath = processedDirectory
|
|
// }
|
|
// }
|
|
|
|
func WithCustomDirectoryPermissions(permissions int) Option {
|
|
return func(ds *DataService) {
|
|
ds.dirCreationRights = permissions
|
|
}
|
|
}
|
|
|
|
func NewDataService(options ...Option) *DataService {
|
|
lookUpFolderPath, err := os.Getwd()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
newFolderPath := "/new"
|
|
processedFolderPath := "/processed"
|
|
dirCreationRights := 0755
|
|
l := logger.NewMarcoLogger()
|
|
ds := DataService{listener: make([]event.EventListener, 0),
|
|
log: l,
|
|
newFolderPath: lookUpFolderPath + newFolderPath,
|
|
processedFolderPath: lookUpFolderPath + processedFolderPath,
|
|
dirCreationRights: dirCreationRights}
|
|
|
|
ds.ef = *eliofile.NewElioDateiFoo(ds.newFolderPath, DataServiceDebugHandler(l, ds.processedFolderPath))
|
|
|
|
for _, opt := range options {
|
|
opt(&ds)
|
|
}
|
|
|
|
l.Infof("Suche CSV Dateien in %s.\n Verarbeitete Dateien gelangen in %s.\n", ds.newFolderPath, ds.processedFolderPath)
|
|
exists, err := tools.CheckDir(ds.newFolderPath)
|
|
if err == nil && !exists {
|
|
l.Infof("Erzeuge Verzeichnis: %s\n", ds.newFolderPath)
|
|
tools.Createdir(ds.newFolderPath, fs.FileMode(ds.dirCreationRights))
|
|
}
|
|
exists, err = tools.CheckDir(ds.processedFolderPath)
|
|
if err == nil && !exists {
|
|
l.Infof("Erzeuge Verzeichnis: %s\n", ds.processedFolderPath)
|
|
tools.Createdir(ds.processedFolderPath, fs.FileMode(ds.dirCreationRights))
|
|
}
|
|
return &ds
|
|
}
|