Make Wait() async
In all of the examples, its recommended to call `Wait()` before starting a process/task. Since `Wait()` is a blocking call, this means it must be called from a goroutine like so: ```go statusC := make(chan uint32) go func() { status, err := task.Wait(ctx) if err != nil { // handle async err } statusC <- status }() task.Start(ctx) <-statusC ``` This means there is a race here where there is no guarentee when the goroutine is going to be scheduled, and even a bit more since this requires an RPC call to be made. In addition, this code is very messy and a common pattern for any caller using Wait+Start. Instead, this changes `Wait()` to use an async model having `Wait()` return a channel instead of the code itself. This ensures that when `Wait()` returns that the client has a handle on the event stream (already made the RPC request) before returning and reduces any sort of race to how the stream is handled by grpc since we can't guarentee that we have a goroutine running and blocked on `Recv()`. Making `Wait()` async also cleans up the code in the caller drastically: ```go statusC, err := task.Wait(ctx) if err != nil { return err } task.Start(ctx) status := <-statusC if status.Err != nil { return err } ``` No more spinning up goroutines and more natural error handling for the caller. Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
51
task.go
51
task.go
@@ -201,15 +201,18 @@ func (t *task) Status(ctx context.Context) (Status, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (t *task) Wait(ctx context.Context) (uint32, error) {
|
||||
func (t *task) Wait(ctx context.Context) (<-chan ExitStatus, error) {
|
||||
cancellable, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
eventstream, err := t.client.EventService().Subscribe(cancellable, &eventsapi.SubscribeRequest{
|
||||
Filters: []string{"topic==" + runtime.TaskExitEventTopic},
|
||||
})
|
||||
if err != nil {
|
||||
return UnknownExitStatus, errdefs.FromGRPC(err)
|
||||
cancel()
|
||||
return nil, errdefs.FromGRPC(err)
|
||||
}
|
||||
|
||||
chStatus := make(chan ExitStatus, 1)
|
||||
|
||||
t.mu.Lock()
|
||||
checkpoint := t.deferred != nil
|
||||
t.mu.Unlock()
|
||||
@@ -217,28 +220,42 @@ func (t *task) Wait(ctx context.Context) (uint32, error) {
|
||||
// first check if the task has exited
|
||||
status, err := t.Status(ctx)
|
||||
if err != nil {
|
||||
return UnknownExitStatus, errdefs.FromGRPC(err)
|
||||
cancel()
|
||||
return nil, errdefs.FromGRPC(err)
|
||||
}
|
||||
if status.Status == Stopped {
|
||||
return status.ExitStatus, nil
|
||||
cancel()
|
||||
chStatus <- ExitStatus{Code: status.ExitStatus, ExitedAt: status.ExitTime}
|
||||
return chStatus, nil
|
||||
}
|
||||
}
|
||||
for {
|
||||
evt, err := eventstream.Recv()
|
||||
if err != nil {
|
||||
return UnknownExitStatus, errdefs.FromGRPC(err)
|
||||
}
|
||||
if typeurl.Is(evt.Event, &eventsapi.TaskExit{}) {
|
||||
v, err := typeurl.UnmarshalAny(evt.Event)
|
||||
|
||||
go func() {
|
||||
defer cancel()
|
||||
chStatus <- ExitStatus{} // signal that goroutine is running
|
||||
for {
|
||||
evt, err := eventstream.Recv()
|
||||
if err != nil {
|
||||
return UnknownExitStatus, err
|
||||
chStatus <- ExitStatus{Code: UnknownExitStatus, Err: errdefs.FromGRPC(err)}
|
||||
return
|
||||
}
|
||||
e := v.(*eventsapi.TaskExit)
|
||||
if e.ContainerID == t.id && e.Pid == t.pid {
|
||||
return e.ExitStatus, nil
|
||||
if typeurl.Is(evt.Event, &eventsapi.TaskExit{}) {
|
||||
v, err := typeurl.UnmarshalAny(evt.Event)
|
||||
if err != nil {
|
||||
chStatus <- ExitStatus{Code: UnknownExitStatus, Err: err}
|
||||
return
|
||||
}
|
||||
e := v.(*eventsapi.TaskExit)
|
||||
if e.ContainerID == t.id && e.Pid == t.pid {
|
||||
chStatus <- ExitStatus{Code: e.ExitStatus, ExitedAt: e.ExitedAt}
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
<-chStatus // wait for the goroutine to be running
|
||||
return chStatus, nil
|
||||
}
|
||||
|
||||
// Delete deletes the task and its runtime state
|
||||
|
Reference in New Issue
Block a user