Commit 5b9cb8d9 authored by Sietse Ringers's avatar Sietse Ringers
Browse files

Do graceful shutdown on ctrl-c in irma server

parent 4c02373c
......@@ -2,8 +2,10 @@ package cmd
import (
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"github.com/go-errors/errors"
"github.com/mitchellh/mapstructure"
......@@ -28,8 +30,31 @@ var RootCommand = &cobra.Command{
if err != nil {
die(errors.WrapPrefix(err, "Failed to configure server", 0))
}
if err := serv.Start(conf); err != nil {
die(errors.WrapPrefix(err, "Failed to start server", 0))
stopped := make(chan struct{})
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
go func() {
if err := serv.Start(conf); err != nil {
die(errors.WrapPrefix(err, "Failed to start server", 0))
}
conf.Logger.Debug("Server stopped")
stopped <- struct{}{}
}()
for {
select {
case <-interrupt:
conf.Logger.Debug("Caught interrupt")
serv.Stop() // causes serv.Start() above to return
conf.Logger.Debug("Sent stop signal to server")
case <-stopped:
conf.Logger.Info("Exiting")
close(stopped)
close(interrupt)
return
}
}
},
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment