mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Engine: new command 'stop' gracefully stops a container.
This commit is contained in:
parent
bef8de9319
commit
dbe1915fee
3 changed files with 34 additions and 16 deletions
13
api.go
13
api.go
|
@ -683,17 +683,14 @@ 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"])
|
||||
if t := r.Form.Get("t"); t != "" {
|
||||
job.Args = append(job.Args, t)
|
||||
}
|
||||
if err := job.Run(); err != nil {
|
||||
return err
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
|
|
|
@ -128,8 +128,7 @@ func TestCreateRmVolumes(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err = srv.ContainerStop(id, 1)
|
||||
if err != nil {
|
||||
if err := eng.Job("stop", id, "1").Run(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
@ -187,7 +186,7 @@ func TestCreateStartRestartStopStartKillRm(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := srv.ContainerStop(id, 15); err != nil {
|
||||
if err := eng.Job("stop", id, "15").Run(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
|
32
server.go
32
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
|
||||
|
@ -1713,16 +1717,34 @@ 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("Not enough arguments. Usage: %s CONTAINER TIMEOUT\n", job.Name)
|
||||
return engine.StatusErr
|
||||
}
|
||||
name := job.Args[0]
|
||||
var t uint64
|
||||
if len(job.Args) == 2 {
|
||||
var err error
|
||||
t, err = strconv.ParseUint(job.Args[1], 10, 32)
|
||||
if err != nil {
|
||||
job.Errorf("Invalid delay format: %s. Please provide an integer number of seconds.\n", job.Args[1])
|
||||
return engine.StatusErr
|
||||
}
|
||||
} else {
|
||||
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 {
|
||||
|
|
Loading…
Reference in a new issue