Skip to content
This repository has been archived by the owner on Sep 24, 2021. It is now read-only.

Commit

Permalink
Merge pull request #114 from Crazykev/force-stop
Browse files Browse the repository at this point in the history
Enable force kill container if timeout
  • Loading branch information
feiskyer authored Mar 27, 2017
2 parents f76ca42 + f1aa6a4 commit e078437
Show file tree
Hide file tree
Showing 3 changed files with 253 additions and 258 deletions.
27 changes: 10 additions & 17 deletions pkg/hyper/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,12 @@ func (c *Client) RemoveContainer(containerID string) error {

// StopContainer stops a hyper container
func (c *Client) StopContainer(containerID string, timeout int64) error {
if timeout <= 0 {
return fmt.Errorf("Timeout can not be %d, it must be greater than zero.", timeout)
ctxTimeout := time.Duration(timeout) * time.Second
if ctxTimeout < hyperContextTimeout {
ctxTimeout = hyperContextTimeout
} else {
// extend 5s give hyperd enough time to response if timeout
ctxTimeout += time.Duration(5) * time.Second
}

// do checks about container status
Expand All @@ -250,23 +254,12 @@ func (c *Client) StopContainer(containerID string, timeout int64) error {
glog.V(3).Infof("Container %q is already stopped, skip", containerID)
return nil
}
ctx, cancel := getContextWithTimeout(ctxTimeout)
defer cancel()

ch := make(chan error, 1)

go func(containerID string) {
ctx, cancel := getContextWithTimeout(hyperContextTimeout)
defer cancel()

_, err := c.client.ContainerStop(ctx, &types.ContainerStopRequest{ContainerID: containerID})
ch <- err
}(containerID)
_, err = c.client.ContainerStop(ctx, &types.ContainerStopRequest{ContainerID: containerID, Timeout: timeout})

select {
case <-time.After(time.Duration(timeout) * time.Second):
return fmt.Errorf("Stop container %s timeout", containerID)
case err := <-ch:
return err
}
return err
}

// GetImageInfo gets the information of the image.
Expand Down
Loading

0 comments on commit e078437

Please sign in to comment.