diff --git a/api.go b/api.go index faa6d3695f..f7e37ef07c 100644 --- a/api.go +++ b/api.go @@ -678,17 +678,12 @@ func postContainersStop(srv *Server, version float64, w http.ResponseWriter, r * if err := parseForm(r); err != nil { return err } - t, err := strconv.Atoi(r.Form.Get("t")) - if err != nil || t < 0 { - t = 10 - } - if vars == nil { return fmt.Errorf("Missing parameter") } - name := vars["name"] - - if err := srv.ContainerStop(name, t); err != nil { + job := srv.Eng.Job("stop", vars["name"]) + job.Setenv("t", r.Form.Get("t")) + if err := job.Run(); err != nil { return err } w.WriteHeader(http.StatusNoContent) diff --git a/engine/job.go b/engine/job.go index 478f06cd60..465086af91 100644 --- a/engine/job.go +++ b/engine/job.go @@ -24,7 +24,7 @@ type Job struct { Eng *Engine Name string Args []string - env *Env + env *Env Stdout *Output Stderr *Output Stdin *Input diff --git a/engine/streams.go b/engine/streams.go index ee26fe5f1e..7cd4a60cf7 100644 --- a/engine/streams.go +++ b/engine/streams.go @@ -168,7 +168,7 @@ func Tail(src io.Reader, n int, dst *[]string) { // AddEnv starts a new goroutine which will decode all subsequent data // as a stream of json-encoded objects, and point `dst` to the last // decoded object. -// The result `env` can be queried using the type-neutral Env interface. +// The result `env` can be queried using the type-neutral Env interface. // It is not safe to query `env` until the Output is closed. func (o *Output) AddEnv() (dst *Env, err error) { src, err := o.AddPipe() @@ -185,9 +185,8 @@ func (o *Output) AddEnv() (dst *Env, err error) { if err != nil { return } - *dst= *env + *dst = *env } }() return dst, nil } - diff --git a/integration/server_test.go b/integration/server_test.go index 9cb26a4c2d..7fb7611192 100644 --- a/integration/server_test.go +++ b/integration/server_test.go @@ -129,8 +129,9 @@ func TestCreateRmVolumes(t *testing.T) { t.Fatal(err) } - err = srv.ContainerStop(id, 1) - if err != nil { + job = eng.Job("stop", id) + job.SetenvInt("t", 1) + if err := job.Run(); err != nil { t.Fatal(err) } @@ -188,7 +189,9 @@ func TestCreateStartRestartStopStartKillRm(t *testing.T) { t.Fatal(err) } - if err := srv.ContainerStop(id, 15); err != nil { + job = eng.Job("stop", id) + job.SetenvInt("t", 15) + if err := job.Run(); err != nil { t.Fatal(err) } diff --git a/server.go b/server.go index 1d716114eb..63643cb20e 100644 --- a/server.go +++ b/server.go @@ -79,6 +79,10 @@ func jobInitApi(job *engine.Job) engine.Status { job.Error(err) return engine.StatusErr } + if err := job.Eng.Register("stop", srv.ContainerStop); err != nil { + job.Error(err) + return engine.StatusErr + } if err := job.Eng.Register("start", srv.ContainerStart); err != nil { job.Error(err) return engine.StatusErr @@ -1727,16 +1731,27 @@ func (srv *Server) ContainerStart(job *engine.Job) engine.Status { return engine.StatusOK } -func (srv *Server) ContainerStop(name string, t int) error { +func (srv *Server) ContainerStop(job *engine.Job) engine.Status { + if len(job.Args) != 1 { + job.Errorf("Usage: %s CONTAINER\n", job.Name) + return engine.StatusErr + } + name := job.Args[0] + t := job.GetenvInt("t") + if t == -1 { + t = 10 + } if container := srv.runtime.Get(name); container != nil { - if err := container.Stop(t); err != nil { - return fmt.Errorf("Cannot stop container %s: %s", name, err) + if err := container.Stop(int(t)); err != nil { + job.Errorf("Cannot stop container %s: %s\n", name, err) + return engine.StatusErr } srv.LogEvent("stop", container.ID, srv.runtime.repositories.ImageName(container.Image)) } else { - return fmt.Errorf("No such container: %s", name) + job.Errorf("No such container: %s\n", name) + return engine.StatusErr } - return nil + return engine.StatusOK } func (srv *Server) ContainerWait(job *engine.Job) engine.Status {