def test_import_module(reload, import_module, install, download_and_extract):

    _modules.import_module("s3://bucket/my-module")

    download_and_extract.assert_called_with("s3://bucket/my-module", _env.code_dir)
    install.assert_called_with(_env.code_dir)
    reload.assert_called_with(import_module(_modules.DEFAULT_MODULE_NAME))
def start(module_app):
    env = _env.ServingEnv()
    gunicorn_bind_address = '0.0.0.0:{}'.format(env.http_port)

    nginx = None

    if env.use_nginx:
        gunicorn_bind_address = UNIX_SOCKET_BIND
        _create_nginx_config(env)
        nginx = subprocess.Popen(['nginx', '-c', nginx_config_file])

    # Install user module before starting GUnicorn
    if env.module_name:
        _modules.import_module(env.module_dir, env.module_name)

    pythonpath = ','.join(sys.path + [_env.code_dir])

    gunicorn = subprocess.Popen([
        'gunicorn', '--timeout',
        str(env.model_server_timeout), '-k', 'gevent', '--pythonpath',
        pythonpath, '-b', gunicorn_bind_address, '--worker-connections',
        str(1000 * env.model_server_workers), '-w',
        str(env.model_server_workers), '--log-level', 'info', module_app
    ])

    _add_sigterm_handler(nginx, gunicorn)

    # wait for child processes. if either exit, so do we.
    pids = {c.pid for c in [nginx, gunicorn] if c}
    while True:
        pid, _ = os.wait()
        if pid in pids:
            break
Esempio n. 3
0
def test_import_module(reload, import_module, install, download_and_extract):

    _modules.import_module('s3://bucket/my-module')

    download_and_extract.assert_called_with('s3://bucket/my-module', 'default_user_module_name', _env.code_dir)
    install.assert_called_with(_env.code_dir)
    reload.assert_called_with(import_module(_modules.DEFAULT_MODULE_NAME))
Esempio n. 4
0
def start(module_app):
    """Placeholder docstring"""
    env = _env.ServingEnv()
    gunicorn_bind_address = "0.0.0.0:{}".format(env.http_port)

    nginx = None

    if env.use_nginx:
        gunicorn_bind_address = UNIX_SOCKET_BIND
        _create_nginx_config(env)
        nginx = subprocess.Popen(["nginx", "-c", nginx_config_file])

    # Install user module before starting GUnicorn
    if env.module_name:
        _modules.import_module(env.module_dir, env.module_name)

    pythonpath = ",".join(sys.path + [_env.code_dir])

    gunicorn = subprocess.Popen(
        [
            "gunicorn",
            "--timeout",
            str(env.model_server_timeout),
            "-k",
            "gevent",
            "--pythonpath",
            pythonpath,
            "-b",
            gunicorn_bind_address,
            "--worker-connections",
            str(1000 * env.model_server_workers),
            "-w",
            str(env.model_server_workers),
            "--log-level",
            "info",
            module_app,
        ]
    )

    _add_sigterm_handler(nginx, gunicorn)

    # wait for child processes. if either exit, so do we.
    pids = {c.pid for c in [nginx, gunicorn] if c}
    while True:
        pid, _ = os.wait()
        if pid in pids:
            break
    def test_without_cache(self):
        with tarfile.open() as tar_file:
            module = _modules.import_module('s3://bucket/my-module', cache=False)

            assert module == importlib.import_module(_modules.DEFAULT_MODULE_NAME)

            _modules.s3_download.assert_called_with('s3://bucket/my-module', '/tmp/tar_file')
            os.makedirs.assert_called_with('/tmp/module_dir')

            tar_file.extractall.assert_called_with(path='/tmp/module_dir')
            _modules.prepare.assert_called_with('/tmp/module_dir', _modules.DEFAULT_MODULE_NAME)
            _modules.install.assert_called_with('/tmp/module_dir')
    def test_any_name(self):
        with tarfile.open() as tar_file:
            _modules.exists.return_value = False

            module = _modules.import_module('s3://bucket/my-module', 'another_module_name', cache=True)

            assert module == importlib.import_module('another_module_name')

            _modules.s3_download.assert_called_with('s3://bucket/my-module', '/tmp/tar_file')
            os.makedirs.assert_called_with('/tmp/module_dir')

            tar_file.extractall.assert_called_with(path='/tmp/module_dir')
            _modules.prepare.assert_called_with('/tmp/module_dir', 'another_module_name')
            _modules.install.assert_called_with('/tmp/module_dir')
    def test_with_cache_and_module_already_installed(self):
        with tarfile.open() as tar_file:
            _modules.exists.return_value = True

            module = _modules.import_module('s3://bucket/my-module', cache=True)

            assert module == importlib.import_module(_modules.DEFAULT_MODULE_NAME)

            _modules.s3_download.return_value.assert_not_called()
            os.makedirs.return_value.assert_not_called()

            tar_file.extractall.return_value.assert_not_called()
            _modules.prepare.return_value.assert_not_called()
            _modules.install.return_value.assert_not_called()