def describe(self, app_id: str) -> Optional[DescribeAppResponse]: if app_id not in self._apps: return None local_app = self._apps[app_id] # check if the app has been known to have finished if is_terminal(local_app.state): state = local_app.state else: running = False failed = False for (_, procs) in local_app.role_procs.items(): for p in procs: running |= self._is_alive(p) failed |= self._failed(p) if running: state = AppState.RUNNING elif failed: state = AppState.FAILED self._terminate(local_app) # terminate danglers else: state = AppState.SUCCEEDED local_app.set_state(state) resp = DescribeAppResponse() resp.app_id = app_id resp.state = state resp.num_restarts = 0 return resp
def describe(self, app_id: str) -> Optional[DescribeAppResponse]: if app_id not in self._apps: return None local_app = self._apps[app_id] # check if the app is known to have finished if is_terminal(local_app.state): state = local_app.state else: running = False failed = False for replicas in local_app.role_replicas.values(): for r in replicas: running |= r.is_alive() failed |= r.failed() if running: state = AppState.RUNNING elif failed: state = AppState.FAILED else: state = AppState.SUCCEEDED local_app.set_state(state) if is_terminal(local_app.state): local_app.close() resp = DescribeAppResponse() resp.app_id = app_id resp.state = state resp.num_restarts = 0 return resp
def test_status_ui_url(self): app_id = "test_app" mock_scheduler = MagicMock() resp = DescribeAppResponse() resp.ui_url = "https://foobar" mock_scheduler.submit.return_value = app_id mock_scheduler.describe.return_value = resp session = StandaloneSession(name="test_ui_url_session", scheduler=mock_scheduler) role = Role("ignored").runs("/bin/echo").on(self.test_container) session.run(Application(app_id).of(role)) status = session.status(app_id) self.assertEquals(resp.ui_url, status.ui_url)
def test_status_structured_msg(self): app_id = "test_app" mock_scheduler = MagicMock() resp = DescribeAppResponse() resp.scheduler_error_msg = '{"message": "test error"}' mock_scheduler.submit.return_value = app_id mock_scheduler.describe.return_value = resp session = StandaloneSession(name="test_structured_msg", schedulers={"default": mock_scheduler}) role = Role("ignored").runs("/bin/echo").on(self.test_container) app_handle = session.run(Application(app_id).of(role)) status = session.status(app_handle) self.assertEquals(resp.scheduler_error_msg, status.scheduler_error_msg)
def test_log_lines(self): app_id = "mock_app" scheduler_mock = MagicMock() scheduler_mock.describe.return_value = DescribeAppResponse( app_id, AppState.RUNNING) scheduler_mock.log_iter.return_value = iter(["hello", "world"]) session = StandaloneSession(name=SESSION_NAME, schedulers={"default": scheduler_mock}, wait_interval=1) role_name = "trainer" replica_id = 2 regex = "QPS.*" since = datetime.datetime.now() until = datetime.datetime.now() lines = list( session.log_lines( f"default://test_session/{app_id}", role_name, replica_id, regex, since, until, )) self.assertEqual(["hello", "world"], lines) scheduler_mock.log_iter.assert_called_once_with( app_id, role_name, replica_id, regex, since, until)