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 package main
import ( import (
"fmt" "context"
"os"
"os/signal"
"sync"
"syscall"
"time"
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/app" "gittea.marcokittel.de/elio/eliotools/datawriter/internal/app"
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/dbwriter" "gittea.marcokittel.de/elio/eliotools/datawriter/internal/dbwriter"
@ -14,19 +19,35 @@ const (
StockMustHaveFileLen = 38 StockMustHaveFileLen = 38
) )
type dbfunction func(data dbwriter.DBWriter) func db(ctx context.Context, app *app.App, wg *sync.WaitGroup) {
defer wg.Done()
var filename = "2023-11-09T15:02:17+00:00-CH-stock.csv" for {
select {
func GetDbBehavior() dbfunction { case <-ctx.Done():
return func(data dbwriter.DBWriter) { return
fmt.Println("Db Behavior") 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() { 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" "os"
"runtime" "runtime"
"sync" "sync"
"time"
"gittea.marcokittel.de/elio/eliotools/datawriter/internal/dbwriter" "gittea.marcokittel.de/elio/eliotools/datawriter/internal/dbwriter"
"gittea.marcokittel.de/elio/eliotools/logger" "gittea.marcokittel.de/elio/eliotools/logger"
@ -13,26 +12,31 @@ import (
) )
type App struct { type App struct {
wg sync.WaitGroup wg *sync.WaitGroup
mu sync.Mutex mu sync.Mutex
log logger.Logger log logger.Logger
newFolderPath string newFolderPath string
processedFolderPath string processedFolderPath string
lookupPath string lookupPath string
dirCreationRights int 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(), a := App{log: logger.NewMarcoLogger(),
newFolderPath: newFolderPath, newFolderPath: newFolderPath,
processedFolderPath: processedFolderPath, processedFolderPath: processedFolderPath,
dirCreationRights: dirCreationRights, dirCreationRights: dirCreationRights,
wg: wg,
} }
return &a 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() { func (a *App) Foo() {
// x := tools.IsFilenameValid(filename) // x := tools.IsFilenameValid(filename)
@ -77,21 +81,5 @@ func (a *App) Run() {
} }
a.log.Info("Applikation gestartet") 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()
} }