Skip to content

Commit

Permalink
Ensure watching of db document changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AmanAgnihotri committed Apr 14, 2024
1 parent 57f4f93 commit c308004
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions lib/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ package lib
import (
"context"
"log"
"os"
"os/signal"
"sync"
"syscall"

"ttt/internal/base"
"ttt/internal/game"
Expand All @@ -24,6 +28,8 @@ func Configure(appID string, db *db.Context, jwt *jwt.Context, mux *http.Mux) {
user.Configure(baseWire)

ensureIndexes(baseWire)

go ensureWatchers(baseWire)
}

func ensureIndexes(base base.Wire) {
Expand All @@ -41,3 +47,32 @@ func ensureIndexes(base base.Wire) {
}
}
}

func ensureWatchers(base base.Wire) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

var watchers = []db.Watcher{
base.AppStore(),
}

var waitGroup sync.WaitGroup

done := make(chan os.Signal, 1)
signal.Notify(done, syscall.SIGINT, syscall.SIGTERM)

for _, watcher := range watchers {
waitGroup.Add(1)

go func(w db.Watcher) {
defer waitGroup.Done()

err := w.Watch(ctx)
log.Println(err)
}(watcher)
}

<-done
cancel()
waitGroup.Wait()
}

0 comments on commit c308004

Please sign in to comment.