Browse Source

fix: optimize query for expired workflow runs by adding date filter and limiting results (#16491)

Yeuoly 1 tháng trước cách đây
mục cha
commit
1907d2a90a
1 tập tin đã thay đổi với 9 bổ sung1 xóa
  1. 9 1
      api/services/clear_free_plan_tenant_expired_logs.py

+ 9 - 1
api/services/clear_free_plan_tenant_expired_logs.py

@@ -149,7 +149,15 @@ class ClearFreePlanTenantExpiredLogs:
 
             while True:
                 with Session(db.engine).no_autoflush as session:
-                    workflow_runs = session.query(WorkflowRun).filter(WorkflowRun.tenant_id == tenant_id).all()
+                    workflow_runs = (
+                        session.query(WorkflowRun)
+                        .filter(
+                            WorkflowRun.tenant_id == tenant_id,
+                            WorkflowRun.created_at < datetime.datetime.now() - datetime.timedelta(days=days),
+                        )
+                        .limit(batch)
+                        .all()
+                    )
 
                     if len(workflow_runs) == 0:
                         break