Skip to content

Commit

Permalink
fix(api): safely stop engine (#5403)
Browse files Browse the repository at this point in the history
* fix(api): safely stop engine

* fix: race condition and concurrent map writes
  • Loading branch information
richardlt authored Sep 2, 2020
1 parent 71c9e47 commit 7ed8d51
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions engine/cmd_start.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os/signal"
"sort"
"strings"
"sync"
"syscall"
"time"

Expand Down Expand Up @@ -275,8 +276,10 @@ See $ engine config command for more details.
return serviceConfs[i].arg < serviceConfs[j].arg
})

var wg sync.WaitGroup
//Configure the services
for _, s := range serviceConfs {
for i := range serviceConfs {
s := serviceConfs[i]
if err := s.service.ApplyConfiguration(s.cfg); err != nil {
sdk.Exit("Unable to init service %s: %v", s.arg, err)
}
Expand All @@ -294,14 +297,20 @@ See $ engine config command for more details.
sdk.Exit("Unable to start tracing exporter: %v", err)
}

go start(ctx, s.service, s.cfg, s.arg)
wg.Add(1)
go func(srv serviceConf) {
start(ctx, srv.service, srv.cfg, srv.arg)
wg.Done()
}(s)

// Stupid trick: when API is starting wait a bit before start the other
if s.arg == "API" || s.arg == "api" {
time.Sleep(2 * time.Second)
}
}

wg.Wait()

//Wait for the end
<-ctx.Done()
if ctx.Err() != nil {
Expand All @@ -312,7 +321,7 @@ See $ engine config command for more details.

func start(c context.Context, s service.Service, cfg interface{}, serviceName string) {
if err := serve(c, s, serviceName, cfg); err != nil {
sdk.Exit("Service has been stopped: %s %+v", serviceName, err)
fmt.Printf("Service has been stopped: %s %+v", serviceName, err)
}
}

Expand Down

0 comments on commit 7ed8d51

Please sign in to comment.