Add graceful shutdown support for async runners (#125)

This commit is contained in:
C Cirello
2016-10-06 00:32:56 +02:00
committed by GitHub
parent 1e62c2a403
commit aa12f3c724
3 changed files with 78 additions and 22 deletions

21
main.go
View File

@@ -3,7 +3,9 @@ package main
import (
"fmt"
"os"
"os/signal"
"strings"
"sync"
log "github.com/Sirupsen/logrus"
"github.com/iron-io/functions/api/datastore"
@@ -38,7 +40,14 @@ func init() {
}
func main() {
ctx := context.Background()
ctx, halt := context.WithCancel(context.Background())
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
log.Info("Halting...")
halt()
}()
ds, err := datastore.New(viper.GetString("DB"))
if err != nil {
@@ -57,10 +66,14 @@ func main() {
tasksURL, port, nasync := viper.GetString("tasks_url"), viper.GetString("port"), viper.GetInt("nasync")
log.Info("async workers:", nasync)
for i := 0; i < nasync; i++ {
go runner.RunAsyncRunner(tasksURL, port)
var wgAsync sync.WaitGroup
if nasync > 0 {
wgAsync.Add(1)
go runner.RunAsyncRunner(ctx, &wgAsync, tasksURL, port, nasync)
}
srv := server.New(ds, mqType, rnr)
srv.Run(ctx)
go srv.Run(ctx)
<-ctx.Done()
wgAsync.Wait()
}