Refactor is-running check so we can re-use more of the code

This commit is contained in:
Bryan Boreham committed 2015-06-12 17:09:11 +01:00
1 parent 5aafbbeff0
commit 13944f3811
2 files changed
+18 -22

No files matched your search

+15 -10
View File
@@ -60,16 +60,21 @@ func (c *Client) AddObserver(ob ContainerObserver) error {
return nil
}
// IsContainerRunning returns true if the container is up and running
func (c *Client) IsContainerRunning(idStr string) (bool, error) {
// check with Docker whether the container is really running
container, err := c.InspectContainer(idStr)
if err != nil {
if _, notThere := err.(*docker.NoSuchContainer); notThere {
return false, nil
}
return false, err
// IsContainerNotRunning returns true if we have checked with Docker that the ID is not running
func (c *Client) IsContainerNotRunning(idStr string) bool {
if !IsContainerID(idStr) {
Debug.Printf("[docker] '%s' does not seem to be a container id", idStr)
return false
}
return container.State.Running, nil
// check with Docker whether the container is really running
container, err := c.InspectContainer(idStr)
if err == nil {
return !container.State.Running
}
if _, notThere := err.(*docker.NoSuchContainer); notThere {
return true
}
Error.Printf("[docker] Could not check container status: %s", err)
return false
}
+3 -12
View File
@@ -75,18 +75,9 @@ func ServeHTTP(listener net.Listener, version string, server *DNSServer, dockerC
} // oh, I already know this. whatever.
}
if dockerCli != nil {
if !docker.IsContainerID(idStr) {
Debug.Printf("[http] '%s' does not seem to be a container id", idStr)
return
}
if running, err := dockerCli.IsContainerRunning(idStr); err != nil {
Error.Printf("[http] Could not check container status: %s", err)
} else if !running {
Info.Printf("[http] '%s' is not running: removing", idStr)
server.Zone.DeleteRecords(idStr, name, ip)
}
if dockerCli != nil && dockerCli.IsContainerNotRunning(idStr) {
Info.Printf("[http] '%s' is not running: removing", idStr)
server.Zone.DeleteRecords(idStr, name, ip)
}
})