2015-12-18 00:07:04 +00:00
|
|
|
package supervisor
|
2015-12-03 01:42:28 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2015-12-16 00:22:53 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-12-03 01:42:28 +00:00
|
|
|
"github.com/docker/containerd/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Worker interface {
|
|
|
|
Start()
|
|
|
|
}
|
|
|
|
|
2016-02-12 01:26:24 +00:00
|
|
|
type startTask struct {
|
2015-12-16 00:22:53 +00:00
|
|
|
Container runtime.Container
|
|
|
|
Checkpoint string
|
|
|
|
Stdin string
|
|
|
|
Stdout string
|
|
|
|
Stderr string
|
|
|
|
Err chan error
|
|
|
|
StartResponse chan StartResponse
|
2015-12-03 01:42:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewWorker(s *Supervisor, wg *sync.WaitGroup) Worker {
|
|
|
|
return &worker{
|
|
|
|
s: s,
|
|
|
|
wg: wg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type worker struct {
|
|
|
|
wg *sync.WaitGroup
|
|
|
|
s *Supervisor
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *worker) Start() {
|
|
|
|
defer w.wg.Done()
|
2016-02-17 18:55:54 +00:00
|
|
|
for t := range w.s.startTasks {
|
2016-02-01 23:07:02 +00:00
|
|
|
started := time.Now()
|
2016-02-11 20:20:29 +00:00
|
|
|
process, err := t.Container.Start(t.Checkpoint, runtime.NewStdio(t.Stdin, t.Stdout, t.Stderr))
|
2016-02-01 23:07:02 +00:00
|
|
|
if err != nil {
|
2016-02-25 20:59:34 +00:00
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"error": err,
|
|
|
|
"id": t.Container.ID(),
|
|
|
|
}).Error("containerd: start container")
|
|
|
|
t.Err <- err
|
2016-02-17 18:55:54 +00:00
|
|
|
evt := &DeleteTask{
|
2016-03-12 00:40:42 +00:00
|
|
|
ID: t.Container.ID(),
|
|
|
|
NoEvent: true,
|
2016-02-17 18:55:54 +00:00
|
|
|
}
|
2016-02-12 01:26:24 +00:00
|
|
|
w.s.SendTask(evt)
|
2016-02-01 23:07:02 +00:00
|
|
|
continue
|
2015-12-03 01:42:28 +00:00
|
|
|
}
|
2016-02-29 22:15:16 +00:00
|
|
|
if err := w.s.monitor.MonitorOOM(t.Container); err != nil && err != runtime.ErrContainerExited {
|
2016-04-07 00:39:13 +00:00
|
|
|
if process.State() != runtime.Stopped {
|
|
|
|
logrus.WithField("error", err).Error("containerd: notify OOM events")
|
|
|
|
}
|
2016-02-29 22:15:16 +00:00
|
|
|
}
|
2016-01-06 21:32:46 +00:00
|
|
|
if err := w.s.monitorProcess(process); err != nil {
|
|
|
|
logrus.WithField("error", err).Error("containerd: add process to monitor")
|
2015-12-16 00:22:53 +00:00
|
|
|
}
|
2015-12-03 01:42:28 +00:00
|
|
|
ContainerStartTimer.UpdateSince(started)
|
|
|
|
t.Err <- nil
|
2016-02-11 19:56:11 +00:00
|
|
|
t.StartResponse <- StartResponse{
|
|
|
|
Container: t.Container,
|
|
|
|
}
|
2016-02-12 01:26:24 +00:00
|
|
|
w.s.notifySubscribers(Event{
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
ID: t.Container.ID(),
|
|
|
|
Type: "start-container",
|
|
|
|
})
|
2015-12-03 01:42:28 +00:00
|
|
|
}
|
|
|
|
}
|