Close wait chan after sending status

This allows the caller to receive multiple times without blocking after
the first call.

This can be useful in cases like this:
```go
  ch, _  := task.Wait(ctx)
  defer func() { <-ch } // don't return until task is done
  task.Start(ctx)

  if err := doSomething(task); err != nil {
    return err
  }

  status := <- ch
  // do stuff with status
```

Since this channel is created in the `Wait()` calls and never accessible
outside, this should be safe.

Signed-off-by: Brian Goff <cpuguy83@gmail.com>
This commit is contained in:
Brian Goff 2017-10-13 12:37:23 -04:00
parent ef5fe56c24
commit 205625473f
2 changed files with 2 additions and 0 deletions

View File

@ -120,6 +120,7 @@ func (p *process) Kill(ctx context.Context, s syscall.Signal, opts ...KillOpts)
func (p *process) Wait(ctx context.Context) (<-chan ExitStatus, error) {
c := make(chan ExitStatus, 1)
go func() {
defer close(c)
r, err := p.task.client.TaskService().Wait(ctx, &tasks.WaitRequest{
ContainerID: p.task.id,
ExecID: p.id,

View File

@ -215,6 +215,7 @@ func (t *task) Status(ctx context.Context) (Status, error) {
func (t *task) Wait(ctx context.Context) (<-chan ExitStatus, error) {
c := make(chan ExitStatus, 1)
go func() {
defer close(c)
r, err := t.client.TaskService().Wait(ctx, &tasks.WaitRequest{
ContainerID: t.id,
})