Handle term signals gracefully

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2016-03-21 12:54:05 -07:00
parent 577f9d7696
commit c28a87cbc1
3 changed files with 38 additions and 50 deletions

View File

@ -4,7 +4,9 @@ import (
"fmt" "fmt"
"net" "net"
"os" "os"
"os/signal"
"sync" "sync"
"syscall"
"time" "time"
"google.golang.org/grpc" "google.golang.org/grpc"
@ -14,6 +16,7 @@ import (
"github.com/docker/containerd" "github.com/docker/containerd"
"github.com/docker/containerd/api/grpc/server" "github.com/docker/containerd/api/grpc/server"
"github.com/docker/containerd/api/grpc/types" "github.com/docker/containerd/api/grpc/types"
"github.com/docker/containerd/osutils"
"github.com/docker/containerd/supervisor" "github.com/docker/containerd/supervisor"
) )
@ -80,7 +83,11 @@ func main() {
func daemon(address, stateDir string, concurrency int, runtimeName string) error { func daemon(address, stateDir string, concurrency int, runtimeName string) error {
// setup a standard reaper so that we don't leave any zombies if we are still alive // setup a standard reaper so that we don't leave any zombies if we are still alive
// this is just good practice because we are spawning new processes // this is just good practice because we are spawning new processes
go reapProcesses() s := make(chan os.Signal, 2048)
signal.Notify(s, syscall.SIGCHLD, syscall.SIGTERM, syscall.SIGINT)
if err := osutils.SetSubreaper(1); err != nil {
logrus.WithField("error", err).Error("containerd: set subpreaper")
}
sv, err := supervisor.New(stateDir, runtimeName) sv, err := supervisor.New(stateDir, runtimeName)
if err != nil { if err != nil {
return err return err
@ -94,17 +101,41 @@ func daemon(address, stateDir string, concurrency int, runtimeName string) error
if err := sv.Start(); err != nil { if err := sv.Start(); err != nil {
return err return err
} }
if err := os.RemoveAll(address); err != nil { server, err := startServer(address, sv)
return err
}
l, err := net.Listen(defaultListenType, address)
if err != nil { if err != nil {
return err return err
} }
for ss := range s {
switch ss {
case syscall.SIGCHLD:
if _, err := osutils.Reap(); err != nil {
logrus.WithField("error", err).Warn("containerd: reap child processes")
}
default:
server.Stop()
os.Exit(0)
}
}
return nil
}
func startServer(address string, sv *supervisor.Supervisor) (*grpc.Server, error) {
if err := os.RemoveAll(address); err != nil {
return nil, err
}
l, err := net.Listen(defaultListenType, address)
if err != nil {
return nil, err
}
s := grpc.NewServer() s := grpc.NewServer()
types.RegisterAPIServer(s, server.NewServer(sv)) types.RegisterAPIServer(s, server.NewServer(sv))
logrus.Debugf("containerd: grpc api on %s", address) go func() {
return s.Serve(l) logrus.Debugf("containerd: grpc api on %s", address)
if err := s.Serve(l); err != nil {
logrus.WithField("error", err).Fatal("containerd: serve grpc")
}
}()
return s, nil
} }
// getDefaultID returns the hostname for the instance host // getDefaultID returns the hostname for the instance host

View File

@ -4,7 +4,6 @@ import (
"log" "log"
"net" "net"
"os" "os"
"os/signal"
"runtime" "runtime"
"syscall" "syscall"
"time" "time"
@ -62,19 +61,6 @@ func checkLimits() error {
return nil return nil
} }
func reapProcesses() {
s := make(chan os.Signal, 2048)
signal.Notify(s, syscall.SIGCHLD)
if err := osutils.SetSubreaper(1); err != nil {
logrus.WithField("error", err).Error("containerd: set subpreaper")
}
for range s {
if _, err := osutils.Reap(); err != nil {
logrus.WithField("error", err).Error("containerd: reap child processes")
}
}
}
func processMetrics() { func processMetrics() {
var ( var (
g = metrics.NewGauge() g = metrics.NewGauge()

View File

@ -1,29 +0,0 @@
package main
import (
"os"
"github.com/codegangsta/cli"
)
var defaultStateDir = os.Getenv("PROGRAMDATA") + `\docker\containerd`
const (
defaultListenType = "tcp"
defaultGRPCEndpoint = "localhost:2377"
)
func appendPlatformFlags() {
}
// TODO Windows: May be able to factor out entirely
func checkLimits() error {
return nil
}
// No idea how to implement this on Windows.
func reapProcesses() {
}
func setAppBefore(app *cli.App) {
}