Implement proper shutdown logic
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
parent
a10aa91051
commit
c10b3cde9f
6 changed files with 67 additions and 20 deletions
|
@ -325,10 +325,14 @@ func (s *server) createCheckpoint(w http.ResponseWriter, r *http.Request) {
|
|||
name = vars["name"]
|
||||
)
|
||||
var cp Checkpoint
|
||||
// most options to the checkpoint action can be left out so don't
|
||||
// decode unless the client passed anything in the body.
|
||||
if r.ContentLength > 0 {
|
||||
if err := json.NewDecoder(r.Body).Decode(&cp); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
e := containerd.NewEvent(containerd.CreateCheckpointEventType)
|
||||
e.ID = id
|
||||
e.Checkpoint = &runtime.Checkpoint{
|
||||
|
|
|
@ -86,9 +86,12 @@ func daemon(stateDir string, concurrency, bufferSize int) error {
|
|||
w := containerd.NewWorker(supervisor, wg)
|
||||
go w.Start()
|
||||
}
|
||||
// only set containerd as the subreaper if it is not an init process
|
||||
if pid := os.Getpid(); pid != 1 {
|
||||
if err := setSubReaper(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// start the signal handler in the background.
|
||||
go startSignalHandler(supervisor, bufferSize)
|
||||
if err := supervisor.Start(); err != nil {
|
||||
|
|
|
@ -34,8 +34,7 @@ func startSignalHandler(supervisor *containerd.Supervisor, bufferSize int) {
|
|||
for s := range signals {
|
||||
switch s {
|
||||
case syscall.SIGTERM, syscall.SIGINT:
|
||||
supervisor.Close()
|
||||
os.Exit(0)
|
||||
supervisor.Stop(signals)
|
||||
case syscall.SIGCHLD:
|
||||
exits, err := reap()
|
||||
if err != nil {
|
||||
|
@ -46,6 +45,8 @@ func startSignalHandler(supervisor *containerd.Supervisor, bufferSize int) {
|
|||
}
|
||||
}
|
||||
}
|
||||
supervisor.Close()
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func reap() (exits []*containerd.Event, err error) {
|
||||
|
|
|
@ -15,6 +15,7 @@ func (h *DeleteEvent) Handle(e *Event) error {
|
|||
logrus.WithField("error", err).Error("containerd: deleting container")
|
||||
}
|
||||
ContainersCounter.Dec(1)
|
||||
h.s.containerGroup.Done()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -2,8 +2,11 @@ package containerd
|
|||
|
||||
import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
goruntime "runtime"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/docker/containerd/runtime"
|
||||
|
@ -66,14 +69,48 @@ type Supervisor struct {
|
|||
tasks chan *StartTask
|
||||
subscribers map[subscriber]bool
|
||||
machine Machine
|
||||
containerGroup sync.WaitGroup
|
||||
}
|
||||
|
||||
type subscriber chan *Event
|
||||
|
||||
// need proper close logic for jobs and stuff so that sending to the channels dont panic
|
||||
// but can complete jobs
|
||||
func (s *Supervisor) Stop(sig chan os.Signal) {
|
||||
// Close the tasks channel so that no new containers get started
|
||||
close(s.tasks)
|
||||
// send a SIGTERM to all containers
|
||||
for id, c := range s.containers {
|
||||
logrus.WithField("id", id).Debug("sending TERM to container processes")
|
||||
procs, err := c.Processes()
|
||||
if err != nil {
|
||||
logrus.WithField("id", id).Warn("get container processes")
|
||||
continue
|
||||
}
|
||||
if len(procs) == 0 {
|
||||
continue
|
||||
}
|
||||
mainProc := procs[0]
|
||||
if err := mainProc.Signal(syscall.SIGTERM); err != nil {
|
||||
pid, _ := mainProc.Pid()
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"id": id,
|
||||
"pid": pid,
|
||||
"error": err,
|
||||
}).Error("send SIGTERM to process")
|
||||
}
|
||||
}
|
||||
go func() {
|
||||
logrus.Debug("waiting for containers to exit")
|
||||
s.containerGroup.Wait()
|
||||
logrus.Debug("all containers exited")
|
||||
// stop receiving signals and close the channel
|
||||
signal.Stop(sig)
|
||||
close(sig)
|
||||
}()
|
||||
}
|
||||
|
||||
// Close closes any open files in the supervisor but expects that Stop has been
|
||||
// callsed so that no more containers are started.
|
||||
func (s *Supervisor) Close() error {
|
||||
//TODO: unsubscribe all channels
|
||||
return s.journal.Close()
|
||||
}
|
||||
|
||||
|
|
|
@ -55,6 +55,7 @@ func (w *worker) Start() {
|
|||
continue
|
||||
}
|
||||
}
|
||||
w.s.containerGroup.Add(1)
|
||||
ContainerStartTimer.UpdateSince(started)
|
||||
t.Err <- nil
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue