From 246742739a5610e15d788e355414e6773fc1bf04 Mon Sep 17 00:00:00 2001 From: hassankh148 Date: Wed, 17 Apr 2024 15:03:41 +0300 Subject: [PATCH 1/4] add batch_execute --- app.py | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index 44b67e9..27c6bd3 100644 --- a/app.py +++ b/app.py @@ -88,19 +88,17 @@ def return_code(self): return self._return_code -@app.route('/execute', methods=['POST']) -def execute(): +def _execute(request_as_string: str): success = False - req = request.get_json() try: conn = get_conn_through_sentinel() - response = conn.execute_command(*req['command'].split()) + response = conn.execute_command(*request_as_string.split()) success = True except (redis.exceptions.ConnectionError, redis.exceptions.ResponseError): try: reload_username_password_from_file_system_if_needed(app) conn = get_conn_through_sentinel() - response = conn.execute_command(*req['command'].split()) + response = conn.execute_command(*request_as_string.split()) success = True except Exception as err: response = 'Exception: cannot connect. %s' % str(err) @@ -108,6 +106,13 @@ def execute(): except Exception as err: response = 'Exception: %s' % str(err) app.logger.exception("execute err") + return response, success + + +@app.route('/execute', methods=['POST']) +def execute(): + req = request.get_json() + response, success = _execute(req['command']) return jsonify({ 'response': response, @@ -115,6 +120,26 @@ def execute(): }) +@app.route('/batch_execute', methods=['POST']) +def batch_execute(): + all_succeeded = True + responses = dict() + req = request.get_json() + commands = req['commands'] + for command in commands: + response, success = _execute(command) + responses[command] = { + 'response': response, + 'success': success, + } + if not success: + all_succeeded = False + return jsonify({ + 'response': responses, + 'success': all_succeeded + }) + + def reload_username_password_from_file_system_if_needed(app): # It may be that the dynamic password was changed since the config was set if should_read_from_file_system(): From 2f9f1cc788b10d8bf4db58dd0cbb23f42e1ac021 Mon Sep 17 00:00:00 2001 From: hassankh148 Date: Wed, 24 Apr 2024 14:12:00 +0300 Subject: [PATCH 2/4] use python 3.8 --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index eb38882..83fffb4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.6.7 +FROM python:3.8 ENV FLASK_APP app.py ENV APP_SETTINGS settings.cfg From f4612d7bd0b280045e9fab98b794f642ab1fa10a Mon Sep 17 00:00:00 2001 From: hassankh148 Date: Wed, 24 Apr 2024 14:25:03 +0300 Subject: [PATCH 3/4] CR --- app.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index 27c6bd3..bca525c 100644 --- a/app.py +++ b/app.py @@ -88,17 +88,17 @@ def return_code(self): return self._return_code -def _execute(request_as_string: str): +def _execute(command: str): success = False try: conn = get_conn_through_sentinel() - response = conn.execute_command(*request_as_string.split()) + response = conn.execute_command(*command.split()) success = True except (redis.exceptions.ConnectionError, redis.exceptions.ResponseError): try: reload_username_password_from_file_system_if_needed(app) conn = get_conn_through_sentinel() - response = conn.execute_command(*request_as_string.split()) + response = conn.execute_command(*command.split()) success = True except Exception as err: response = 'Exception: cannot connect. %s' % str(err) From 065cfbe4c75b244dafbe3b6330a5a2334c331773 Mon Sep 17 00:00:00 2001 From: hassankh148 Date: Wed, 24 Apr 2024 17:00:06 +0300 Subject: [PATCH 4/4] CR --- app.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index bca525c..166f43e 100644 --- a/app.py +++ b/app.py @@ -123,15 +123,15 @@ def execute(): @app.route('/batch_execute', methods=['POST']) def batch_execute(): all_succeeded = True - responses = dict() + responses = [] req = request.get_json() commands = req['commands'] for command in commands: response, success = _execute(command) - responses[command] = { + responses.append({ 'response': response, 'success': success, - } + }) if not success: all_succeeded = False return jsonify({