mirror of https://github.com/docker/docs.git
Merge pull request #2681 from dgageot/close-drivers
Close drivers on exit
This commit is contained in:
commit
0ab066eadb
|
@ -12,6 +12,7 @@ import (
|
|||
"github.com/docker/machine/libmachine"
|
||||
"github.com/docker/machine/libmachine/cert"
|
||||
"github.com/docker/machine/libmachine/crashreport"
|
||||
"github.com/docker/machine/libmachine/drivers/rpc"
|
||||
"github.com/docker/machine/libmachine/host"
|
||||
"github.com/docker/machine/libmachine/log"
|
||||
"github.com/docker/machine/libmachine/mcnutils"
|
||||
|
@ -116,6 +117,8 @@ func fatalOnError(command func(commandLine CommandLine, api libmachine.API) erro
|
|||
mcnutils.GithubAPIToken = api.GithubAPIToken
|
||||
ssh.SetDefaultClient(api.SSHClientType)
|
||||
|
||||
defer rpcdriver.CloseDrivers()
|
||||
|
||||
if err := command(&contextCommandLine{context}, api); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
|
@ -321,12 +321,6 @@ func cmdCreateOuter(c CommandLine, api libmachine.API) error {
|
|||
driver = serialDriver.Driver
|
||||
}
|
||||
|
||||
if rpcd, ok := driver.(*rpcdriver.RPCClientDriver); ok {
|
||||
if err := rpcd.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return c.Application().Run(os.Args)
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ package rpcdriver
|
|||
import (
|
||||
"fmt"
|
||||
"net/rpc"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/docker/machine/libmachine/drivers"
|
||||
|
@ -15,6 +16,8 @@ import (
|
|||
|
||||
var (
|
||||
heartbeatInterval = 5 * time.Second
|
||||
openedDrivers = []*RPCClientDriver{}
|
||||
openedDriversLock = &sync.Mutex{}
|
||||
)
|
||||
|
||||
type RPCClientDriver struct {
|
||||
|
@ -85,6 +88,17 @@ func NewInternalClient(rpcclient *rpc.Client) *InternalClient {
|
|||
}
|
||||
}
|
||||
|
||||
func CloseDrivers() {
|
||||
openedDriversLock.Lock()
|
||||
|
||||
for _, openedDriver := range openedDrivers {
|
||||
openedDriver.close()
|
||||
}
|
||||
openedDrivers = []*RPCClientDriver{}
|
||||
|
||||
openedDriversLock.Unlock()
|
||||
}
|
||||
|
||||
func NewRPCClientDriver(driverName string, rawDriver []byte) (*RPCClientDriver, error) {
|
||||
mcnName := ""
|
||||
|
||||
|
@ -116,6 +130,10 @@ func NewRPCClientDriver(driverName string, rawDriver []byte) (*RPCClientDriver,
|
|||
heartbeatDoneCh: make(chan bool),
|
||||
}
|
||||
|
||||
openedDriversLock.Lock()
|
||||
openedDrivers = append(openedDrivers, c)
|
||||
openedDriversLock.Unlock()
|
||||
|
||||
var serverVersion int
|
||||
if err := c.Client.Call(GetVersionMethod, struct{}{}, &serverVersion); err != nil {
|
||||
// this is the first call we make to the server. We try to play nice with old pre 0.5.1 client,
|
||||
|
@ -141,7 +159,7 @@ func NewRPCClientDriver(driverName string, rawDriver []byte) (*RPCClientDriver,
|
|||
case <-time.After(heartbeatInterval):
|
||||
if err := c.Client.Call(HeartbeatMethod, struct{}{}, nil); err != nil {
|
||||
log.Warnf("Error attempting heartbeat call to plugin server: %s", err)
|
||||
c.Close()
|
||||
c.close()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -168,7 +186,7 @@ func (c *RPCClientDriver) UnmarshalJSON(data []byte) error {
|
|||
return c.SetConfigRaw(data)
|
||||
}
|
||||
|
||||
func (c *RPCClientDriver) Close() error {
|
||||
func (c *RPCClientDriver) close() error {
|
||||
c.heartbeatDoneCh <- true
|
||||
close(c.heartbeatDoneCh)
|
||||
|
||||
|
|
Loading…
Reference in New Issue