def status_alg(self): response = requests.get(self.algorithm_url + "/status_alg", headers={"Content-Type": "text/plain"}, auth=(self.algorithm_user, self.algorithm_password), timeout=3.0) response.raise_for_status( ) # if not 200 raise an exception requests.exceptions.HTTPError data_from_alg = json.loads(response.text) if data_from_alg['status'] == 'ERROR': if not data_from_alg['msg']: logger.info( "[STATUS_ALG]Algorithm status: {}. Reason: Unable to find. Json not sent or not compliant" .format(data_from_alg['status'])) else: logger.info( "[STATUS_ALG]Algorithm status: {}. Reason: {}".format( data_from_alg['status'], data_from_alg['msg'])) raise errors.AlgorithmBadStatusException() logger.info("[STATUS_ALG]Algorithm status: {}. Info: {}".format( data_from_alg['status'], data_from_alg['msg'])) return data_from_alg
def run_alg(self, request_id): # Update database of request_id # Save request_id asociated to algorithm data = {} data['algorithm_id'] = self.algorithm_id data['request_id'] = int(request_id) db = TinyDB(globals.DATABASE) table = db.table('request_table') if table.insert(data) < 0: raise errors.DBInsertionWrongException() logger.info( "[THIRFT SERVER] Insertion in data base correct of request_id") response = requests.post(self.algorithm_url + "/run_alg", data=json.dumps( dict({ "config": self.algorithm_config, "request_id": request_id, "dss_api_endpoint": globals.DSSURL })), headers={"Content-Type": "application/json"}, auth=(self.algorithm_user, self.algorithm_password), timeout=10.0) response.raise_for_status( ) # if not 200 raise an exception requests.exceptions.HTTPError data_from_alg = json.loads(response.text) if data_from_alg['status'] != 'STARTED': if not data_from_alg['msg']: logger.info( "[RUN_ALG]Algorithm status: {}. Reason: Unable to find. Json not sent or not compliant" .format(data_from_alg['status'])) else: logger.info("[RUN_ALG]Algorithm status: {}. Reason: {}".format( data_from_alg['status'], data_from_alg['msg'])) raise errors.AlgorithmBadStatusException() logger.info("[RUN_ALG]Algorithm status: {}. Info: {}".format( data_from_alg['status'], data_from_alg['msg']))
def stop_alg(self): response = requests.get(self.algorithm_url + "/stop_alg", headers={"Content-Type": "text/plain"}, auth=('entrypoint', 'fakepass')) response.raise_for_status( ) # if not 200 raise an exception requests.exceptions.HTTPError data_from_alg = json.loads(response.text) if data_from_alg['status'] != 'STOPPED': if not data_from_alg['msg']: logging.debug( "Algorithm status: {}. Reason: Unable to find. Json not sent or not compliant" .format(data_from_alg['status'])) else: logging.debug("Algorithm status: {}. Reason: {}".format( data_from_alg['status'], data_from_alg['msg'])) raise errors.AlgorithmBadStatusException() logging.debug("Algorithm status: {}. Info: {}".format( data_from_alg['status'], data_from_alg['msg']))
def run_alg(self): response = requests.post(self.algorithm_url + "/run_alg", data=json.dumps( dict({"config": self.algorithm_config})), headers={"Content-Type": "application/json"}, auth=('entrypoint', 'fakepass')) response.raise_for_status( ) # if not 200 raise an exception requests.exceptions.HTTPError data_from_alg = json.loads(response.text) if data_from_alg['status'] != 'STARTED': if not data_from_alg['msg']: logging.debug( "Algorithm status: {}. Reason: Unable to find. Json not sent or not compliant" .format(data_from_alg['status'])) else: logging.debug("Algorithm status: {}. Reason: {}".format( data_from_alg['status'], data_from_alg['msg'])) raise errors.AlgorithmBadStatusException() logging.debug("Algorithm status: {}. Info: {}".format( data_from_alg['status'], data_from_alg['msg']))
def get(self, algorithm_id): self.algorithm_id = int(algorithm_id) try: db = TinyDB(globals.DATABASE) table = db.table('algorithm_list') # switch to table query = Query() result = table.search(query.id == self.algorithm_id) if not result: raise errors.DBAlgorithmNotExistException() algorithm = EntryPoint(algorithm_url=result[0]['urlapi'], algorithm_config={}, algorithm_id=self.algorithm_id, user=result[0]['auth']['user'], password=result[0]['auth']['password']) # convey data to algorithm through /status_alg response = algorithm.status_alg() logger.info("[Status API] Algorithm sent status. Code 200 sent") # check if status is correct if not response['status'] in ("STARTED", "STOPPED"): raise errors.AlgorithmBadStatusException() # update status if not change_status(id=self.algorithm_id, new_status=response['status'], table=table): raise errors.DBAlgorithmUpdateException() return Response(json.dumps(response), status=200, mimetype='application/json') except (requests.exceptions.HTTPError, requests.exceptions.ConnectionError): logger.info("[Status API][requests.exceptions.HTTPError, requests.exceptions.ConnectionError]" \ "Algorithm not reachable in {}. Code 501 sent".format(result[0]['urlapi'])) return Response("Algorithm api not reachable", status=501, mimetype='text/plain') except (errors.AlgorithmBadStatusException, json.decoder.JSONDecodeError): logger.info("[Status API][AlgorithmBadStatusException, json.decoder.JSONDecodeError]" \ "Command sent but algorithm reply ERROR or not a json. Code 502 sent") return Response( "Command sent but algorithm reply ERROR or not a json", status=502, mimetype='text/plain') except (errors.DBAlgorithmNotExistException, errors.DBAlgorithmUpdateException): logger.info("[Status API][errors.DBAlgorithmNotExistException, errors.DBAlgorithmUpdateException, Exception] " \ "Failure in DSS to update algorithm or algorithm do not exist. Code 500 sent") return Response("Failure in DSS to get status of algorithm", status=500, mimetype='text/plain') except Exception as e: logger.info("[Status API][UncaughtException] {}".format(e)) return Response( "Failure in DSS to get status of algorithm. Check log", status=500, mimetype='text/plain')