Skip to content

Commit 1e651a3

Browse files
authored
Merge pull request #23 from RedisLabs/hassan-add-batch-execute-RED-124787
RED-124787 - add batch_execute
2 parents 19c5476 + 065cfbe commit 1e651a3

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM python:3.6.7
1+
FROM python:3.8
22

33
ENV FLASK_APP app.py
44
ENV APP_SETTINGS settings.cfg

app.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,33 +88,58 @@ def return_code(self):
8888
return self._return_code
8989

9090

91-
@app.route('/execute', methods=['POST'])
92-
def execute():
91+
def _execute(command: str):
9392
success = False
94-
req = request.get_json()
9593
try:
9694
conn = get_conn_through_sentinel()
97-
response = conn.execute_command(*req['command'].split())
95+
response = conn.execute_command(*command.split())
9896
success = True
9997
except (redis.exceptions.ConnectionError, redis.exceptions.ResponseError):
10098
try:
10199
reload_username_password_from_file_system_if_needed(app)
102100
conn = get_conn_through_sentinel()
103-
response = conn.execute_command(*req['command'].split())
101+
response = conn.execute_command(*command.split())
104102
success = True
105103
except Exception as err:
106104
response = 'Exception: cannot connect. %s' % str(err)
107105
app.logger.exception("execute err")
108106
except Exception as err:
109107
response = 'Exception: %s' % str(err)
110108
app.logger.exception("execute err")
109+
return response, success
110+
111+
112+
@app.route('/execute', methods=['POST'])
113+
def execute():
114+
req = request.get_json()
115+
response, success = _execute(req['command'])
111116

112117
return jsonify({
113118
'response': response,
114119
'success': success
115120
})
116121

117122

123+
@app.route('/batch_execute', methods=['POST'])
124+
def batch_execute():
125+
all_succeeded = True
126+
responses = []
127+
req = request.get_json()
128+
commands = req['commands']
129+
for command in commands:
130+
response, success = _execute(command)
131+
responses.append({
132+
'response': response,
133+
'success': success,
134+
})
135+
if not success:
136+
all_succeeded = False
137+
return jsonify({
138+
'response': responses,
139+
'success': all_succeeded
140+
})
141+
142+
118143
def reload_username_password_from_file_system_if_needed(app):
119144
# It may be that the dynamic password was changed since the config was set
120145
if should_read_from_file_system():

0 commit comments

Comments
 (0)