Making containers run and delete
This commit is contained in:
@@ -53,37 +53,50 @@ type Supervisor struct {
|
||||
workerGroup sync.WaitGroup
|
||||
}
|
||||
|
||||
// Run is a blocking call that runs the supervisor for monitoring contianer processes and
|
||||
// Start is a non-blocking call that runs the supervisor for monitoring contianer processes and
|
||||
// executing new containers.
|
||||
//
|
||||
// This event loop is the only thing that is allowed to modify state of containers and processes.
|
||||
func (s *Supervisor) Run(events chan Event) error {
|
||||
func (s *Supervisor) Start(events chan Event) error {
|
||||
if events == nil {
|
||||
return ErrEventChanNil
|
||||
}
|
||||
s.events = events
|
||||
for evt := range events {
|
||||
logrus.WithField("event", evt).Debug("containerd: processing event")
|
||||
switch e := evt.(type) {
|
||||
case *ExitEvent:
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"pid": e.Pid,
|
||||
"status": e.Status,
|
||||
}).Debug("containerd: process exited")
|
||||
if container, ok := s.processes[e.Pid]; ok {
|
||||
container.SetExited(e.Status)
|
||||
go func() {
|
||||
for evt := range events {
|
||||
logrus.WithField("event", evt).Debug("containerd: processing event")
|
||||
switch e := evt.(type) {
|
||||
case *ExitEvent:
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"pid": e.Pid,
|
||||
"status": e.Status,
|
||||
}).Debug("containerd: process exited")
|
||||
if container, ok := s.processes[e.Pid]; ok {
|
||||
container.SetExited(e.Status)
|
||||
if err := container.Delete(); err != nil {
|
||||
logrus.WithField("error", err).Error("containerd: deleting container")
|
||||
}
|
||||
delete(s.processes, e.Pid)
|
||||
delete(s.containers, container.ID())
|
||||
}
|
||||
case *StartedEvent:
|
||||
s.containers[e.ID] = e.Container
|
||||
pid, err := e.Container.Pid()
|
||||
if err != nil {
|
||||
logrus.WithField("error", err).Error("containerd: getting container pid")
|
||||
continue
|
||||
}
|
||||
s.processes[pid] = e.Container
|
||||
case *CreateContainerEvent:
|
||||
j := &CreateJob{
|
||||
ID: e.ID,
|
||||
BundlePath: e.BundlePath,
|
||||
Err: e.Err,
|
||||
}
|
||||
s.jobs <- j
|
||||
}
|
||||
case *StartedEvent:
|
||||
s.containers[e.ID] = e.Container
|
||||
case *CreateContainerEvent:
|
||||
j := &CreateJob{
|
||||
ID: e.ID,
|
||||
BundlePath: e.BundlePath,
|
||||
Err: e.Err,
|
||||
}
|
||||
s.jobs <- j
|
||||
}
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user