From 62776f06334347cbf365be22163788696297f4ef Mon Sep 17 00:00:00 2001 From: Marco Kittel Date: Mon, 23 Jun 2025 00:39:39 +0200 Subject: [PATCH] Einbau von Goroutine, Waitgroups und Context --- cmd/shell/main.go | 43 ++++++++++++++++++++++++++++++++----------- internal/app/app.go | 30 +++++++++--------------------- 2 files changed, 41 insertions(+), 32 deletions(-) diff --git a/cmd/shell/main.go b/cmd/shell/main.go index 33437be..2607ecc 100644 --- a/cmd/shell/main.go +++ b/cmd/shell/main.go @@ -1,7 +1,12 @@ package main import ( - "fmt" + "context" + "os" + "os/signal" + "sync" + "syscall" + "time" "gittea.marcokittel.de/elio/eliotools/datawriter/internal/app" "gittea.marcokittel.de/elio/eliotools/datawriter/internal/dbwriter" @@ -14,19 +19,35 @@ const ( StockMustHaveFileLen = 38 ) -type dbfunction func(data dbwriter.DBWriter) - -var filename = "2023-11-09T15:02:17+00:00-CH-stock.csv" - -func GetDbBehavior() dbfunction { - return func(data dbwriter.DBWriter) { - fmt.Println("Db Behavior") +func db(ctx context.Context, app *app.App, wg *sync.WaitGroup) { + defer wg.Done() + for { + select { + case <-ctx.Done(): + return + default: + //Hier nach neuen Dateien suchen + err := app.AppendData(dbwriter.MyStruct{A: "bla", B: 4}) + if err != nil { + //todo + } + time.Sleep(time.Second * 2) + } } } func main() { - // Auf mdcat warten - app := app.NewApp(NewDir, ProcessedDir, Rights) - app.Run() + ctx, cancel := signal.NotifyContext( + context.Background(), + os.Interrupt, + syscall.SIGTERM, + ) + defer cancel() + var wg sync.WaitGroup + app := app.NewApp(NewDir, ProcessedDir, Rights, &wg) + wg.Add(1) + go db(ctx, app, &wg) + app.Run() + wg.Wait() } diff --git a/internal/app/app.go b/internal/app/app.go index 4bbe679..b02547a 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -5,7 +5,6 @@ import ( "os" "runtime" "sync" - "time" "gittea.marcokittel.de/elio/eliotools/datawriter/internal/dbwriter" "gittea.marcokittel.de/elio/eliotools/logger" @@ -13,26 +12,31 @@ import ( ) type App struct { - wg sync.WaitGroup + wg *sync.WaitGroup mu sync.Mutex log logger.Logger newFolderPath string processedFolderPath string lookupPath string dirCreationRights int - job func(dbwriter.DBWriter) - dbw dbwriter.DBWriter } -func NewApp(newFolderPath string, processedFolderPath string, dirCreationRights int) *App { +func NewApp(newFolderPath string, processedFolderPath string, dirCreationRights int, wg *sync.WaitGroup) *App { a := App{log: logger.NewMarcoLogger(), newFolderPath: newFolderPath, processedFolderPath: processedFolderPath, dirCreationRights: dirCreationRights, + wg: wg, } return &a } +func (a *App) AppendData(data dbwriter.MyStruct) error { + a.log.Info("Verarbeite Daten: " + data.A + " " + string(data.B)) + //Todo + return nil +} + func (a *App) Foo() { // x := tools.IsFilenameValid(filename) @@ -77,21 +81,5 @@ func (a *App) Run() { } a.log.Info("Applikation gestartet") - for { - time.Sleep(time.Second * 10) - a.log.Info("Oh yeah baby, wir nehmen fahrt auf...") - } - // Todo: - // for i := 0; i < 4; i++ { - // a.wg.Add(1) - // go func() { - // defer a.wg.Done() - - // ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Duration(1*time.Millisecond))) - // defer cancel() - - // }() - // } - // a.wg.Wait() }