
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>
107 lines
2.1 KiB
Go
107 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/containerd/console"
|
|
"github.com/containerd/containerd"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/urfave/cli"
|
|
)
|
|
|
|
var taskExecCommand = cli.Command{
|
|
Name: "exec",
|
|
Usage: "execute additional processes in an existing container",
|
|
ArgsUsage: "CONTAINER CMD [ARG...]",
|
|
Flags: []cli.Flag{
|
|
cli.StringFlag{
|
|
Name: "cwd",
|
|
Usage: "working directory of the new process",
|
|
},
|
|
cli.BoolFlag{
|
|
Name: "tty,t",
|
|
Usage: "allocate a TTY for the container",
|
|
},
|
|
cli.StringFlag{
|
|
Name: "exec-id",
|
|
Usage: "exec specific id for the process",
|
|
},
|
|
},
|
|
Action: func(context *cli.Context) error {
|
|
var (
|
|
ctx, cancel = appContext(context)
|
|
id = context.Args().First()
|
|
args = context.Args().Tail()
|
|
tty = context.Bool("tty")
|
|
)
|
|
defer cancel()
|
|
|
|
if id == "" {
|
|
return errors.New("container id must be provided")
|
|
}
|
|
client, err := newClient(context)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
container, err := client.LoadContainer(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
spec, err := container.Spec()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
task, err := container.Task(ctx, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
pspec := spec.Process
|
|
pspec.Terminal = tty
|
|
pspec.Args = args
|
|
|
|
io := containerd.Stdio
|
|
if tty {
|
|
io = containerd.StdioTerminal
|
|
}
|
|
process, err := task.Exec(ctx, context.String("exec-id"), pspec, io)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer process.Delete(ctx)
|
|
|
|
statusC, err := task.Wait(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
var con console.Console
|
|
if tty {
|
|
con = console.Current()
|
|
defer con.Reset()
|
|
if err := con.SetRaw(); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if err := process.Start(ctx); err != nil {
|
|
return err
|
|
}
|
|
if tty {
|
|
if err := handleConsoleResize(ctx, process, con); err != nil {
|
|
logrus.WithError(err).Error("console resize")
|
|
}
|
|
} else {
|
|
sigc := forwardAllSignals(ctx, process)
|
|
defer stopCatch(sigc)
|
|
}
|
|
status := <-statusC
|
|
if status.Err != nil {
|
|
return status.Err
|
|
}
|
|
if status.Code != 0 {
|
|
return cli.NewExitError("", int(status.Code))
|
|
}
|
|
return nil
|
|
},
|
|
}
|