Skip to content

Commit

Permalink
Merge branch 'singularity-from-notebook-56-bmiport-envvar-55'
Browse files Browse the repository at this point in the history
  • Loading branch information
sverhoeven committed Feb 19, 2020
2 parents eabae8e + a15967a commit d4713d8
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 6 deletions.
4 changes: 4 additions & 0 deletions cpp/bmi_grpc_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,10 @@ void run_bmi_server(BmiClass *model, int argc, char *argv[])
{
server_address = "0.0.0.0:" + std::string(argv[1]);
}
std::string bmi_port = std::getenv("BMI_PORT");
if(!bmi_port.empty()) {
server_address = "0.0.0.0:" + bmi_port;
}
std::cerr << "BMI grpc server attached to server address " << server_address << std::endl;
BmiGRPCService service(model);
grpc::ServerBuilder builder;
Expand Down
1 change: 1 addition & 0 deletions docs/server/Cpp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Running
-------

Since native language lack reflection, it is necessary to make your own ``run_bmi_server`` program. We provide a function ``run_bmi_server(Bmi*, int*, char*)`` in the ``bmi_grpc_server.h`` header that can be called with your model instance (see the example below). To compile your server binary, it is necessary to link against grpc4bmi and protobuf libraries.
The program will accept a single optional argument which is the port the server will run on. The port can also be specified using the BMI_PORT environment variable. The default port is 50051.

.. _example_cpp:

Expand Down
5 changes: 3 additions & 2 deletions grpc4bmi/bmi_client_singularity.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def check_singularity_version():
class BmiClientSingularity(BmiClient):
"""BMI GRPC client for singularity server processes
During initialization launches a singularity container with run-bmi-server as its command.
The container exposes a port the client connects to.
The client picks a random port and expects the container to run the server on that port.
The port is passed to the container using the BMI_PORT environment variable.
>>> from grpc4bmi.bmi_client_singularity import BmiClientSingularity
>>> image = 'docker://ewatercycle/wflow-grpc4bmi:latest'
Expand Down Expand Up @@ -68,7 +69,7 @@ def __init__(self, image, input_dir=None, output_dir=None):
env = os.environ.copy()
env['BMI_PORT'] = str(port)
logging.info(f'Running {image} singularity container on port {port}')
self.container = subprocess.Popen(args, stderr=sys.stderr, stdout=sys.stdout, env=env, preexec_fn=os.setsid)
self.container = subprocess.Popen(args, env=env, preexec_fn=os.setsid)
super(BmiClientSingularity, self).__init__(BmiClient.create_grpc_channel(port=port, host=host))

def __del__(self):
Expand Down
4 changes: 2 additions & 2 deletions grpc4bmi/run_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ def main(argv=sys.argv[1:]):
else:
model = build(args.name, path)

port = args.port
port = int(os.environ.get("BMI_PORT", 0))
if port == 0:
port = int(os.environ.get("BMI_PORT", 0))
port = args.port
if port == 0:
with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
s.bind(("", 0))
Expand Down
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ sphinx
sphinxcontrib-napoleon
sphinx-argparse
sphinx_rtd_theme
nbconvert
ipykernel
nbformat
27 changes: 25 additions & 2 deletions test/test_singularity.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
from textwrap import dedent

import pytest
from nbconvert.preprocessors import ExecutePreprocessor
from nbformat.v4 import new_notebook, new_code_cell

from grpc4bmi.bmi_client_singularity import BmiClientSingularity
from grpc4bmi.reserve import reserve_grid_padding

IMAGE_NAME = "docker://ewatercycle/walrus-grpc4bmi:v0.3.1"


@pytest.fixture()
def walrus_model(tmp_path, walrus_input):
model = BmiClientSingularity(image="docker://ewatercycle/walrus-grpc4bmi:v0.3.1", input_dir=str(tmp_path))
model = BmiClientSingularity(image=IMAGE_NAME, input_dir=str(tmp_path))
yield model
del model

Expand All @@ -26,5 +32,22 @@ def test_get_value_ref(self, walrus_model):
def test_get_grid_origin(self, walrus_input, walrus_model):
walrus_model.initialize(str(walrus_input))
grid_id = walrus_model.get_var_grid('Q')

assert len(walrus_model.get_grid_origin(grid_id, reserve_grid_padding(walrus_model, grid_id))) == 2


@pytest.fixture
def notebook():
cells = [
new_code_cell(dedent("""\
from grpc4bmi.bmi_client_singularity import BmiClientSingularity
walrus_model = BmiClientSingularity(image='{0}')
assert walrus_model.get_component_name() == 'WALRUS'
del walrus_model
""".format(IMAGE_NAME)))
]
return new_notebook(cells=cells)


def test_from_notebook(notebook, tmp_path):
ep = ExecutePreprocessor(timeout=600, kernel_name='python3')
ep.preprocess(notebook, {'metadata': {'path': tmp_path}})

0 comments on commit d4713d8

Please sign in to comment.