Move converge() to a test module, and use a short timeout for tests.

Signed-off-by: Daniel Nephin <dnephin@gmail.com>
This commit is contained in:
Daniel Nephin 2015-06-14 17:11:29 -04:00
commit 06db577105
7 changed files with 69 additions and 52 deletions

View file

@ -10,7 +10,9 @@ import sys
from docker.errors import APIError
import dockerpty
from .. import __version__, legacy
from .. import __version__
from .. import legacy
from ..const import DEFAULT_TIMEOUT
from ..project import NoSuchService, ConfigurationError
from ..service import BuildError, NeedsBuildError
from ..config import parse_environment
@ -394,9 +396,8 @@ class TopLevelCommand(Command):
-t, --timeout TIMEOUT Specify a shutdown timeout in seconds.
(default: 10)
"""
timeout = options.get('--timeout')
params = {} if timeout is None else {'timeout': int(timeout)}
project.stop(service_names=options['SERVICE'], **params)
timeout = float(options.get('--timeout') or DEFAULT_TIMEOUT)
project.stop(service_names=options['SERVICE'], timeout=timeout)
def restart(self, project, options):
"""
@ -408,9 +409,8 @@ class TopLevelCommand(Command):
-t, --timeout TIMEOUT Specify a shutdown timeout in seconds.
(default: 10)
"""
timeout = options.get('--timeout')
params = {} if timeout is None else {'timeout': int(timeout)}
project.restart(service_names=options['SERVICE'], **params)
timeout = float(options.get('--timeout') or DEFAULT_TIMEOUT)
project.restart(service_names=options['SERVICE'], timeout=timeout)
def up(self, project, options):
"""
@ -452,7 +452,7 @@ class TopLevelCommand(Command):
allow_recreate = not options['--no-recreate']
smart_recreate = options['--x-smart-recreate']
service_names = options['SERVICE']
timeout = int(options['--timeout']) if options['--timeout'] is not None else None
timeout = float(options.get('--timeout') or DEFAULT_TIMEOUT)
project.up(
service_names=service_names,
@ -479,8 +479,7 @@ class TopLevelCommand(Command):
signal.signal(signal.SIGINT, handler)
print("Gracefully stopping... (press Ctrl+C again to force)")
params = {} if timeout is None else {'timeout': timeout}
project.stop(service_names=service_names, **params)
project.stop(service_names=service_names, timeout=timeout)
def migrate_to_labels(self, project, _options):
"""

View file

@ -1,4 +1,5 @@
DEFAULT_TIMEOUT = 10
LABEL_CONTAINER_NUMBER = 'com.docker.compose.container-number'
LABEL_ONE_OFF = 'com.docker.compose.oneoff'
LABEL_PROJECT = 'com.docker.compose.project'

View file

@ -6,7 +6,7 @@ from functools import reduce
from docker.errors import APIError
from .config import get_service_name_from_net, ConfigurationError
from .const import LABEL_PROJECT, LABEL_SERVICE, LABEL_ONE_OFF
from .const import LABEL_PROJECT, LABEL_SERVICE, LABEL_ONE_OFF, DEFAULT_TIMEOUT
from .service import Service
from .container import Container
from .legacy import check_for_legacy_containers
@ -212,7 +212,7 @@ class Project(object):
smart_recreate=False,
insecure_registry=False,
do_build=True,
timeout=None):
timeout=DEFAULT_TIMEOUT):
services = self.get_services(service_names, include_deps=start_deps)

View file

@ -13,6 +13,7 @@ from docker.utils import create_host_config, LogConfig
from . import __version__
from .config import DOCKER_CONFIG_KEYS, merge_environment
from .const import (
DEFAULT_TIMEOUT,
LABEL_CONTAINER_NUMBER,
LABEL_ONE_OFF,
LABEL_PROJECT,
@ -251,26 +252,6 @@ class Service(object):
else:
return self.options['image']
def converge(self,
allow_recreate=True,
smart_recreate=False,
insecure_registry=False,
do_build=True):
"""
If a container for this service doesn't exist, create and start one. If there are
any, stop them, create+start new ones, and remove the old containers.
"""
plan = self.convergence_plan(
allow_recreate=allow_recreate,
smart_recreate=smart_recreate,
)
return self.execute_convergence_plan(
plan,
insecure_registry=insecure_registry,
do_build=do_build,
)
def convergence_plan(self,
allow_recreate=True,
smart_recreate=False):
@ -312,7 +293,7 @@ class Service(object):
plan,
insecure_registry=False,
do_build=True,
timeout=None):
timeout=DEFAULT_TIMEOUT):
(action, containers) = plan
if action == 'create':
@ -352,7 +333,7 @@ class Service(object):
def recreate_container(self,
container,
insecure_registry=False,
timeout=None):
timeout=DEFAULT_TIMEOUT):
"""Recreate a container.
The original container is renamed to a temporary name so that data
@ -361,8 +342,7 @@ class Service(object):
"""
log.info("Recreating %s..." % container.name)
try:
stop_params = {} if timeout is None else {'timeout': timeout}
container.stop(**stop_params)
container.stop(timeout=timeout)
except APIError as e:
if (e.response.status_code == 500
and e.explanation