Skip to content

Commit

Permalink
add daemon ready (#62)
Browse files Browse the repository at this point in the history
* add daemon ready

* refactor service startup sequence
  • Loading branch information
dmolina79 authored Jun 23, 2020
1 parent eaa3654 commit 6962060
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
13 changes: 9 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app

import (
"context"
"fmt"
"log"
"os"
"os/signal"
Expand Down Expand Up @@ -115,6 +116,13 @@ func Start(ctx context.Context, cfg config.Config, env env.SpaceEnv) {
grpc.WithPort(cfg.GetInt(config.SpaceServerPort, 0)),
)

g.Go(func() error {
sync.RegisterNotifier(srv)
return sync.Start(ctx)
})

<-sync.WaitForReady()

// start the gRPC server
g.Go(func() error {
if svErr != nil {
Expand All @@ -124,10 +132,7 @@ func Start(ctx context.Context, cfg config.Config, env env.SpaceEnv) {
return srv.Start(ctx)
})

g.Go(func() error {
sync.RegisterNotifier(srv)
return sync.Start(ctx)
})
fmt.Println("daemon ready")

// wait for interruption or done signal
select {
Expand Down
15 changes: 15 additions & 0 deletions core/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type GrpcNotifier interface {
}

type BucketSynchronizer interface {
WaitForReady() chan bool
Start(ctx context.Context) error
Stop()
RegisterNotifier(notifier GrpcNotifier)
Expand Down Expand Up @@ -62,6 +63,7 @@ type bucketSynchronizer struct {
textileThreadListeners []textile.ThreadListener
notifier GrpcNotifier
store store.Store
ready chan bool
}

// Creates a new bucketSynchronizer instancelistenerEventHandler
Expand All @@ -81,6 +83,7 @@ func New(
textileThreadListeners: textileThreadListeners,
notifier: notifier,
store: store,
ready: make(chan bool),
}
}

Expand Down Expand Up @@ -151,15 +154,25 @@ func (bs *bucketSynchronizer) Start(ctx context.Context) error {
}
}

go func() {
bs.ready <- true
}()

err = g.Wait()

if err != nil {
return err
}



return nil
}

func (bs *bucketSynchronizer) WaitForReady() chan bool {
return bs.ready
}

func (bs *bucketSynchronizer) Stop() {
// add shutdown logic here
log.Debug("shutting down folder watcher in bucketsync")
Expand All @@ -168,6 +181,8 @@ func (bs *bucketSynchronizer) Stop() {
for _, listener := range bs.textileThreadListeners {
listener.Close()
}

close(bs.ready)
}

func (bs *bucketSynchronizer) RegisterNotifier(notifier GrpcNotifier) {
Expand Down

0 comments on commit 6962060

Please sign in to comment.