diff --git a/docker/app.py b/docker/app.py index e3f94b18..73f7b082 100644 --- a/docker/app.py +++ b/docker/app.py @@ -42,13 +42,16 @@ def is_invalid_status(status: str) -> bool: @app.post('/start') async def start_container(num_experiments: int, config: Request) -> JSONResponse: - container_info = manager.start(config=await config.json()) - for _ in range(num_experiments): - container_info += manager.start(config=await config.json()) - if (is_invalid_status(container_info.status) or container_info.data is False): - return JSONResponse(status_code=404, content=vars(container_info)) - else: - return JSONResponse(vars(container_info)) + final_infos = [] + for number in range(num_experiments): + final_infos += [manager.start(config=await config.json())] + if (is_invalid_status(final_infos[number].status) or final_infos[number].data is False): + return JSONResponse(status_code=404, content=vars(final_infos[0])) + return_dict = {} + for index in range(num_experiments): + return_dict[index] = vars(final_infos[index]) + print(f'successfully started {num_experiments} container') + return JSONResponse(return_dict) @app.get('/health/') diff --git a/docker/docker_manager.py b/docker/docker_manager.py index 83f956af..78c15996 100644 --- a/docker/docker_manager.py +++ b/docker/docker_manager.py @@ -32,21 +32,6 @@ def __init__(self, id: str, status: str, data=None, stream: GeneratorType = None self.data = data self.stream = stream - def __add__(self, other_docker_info): - resulting_docker_info = DockerInfo(id='not relevant', status='not relavant') - resulting_docker_info.id = self._add_to_list(self.id, other_docker_info.id) - resulting_docker_info.status = self._add_to_list(self.status, other_docker_info.status) - resulting_docker_info.data = self._add_to_list(self.data, other_docker_info.data) - resulting_docker_info.stream = self._add_to_list(self.stream, other_docker_info.stream) - return resulting_docker_info - - def _add_to_list(self, var1, var2) -> list: - # var1 could be list - if type(var1) == list: - return var1 + [var2] - else: - return [var1, var2] - class DockerManager(): """ @@ -78,15 +63,16 @@ def __new__(cls): cls._update_port_mapping() return cls._instance - def start(self, config: dict) -> DockerInfo: + def start(self, config: dict, count: int) -> DockerInfo: """ To be called by the REST API. Create and start a new docker container from the image of the specified command. Args: config (dict): The combined hyperparameter_config and environment_config_command dictionaries that should be sent to the container. + count (int): number of containers that should be started Returns: - DockerInfo: A JSON serializable object containing the id and the status of the new container. + DockerInfo: A JSON serializable object containing the id and the status of the new container(s). """ if 'hyperparameter' not in config: return DockerInfo(id='No container was started', status='The config is missing the "hyperparameter"-field') diff --git a/docker/test_docker_manager.py b/docker/test_docker_manager.py index ee0ef372..a5b83be4 100644 --- a/docker/test_docker_manager.py +++ b/docker/test_docker_manager.py @@ -94,41 +94,3 @@ def test_port_mapping_initialization(): def test_allowed_commands_is_up_to_date(): assert set(manager._allowed_commands) == {'training', 'exampleprinter', 'agent_monitoring'}, \ 'The set of allowed commands has changed, please update this and all the other tests!' - - -def test_docker_info_add(): - docker_info1 = docker_manager.DockerInfo(id='test1', status='healthy', data='some test data', stream=None) - docker_info2 = docker_manager.DockerInfo(id='test2', status='not so healthy', data=False, stream=None) - - expected_docker_info = docker_manager.DockerInfo(id='not relevant', status='not relavant') - expected_docker_info.id = ['test1', 'test2'] - expected_docker_info.status = ['healthy', 'not so healthy'] - expected_docker_info.data = ['some test data', False] - expected_docker_info.stream = [None, None] - - resulting_docker_info = docker_info1 + docker_info2 - assert expected_docker_info.id == resulting_docker_info.id - assert expected_docker_info.status == resulting_docker_info.status - assert expected_docker_info.data == resulting_docker_info.data - assert expected_docker_info.stream == resulting_docker_info.stream - - -def test_docker_info_add_multiple(): - docker_info1 = docker_manager.DockerInfo(id='test1', status='healthy', data='some test data', stream=None) - docker_info2 = docker_manager.DockerInfo(id='test2', status='not so healthy', data=False, stream=None) - docker_info3 = docker_manager.DockerInfo(id='test3', status='so lala', data=42, stream=None) - - resulting_docker_info = docker_info1 - for docker_info in [docker_info2, docker_info3]: - resulting_docker_info += docker_info - - expected_docker_info = docker_manager.DockerInfo(id='not relevant', status='not relavant') - expected_docker_info.id = ['test1', 'test2', 'test3'] - expected_docker_info.status = ['healthy', 'not so healthy', 'so lala'] - expected_docker_info.data = ['some test data', False, 42] - expected_docker_info.stream = [None, None, None] - - assert expected_docker_info.id == resulting_docker_info.id - assert expected_docker_info.status == resulting_docker_info.status - assert expected_docker_info.data == resulting_docker_info.data - assert expected_docker_info.stream == resulting_docker_info.stream