def test_serialize(self): status = AppStatus(AppState.FAILED) serialized = status.__repr__() deser_status_dict = json.loads(serialized) deser_status = AppStatus(**deser_status_dict) self.assertEqual(status.state, deser_status.state) self.assertEqual(status.msg, deser_status.msg) self.assertEqual(status.structured_error_msg, deser_status.structured_error_msg)
def test_serialize_embed_json(self): status = AppStatus(AppState.FAILED, structured_error_msg='{"message": "test error"}') serialized = status.__repr__() deser_status_dict = json.loads(serialized) scheduler_msg = deser_status_dict.pop("structured_error_msg") scheduler_msg_json = json.dumps(scheduler_msg) deser_status_dict["structured_error_msg"] = scheduler_msg_json deser_status = AppStatus(**deser_status_dict) self.assertEqual(status.state, deser_status.state) self.assertEqual(status.msg, deser_status.msg) self.assertEqual(status.structured_error_msg, deser_status.structured_error_msg)
def test_is_terminal(self): for s in AppState: is_terminal = AppStatus(state=s).is_terminal() if s in _TERMINAL_STATES: self.assertTrue(is_terminal) else: self.assertFalse(is_terminal)
def status(self, app_handle: AppHandle) -> Optional[AppStatus]: # allow status checks of apps from other sessions scheduler, app_id = self._scheduler_app_id(app_handle, check_session=False) desc = scheduler.describe(app_id) if not desc: # app does not exist on the scheduler # remove it from apps cache if it exists # effectively removes this app from the list() API self._apps.pop(app_handle, None) return None app_status = AppStatus(desc.state, desc.num_restarts, desc.msg, desc.structured_error_msg) app_status.ui_url = desc.ui_url return app_status
def test_get_formatted_string(self): status = AppStatus( AppState.FAILED, replicas={ "worker1": [ RoleReplicaStatus("id1", AppState.FAILED, role="worker1", exit_code=3) ] }, ) formatted_str = status.get_formatted_str() expected_str = ( "\nState: FAILED ; Num Restarts: 0\nMsg: \nReplicas: \n- Role: [worker1]:\n\n- [worker1:id1]\n " "Timestamp: None; Exit Code: 3\n State: FAILED\n Error Message: None\n\n\n" ) self.assertEqual(expected_str, formatted_str)
def test_get_formatted_string(self): status = AppStatus( AppState.FAILED, replicas={ "worker1": [ RoleReplicaStatus("id1", AppState.FAILED, role="worker1", exit_code=3) ] }, ) formatted_str = status.get_formatted_str() expected_str = ( "\nstate: AppState.FAILED\nnum_restarts: 0\nmsg: " "\nReplicas: \n- Role: [worker1]:\n\n- [worker1:id1]\n " "timestamp: None\n exit_code: 3\n state: AppState.FAILED\n error_msg: None\n\n\n" ) self.assertEqual(expected_str, formatted_str)
def status(self, app_id: str) -> Optional[AppStatus]: if app_id not in self._apps: raise UnknownAppException(app_id) desc = self._scheduler.describe(app_id) if not desc: # app does not exist on the scheduler; remove it from apps cache # effectively removes this app from the list() API del self._apps[app_id] return None msg = "TODO return a message" return AppStatus(desc.state, desc.num_restarts, msg)