func containerEvents(c types.APIClient, container string) <-chan *types.Event { evChan := make(chan *types.Event) ts := time.Now() go func() { for { tsp, err := ptypes.TimestampProto(ts) if err != nil { close(evChan) return } events, err := c.Events(netcontext.Background(), &types.EventsRequest{Timestamp: tsp}) if err != nil { fmt.Printf("c.Events error: %v", err) // TODO try to find a way to kill the process ? close(evChan) return } for { e, err := events.Recv() if err != nil { time.Sleep(1 * time.Second) break } ts, err = ptypes.Timestamp(e.Timestamp) if e.Id == container { evChan <- e } } } }() return evChan }
func waitForExit(c types.APIClient, events types.API_EventsClient, id, pid string, closer func()) { timestamp := time.Now() for { e, err := events.Recv() if err != nil { if grpc.ErrorDesc(err) == transport.ErrConnClosing.Desc { closer() os.Exit(128 + int(syscall.SIGHUP)) } time.Sleep(1 * time.Second) tsp, err := ptypes.TimestampProto(timestamp) if err != nil { closer() fmt.Fprintf(os.Stderr, "%s", err.Error()) os.Exit(1) } events, _ = c.Events(netcontext.Background(), &types.EventsRequest{Timestamp: tsp}) continue } timestamp, err = ptypes.Timestamp(e.Timestamp) if e.Id == id && e.Type == "exit" && e.Pid == pid { closer() os.Exit(int(e.Status)) } } }
func waitForExit(c types.APIClient, events types.API_EventsClient, id, pid string, closer func()) { timestamp := uint64(time.Now().Unix()) for { e, err := events.Recv() if err != nil { time.Sleep(1 * time.Second) events, _ = c.Events(netcontext.Background(), &types.EventsRequest{Timestamp: timestamp}) continue } timestamp = e.Timestamp if e.Id == id && e.Type == "exit" && e.Pid == pid { closer() os.Exit(int(e.Status)) } } }