Einbau von Goroutine, Waitgroups und Context

This commit is contained in:
Marco Kittel 2025-06-23 00:39:39 +02:00
parent a85d886baf
commit 62776f0633
2 changed files with 41 additions and 32 deletions

View File

@ -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()
}

View File

@ -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()
}