feat: refactor workflow retrieval to use a session for database operations

Signed-off-by: -LAN- <laipz8200@outlook.com>
This commit is contained in:
-LAN- 2025-03-03 15:03:13 +08:00
parent fd4b3433f6
commit 22d3484e44
No known key found for this signature in database
GPG Key ID: 6BA0D108DED011FF
2 changed files with 19 additions and 15 deletions

View File

@ -4,6 +4,7 @@ from typing import cast
from flask import abort, request from flask import abort, request
from flask_restful import Resource, inputs, marshal_with, reqparse # type: ignore from flask_restful import Resource, inputs, marshal_with, reqparse # type: ignore
from sqlalchemy.orm import Session
from werkzeug.exceptions import Forbidden, InternalServerError, NotFound from werkzeug.exceptions import Forbidden, InternalServerError, NotFound
import services import services
@ -14,6 +15,7 @@ from controllers.console.app.wraps import get_app_model
from controllers.console.wraps import account_initialization_required, setup_required from controllers.console.wraps import account_initialization_required, setup_required
from core.app.apps.base_app_queue_manager import AppQueueManager from core.app.apps.base_app_queue_manager import AppQueueManager
from core.app.entities.app_invoke_entities import InvokeFrom from core.app.entities.app_invoke_entities import InvokeFrom
from extensions.ext_database import db
from factories import variable_factory from factories import variable_factory
from fields.workflow_fields import workflow_fields, workflow_pagination_fields from fields.workflow_fields import workflow_fields, workflow_pagination_fields
from fields.workflow_run_fields import workflow_run_node_execution_fields from fields.workflow_run_fields import workflow_run_node_execution_fields
@ -505,7 +507,9 @@ class PublishedAllWorkflowApi(Resource):
user_id = cast(str, user_id) user_id = cast(str, user_id)
workflow_service = WorkflowService() workflow_service = WorkflowService()
with Session(db.engine) as session:
workflows, has_more = workflow_service.get_all_published_workflow( workflows, has_more = workflow_service.get_all_published_workflow(
session=session,
app_model=app_model, app_model=app_model,
page=page, page=page,
limit=limit, limit=limit,

View File

@ -83,6 +83,7 @@ class WorkflowService:
def get_all_published_workflow( def get_all_published_workflow(
self, self,
*, *,
session: Session,
app_model: App, app_model: App,
page: int, page: int,
limit: int, limit: int,
@ -109,7 +110,6 @@ class WorkflowService:
if named_only: if named_only:
stmt = stmt.where(Workflow.marked_name != "") stmt = stmt.where(Workflow.marked_name != "")
with Session(db.engine) as session:
workflows = session.scalars(stmt).all() workflows = session.scalars(stmt).all()
has_more = len(workflows) > limit has_more = len(workflows) > limit