execution: "restore" container on service creation
Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
This commit is contained in:
parent
73cb78fae3
commit
0fdd2469f6
8 changed files with 90 additions and 36 deletions
|
@ -2,6 +2,7 @@ package execution
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
|
@ -16,10 +17,47 @@ var (
|
|||
emptyResponse = &google_protobuf.Empty{}
|
||||
)
|
||||
|
||||
func New(executor Executor) (*Service, error) {
|
||||
return &Service{
|
||||
func New(ctx context.Context, executor Executor) (*Service, error) {
|
||||
svc := &Service{
|
||||
executor: executor,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// List existing container, some of them may have died away if
|
||||
// we've been restarted
|
||||
containers, err := executor.List(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, c := range containers {
|
||||
status := c.Status()
|
||||
if status == Stopped || status == Deleted {
|
||||
// generate exit event for all processes, (generate event for init last)
|
||||
processes := c.Processes()
|
||||
processes = append(processes[1:], processes[0])
|
||||
for _, p := range c.Processes() {
|
||||
if p.Status() != Stopped {
|
||||
p.Signal(os.Kill)
|
||||
}
|
||||
sc, err := p.Wait()
|
||||
if err != nil {
|
||||
sc = UnknownStatusCode
|
||||
}
|
||||
topic := GetContainerProcessEventTopic(c.ID(), p.ID())
|
||||
svc.publishEvent(ctx, topic, &ContainerExitEvent{
|
||||
ContainerEvent: ContainerEvent{
|
||||
Timestamp: time.Now(),
|
||||
ID: c.ID(),
|
||||
Action: "exit",
|
||||
},
|
||||
PID: p.ID(),
|
||||
StatusCode: sc,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return svc, nil
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue