Skip to content

Commit

Permalink
initiate graceful shutdown after SIGTERM (cadence-workflow#2881)
Browse files Browse the repository at this point in the history
* initiate graceful shutdown after SIGTERM

* move import

* update shutdown sequence

* use select to handle signals
  • Loading branch information
Vitaly authored Dec 4, 2019
1 parent 2ea7256 commit bf93d90
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
22 changes: 19 additions & 3 deletions cmd/server/cadence.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,20 @@
package main

import (
"github.com/uber/cadence/common"
"log"
"os"
"os/signal"
"strings"

"github.com/urfave/cli"
"syscall"

"github.com/uber/cadence/common/service/config"
"github.com/uber/cadence/tools/cassandra"
"github.com/uber/cadence/tools/sql"
_ "github.com/uber/cadence/tools/sql-extensions/mysql" // needed to load mysql extensions
_ "github.com/uber/cadence/tools/sql-extensions/postgres" // needed to load postgres extensions

"github.com/urfave/cli"
)

// validServices is the list of all valid cadence services
Expand Down Expand Up @@ -72,16 +75,29 @@ func startHandler(c *cli.Context) {
log.Fatal("Incompatible sql versions: ", err)
}

var daemons []common.Daemon
services := getServices(c)
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, syscall.SIGTERM)
for _, svc := range services {
if _, ok := cfg.Services[svc]; !ok {
log.Fatalf("`%v` service missing config", svc)
}
server := newServer(svc, &cfg)
daemons = append(daemons, server)
server.Start()
}

select {}
select {
case <-sigc:
{
log.Println("Received SIGTERM signal, initiating shutdown.")
for _, daemon := range daemons {
daemon.Stop()
}
os.Exit(0)
}
}
}

func getEnvironment(c *cli.Context) string {
Expand Down
1 change: 1 addition & 0 deletions service/history/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ func (s *Service) Start() {
log.Info("started", tag.Service(common.HistoryServiceName))

<-s.stopC
handler.Stop()
base.Stop()
}

Expand Down
1 change: 1 addition & 0 deletions service/matching/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func (s *Service) Start() {

logger.Info("matching started", tag.Service(common.MatchingServiceName))
<-s.stopC
handler.Stop()
s.Resource.Stop()
}

Expand Down

0 comments on commit bf93d90

Please sign in to comment.