Skip to content

Commit

Permalink
Don't panic when docker client cannot be obtained.
Browse files Browse the repository at this point in the history
Make close session faster by deleting instances in parallel.
  • Loading branch information
xetorthio committed Aug 11, 2017
1 parent 0bc0342 commit 7f17a7e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 18 deletions.
8 changes: 7 additions & 1 deletion pwd/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,13 @@ func (p *pwd) InstanceNew(session *types.Session, conf types.InstanceConfig) (*t

func (p *pwd) InstanceExec(instance *types.Instance, cmd []string) (int, error) {
defer observeAction("InstanceExec", time.Now())
return p.docker(instance.SessionId).Exec(instance.Name, cmd)

dockerClient, err := p.dockerFactory.GetForSession(instance.SessionId)
if err != nil {
log.Println(err)
return -1, err
}
return dockerClient.Exec(instance.Name, cmd)
}

func (p *pwd) InstanceAllowedImages() []string {
Expand Down
8 changes: 0 additions & 8 deletions pwd/pwd.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,6 @@ func (p *pwd) getProvisioner(t string) (provisioner.ProvisionerApi, error) {
}
}

func (p *pwd) docker(sessionId string) docker.DockerApi {
d, err := p.dockerFactory.GetForSession(sessionId)
if err != nil {
panic("Should not have got here. Session always need to be validated before calling this.")
}
return d
}

func (p *pwd) setGauges() {
s, _ := p.storage.SessionCount()
ses := float64(s)
Expand Down
43 changes: 34 additions & 9 deletions pwd/session.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pwd

import (
"context"
"fmt"
"log"
"math"
Expand All @@ -11,6 +12,8 @@ import (
"sync"
"time"

"golang.org/x/sync/errgroup"

"github.com/play-with-docker/play-with-docker/config"
"github.com/play-with-docker/play-with-docker/docker"
"github.com/play-with-docker/play-with-docker/event"
Expand Down Expand Up @@ -63,7 +66,11 @@ func (p *pwd) SessionNew(duration time.Duration, stack, stackName, imageName str

log.Printf("NewSession id=[%s]\n", s.Id)

dockerClient := p.docker(s.Id)
dockerClient, err := p.dockerFactory.GetForSession(s.Id)
if err != nil {
// We assume we are out of capacity
return nil, fmt.Errorf("Out of capacity")
}
u, _ := url.Parse(dockerClient.GetDaemonHost())
if u.Host == "" {
s.Host = "localhost"
Expand Down Expand Up @@ -113,22 +120,33 @@ func (p *pwd) SessionClose(s *types.Session) error {
s = updatedSession

log.Printf("Starting clean up of session [%s]\n", s.Id)
g, _ := errgroup.WithContext(context.Background())
for _, i := range s.Instances {
err := p.InstanceDelete(s, i)
if err != nil {
log.Println(err)
return err
}
i := i
g.Go(func() error {
return p.InstanceDelete(s, i)
})
}
err = g.Wait()
if err != nil {
log.Println(err)
return err
}

// Disconnect PWD daemon from the network
if err := p.docker(s.Id).DisconnectNetwork(config.L2ContainerName, s.Id); err != nil {
dockerClient, err := p.dockerFactory.GetForSession(s.Id)
if err != nil {
log.Println(err)
return err
}
if err := dockerClient.DisconnectNetwork(config.L2ContainerName, s.Id); err != nil {
if !strings.Contains(err.Error(), "is not connected to the network") {
log.Println("ERROR NETWORKING", err)
return err
}
}
log.Printf("Disconnected l2 from network [%s]\n", s.Id)
if err := p.docker(s.Id).DeleteNetwork(s.Id); err != nil {
if err := dockerClient.DeleteNetwork(s.Id); err != nil {
if !strings.Contains(err.Error(), "not found") {
log.Println(err)
return err
Expand Down Expand Up @@ -189,7 +207,14 @@ func (p *pwd) SessionDeployStack(s *types.Session) error {
cmd := fmt.Sprintf("docker swarm init --advertise-addr eth0 && docker-compose -f %s pull && docker stack deploy -c %s %s", file, file, s.StackName)

w := sessionBuilderWriter{sessionId: s.Id, event: p.event}
code, err := p.docker(s.Id).ExecAttach(i.Name, []string{"sh", "-c", cmd}, &w)

dockerClient, err := p.dockerFactory.GetForSession(s.Id)
if err != nil {
log.Println(err)
return err
}

code, err := dockerClient.ExecAttach(i.Name, []string{"sh", "-c", cmd}, &w)
if err != nil {
log.Printf("Error executing stack [%s]: %s\n", s.Stack, err)
return err
Expand Down

0 comments on commit 7f17a7e

Please sign in to comment.