diff options
author | Jonathan Weth <git@jonathanweth.de> | 2020-07-31 16:07:38 +0200 |
---|---|---|
committer | Jonathan Weth <git@jonathanweth.de> | 2020-07-31 16:07:38 +0200 |
commit | bb5a7fcf30e11c7b955f46572de1ef4a3b773c97 (patch) | |
tree | a78f6971f5a01bf5e58676091abbe25234343b43 | |
parent | 8bdd82f633e2904959327cb90c39901e9499bfa9 (diff) | |
download | RWA.Support.SessionService-bb5a7fcf30e11c7b955f46572de1ef4a3b773c97.tar.gz RWA.Support.SessionService-bb5a7fcf30e11c7b955f46572de1ef4a3b773c97.tar.bz2 RWA.Support.SessionService-bb5a7fcf30e11c7b955f46572de1ef4a3b773c97.zip |
Return error status if connection to RWA wasn't successful
Close #10
-rwxr-xr-x | service.py | 31 | ||||
-rw-r--r-- | session.py | 26 |
2 files changed, 36 insertions, 21 deletions
@@ -70,30 +70,39 @@ class RWAService(dbus.service.Object): :: - {"status": "error"} + {"status": "error", "type": "<type>"} + + **Possible choices for error types:** ``multiple``, ``connection`` """ if ALLOW_ONLY_ONE_SESSION and len(self.sessions.values()) > 0: logging.warning( "There is already one session running and the service is configured to allow only one " "session, so this session won't be started." ) - return json.dumps({"status": "error"}) + return json.dumps({"status": "error", "type": "multiple"}) # Start session - session = Session(self.trigger_service.port, mockup_mode) + try: + session = Session(self.trigger_service.port, mockup_mode) - # Add session to sessions list - self.sessions[session.pid] = session + # Add session to sessions list + self.sessions[session.pid] = session - # Start session update service - self._ensure_update_service() + # Start session update service + self._ensure_update_service() - return_json = session.client_meta - return_json["status"] = "success" + return_json = session.client_meta + return_json["status"] = "success" - logging.info(f"New session #{session.pid} was started with meta {return_json}.") + logging.info( + f"New session #{session.pid} was started with meta {return_json}." + ) + + return json.dumps(return_json) + except ConnectionError: + pass - return json.dumps(return_json) + return json.dumps({"status": "error", "type": "connection"}) @dbus.service.method("org.ArcticaProject.RWA", in_signature="i", out_signature="s") def status(self, pid: int) -> str: @@ -107,21 +107,27 @@ class Session: def _register_session(self): """Register session in RWA if not in mockup_session mode.""" if not self.mockup_session: - r = requests.post( - REGISTER_URL, - json={ - "port": self.ws_port, - "password": self.password, - "pid": self.vnc_pid, - "trigger_port": self.trigger_port, - "trigger_token": self.trigger_token, - }, - ) + try: + r = requests.post( + REGISTER_URL, + json={ + "port": self.ws_port, + "password": self.password, + "pid": self.vnc_pid, + "trigger_port": self.trigger_port, + "trigger_token": self.trigger_token, + }, + ) + except requests.exceptions.ConnectionError: + raise ConnectionError() logging.info( f"The session has been registered in RWA with status code {r.status_code} and response {r.content.decode()}." ) + if r.status_code != 200: + raise ConnectionError() + self.meta = r.json() self.session_id = self.meta["session_id"] self.web_url = self.meta["url"] |