From 33cbf9670d6abde5544f8be0a837cd19ccb79973 Mon Sep 17 00:00:00 2001 From: Jonathan Weth Date: Mon, 27 Jul 2020 12:13:26 +0200 Subject: Add logging Close #5 --- session.py | 52 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 16 deletions(-) (limited to 'session.py') diff --git a/session.py b/session.py index b57bf57..af71c6a 100644 --- a/session.py +++ b/session.py @@ -1,3 +1,4 @@ +import logging import os import random import secrets @@ -75,11 +76,15 @@ class Session: if not self.mockup_session: self.pw_filename = save_password(self.password) + logging.info("The password for the session has been generated.") + def _start_vnc(self): """Start x11vnc server if not in mockup_session mode.""" if not self.mockup_session: process_info = run_vnc(self.pw_filename) + logging.info("The VNC server has been started.") + self.vnc_pid = process_info["vnc"]["pid"] self.vnc_port = process_info["vnc"]["port"] self.ws_pid = process_info["ws"]["pid"] @@ -91,6 +96,8 @@ class Session: self.ws_pid = int(random_digits(5)) self.vnc_pid = int(random_digits(5)) + logging.info("The lock file for mocking a VNC server has been created.") + # Create a temporary file to indicate that this process is still 'Running' filename = f"/tmp/rwa/{str(self.ws_port) + str(self.vnc_port) + str(self.ws_pid) + str(self.vnc_pid)}.lock" new_file = open(filename, "w") @@ -109,14 +116,18 @@ class Session: "trigger_token": self.trigger_token, }, ) - print(r) + + logging.info( + f"The session has been registered in RWA with status code {r.status_code} and response {r.content.decode()}." + ) + self.meta = r.json() self.session_id = self.meta["session_id"] self.web_url = self.meta["url"] self.api_token = self.meta["token"] self.pin = self.meta["pin"] else: - print('"Registered" in RWA') + logging.info(f"The session has pretended that he had created a session.") self.meta = {} self.session_id = int(random_digits(10)) self.web_url = "testhostname:" + random_digits(5) + "/RWA/test/" @@ -125,7 +136,6 @@ class Session: def trigger(self): """Event triggered by Django.""" - print("Triggered") self.pull() def pull(self): @@ -135,6 +145,10 @@ class Session: STATUS_URL, params={"id": self.session_id}, headers=self._api_headers ) + logging.info( + f"The session has received its status from RWA with status code {r.status_code} and response {r.content.decode()}." + ) + if r.status_code in (401, 402, 403, 404, 405): # Session doesn't exist anymore, so stop it local self.stop(triggered=True) @@ -153,17 +167,23 @@ class Session: def _do_file_job(self, job): """Download a file from server to the user's desktop.""" + logging.info( + f"The session has received a file job and is downloading it now ({job}):" + ) subprocess.Popen(["wget", job["file"], "-P", self.desktop_dir]) self._mark_job_as_done(job) def _mark_job_as_done(self, job): """Mark a job as done (in this service and on the server).""" self.done_jobs.append(job["job_id"]) - requests.post( + r = requests.post( MARK_JOB_AS_DONE_URL, params={"id": job["job_id"]}, headers=self._api_headers, ) + logging.info( + f"The session has marked the job {job} as done in RWA with status code {r.status_code} and response {r.content.decode()}." + ) def push(self): """Update status: Push status to Django.""" @@ -172,6 +192,7 @@ class Session: def stop(self, triggered: bool = False): """Stop session and clean up.""" if self.mockup_session: + logging.info("Mock session has been stopped by deleting its lock file.") filename = f"/tmp/rwa/{str(self.ws_port) + str(self.vnc_port) + str(self.ws_pid) + str(self.vnc_pid)}.lock" if os.path.isfile(filename): os.remove(filename) @@ -180,31 +201,30 @@ class Session: del self return - # Kill VNC - if self.vnc_pid in psutil.pids(): - print("Kill VNC.") - os.kill(self.vnc_pid, signal.SIGTERM) - # Kill websockify if self.ws_pid in psutil.pids(): - print("Kill websockify.") os.kill(self.ws_pid, signal.SIGTERM) + logging.info("The websockify server has been terminated.") + + # Kill VNC + if self.vnc_pid in psutil.pids(): + os.kill(self.vnc_pid, signal.SIGTERM) + logging.info("The VNC server has been terminated.") # Delete PW file if os.path.exists(self.pw_filename): - print("Delete password file") os.remove(self.pw_filename) - - if hasattr(self, "trigger_thread"): - print("Kill trigger service.") - self.trigger_thread.shutdown() + logging.info("The VNC server password file has been removed.") self.push() if not triggered: - requests.post( + r = requests.post( STOP_URL, params={"id": self.session_id}, headers=self._api_headers ) + logging.info( + f"The stop action has been registered in RWA with status code {r.status_code} and response {r.content.decode()}." + ) self.status_text = "stopped" -- cgit v1.2.3