From 11cb80d5853955ca01403a0cf2588b1d654ad9cd Mon Sep 17 00:00:00 2001 From: Kastan Day Date: Mon, 1 Apr 2024 15:20:47 -0700 Subject: [PATCH] Restructuring to handle DB locks --- ai_ta_backend/flows.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/ai_ta_backend/flows.py b/ai_ta_backend/flows.py index 1f103e99..c43f1f92 100644 --- a/ai_ta_backend/flows.py +++ b/ai_ta_backend/flows.py @@ -208,10 +208,20 @@ def main_flow(self, name: str, api_key: str = "", data: str = ""): raise Exception('No executions found') id = str(id) + # start job try: - self.supabase_client.table('n8n_api_keys').insert({"id": id}).execute() + start_time = time.monotonic() + self.supabase_client.table('n8n_api_keys').insert({"in_progress_workflow_id": id}).execute() self.execute_flow(hook, new_data) print("Executed") + print(f"⏰ Runtime to execute_flow(): {(time.monotonic() - start_time):.4f} seconds") + except: + pass + finally: + # TODO: Remove lock from Supabase table. + pass + + try: executions = self.get_executions(20, id, True, api_key) print("Got executions", executions) while executions is None: @@ -220,10 +230,11 @@ def main_flow(self, name: str, api_key: str = "", data: str = ""): print("Can't find id in executions") time.sleep(1) print("Found id in executions ") - self.supabase_client.table('n8n_api_keys').delete().eq('id', id).execute() + self.supabase_client.table('n8n_api_keys').delete().eq('in_progress_workflow_id', id).execute() print("Deleted id") print("Returning") except Exception as e: - self.supabase_client.table('n8n_api_keys').delete().eq('id', id).execute() + self.supabase_client.table('n8n_api_keys').delete().eq('in_progress_workflow_id', id).execute() return {"error": str(e)} + return executions