@@ -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+
118143def 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