From 5ce921c3794c9372e26f07af3679391addec739d Mon Sep 17 00:00:00 2001 From: Serwan Asaad Date: Tue, 24 Sep 2024 20:38:02 +0200 Subject: [PATCH 1/2] avoid running node through library --- qualibrate_runner/core/run_job.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/qualibrate_runner/core/run_job.py b/qualibrate_runner/core/run_job.py index 46e8542..a66350e 100644 --- a/qualibrate_runner/core/run_job.py +++ b/qualibrate_runner/core/run_job.py @@ -1,6 +1,6 @@ import traceback from datetime import datetime -from typing import Any, Mapping, Type +from typing import Any, Mapping, Type, cast from fastapi import HTTPException, status from pydantic import BaseModel, ValidationError @@ -8,6 +8,7 @@ from qualibrate.qualibration_library import QualibrationLibrary from qualibrate.qualibration_node import QualibrationNode +from qualibrate.run_summary.node import NodeRunSummary from qualibrate_runner.config import State from qualibrate_runner.core.models.last_run import ( LastRun, @@ -50,11 +51,9 @@ def run_node( runnable_type=RunnableType.NODE, ) try: - library = get_active_library_or_error() - node = library.nodes[node.name] - result = library.run_node( - node.name, node.parameters_class(**passed_input_parameters) - ) + result = node.run(passed_parameters=passed_input_parameters) + result = cast(NodeRunSummary, result) + node = QualibrationNode.last_executed_node except Exception as ex: state.last_run = LastRun( name=state.last_run.name, From ff8bb40e2cd1ce0d2f20bc1451e9b033e1034206 Mon Sep 17 00:00:00 2001 From: Maxim V4S Date: Wed, 25 Sep 2024 11:39:43 +0300 Subject: [PATCH 2/2] feat: use library rescan --- qualibrate_runner/api/dependencies.py | 21 +++++++++---------- qualibrate_runner/api/routes/get_runnables.py | 20 ------------------ qualibrate_runner/core/run_job.py | 11 ++++++---- 3 files changed, 17 insertions(+), 35 deletions(-) diff --git a/qualibrate_runner/api/dependencies.py b/qualibrate_runner/api/dependencies.py index f0624d5..1e7c8a8 100644 --- a/qualibrate_runner/api/dependencies.py +++ b/qualibrate_runner/api/dependencies.py @@ -19,7 +19,7 @@ def get_state() -> State: @cache -def get_library( +def get_cached_library( settings: Annotated[QualibrateRunnerSettings, Depends(get_settings)], ) -> QualibrationLibrary: return settings.calibration_library_resolver( @@ -27,14 +27,21 @@ def get_library( ) -@cache +def get_library( + library: Annotated[QualibrationLibrary, Depends(get_cached_library)], + rescan: bool = False, +) -> QualibrationLibrary: + if rescan: + library.rescan() + return library + + def get_nodes( library: Annotated[QualibrationLibrary, Depends(get_library)], ) -> Mapping[str, QualibrationNode]: return cast(Mapping[str, QualibrationNode], library.get_nodes()) -@cache def get_graphs( library: Annotated[QualibrationLibrary, Depends(get_library)], ) -> Mapping[str, QualibrationGraph]: @@ -61,11 +68,3 @@ def get_graph( status_code=422, detail=f"Unknown graph name {name}" ) return graph - - -CACHED_DEPENDENCIES = (get_library, get_nodes, get_graphs) - - -def cache_clear() -> None: - for func in CACHED_DEPENDENCIES: - func.cache_clear() diff --git a/qualibrate_runner/api/routes/get_runnables.py b/qualibrate_runner/api/routes/get_runnables.py index 37da21e..075423e 100644 --- a/qualibrate_runner/api/routes/get_runnables.py +++ b/qualibrate_runner/api/routes/get_runnables.py @@ -4,10 +4,6 @@ from qualibrate.qualibration_graph import QualibrationGraph from qualibrate.qualibration_node import QualibrationNode -from qualibrate_runner.api.dependencies import ( - cache_clear, - get_library, -) from qualibrate_runner.api.dependencies import ( get_graph as get_qgraph, ) @@ -20,10 +16,6 @@ from qualibrate_runner.api.dependencies import ( get_nodes as get_qnodes, ) -from qualibrate_runner.config import ( - QualibrateRunnerSettings, - get_settings, -) get_runnables_router = APIRouter() @@ -31,27 +23,15 @@ @get_runnables_router.get("/get_nodes") def get_nodes( nodes: Annotated[Mapping[str, QualibrationNode], Depends(get_qnodes)], - settings: Annotated[QualibrateRunnerSettings, Depends(get_settings)], - rescan: bool = False, ) -> Mapping[str, Any]: - if rescan: - cache_clear() - library = get_library(settings) - nodes = get_qnodes(library) return {node_name: node.serialize() for node_name, node in nodes.items()} @get_runnables_router.get("/get_graphs") def get_graphs( graphs: Annotated[Mapping[str, QualibrationNode], Depends(get_qgraphs)], - settings: Annotated[QualibrateRunnerSettings, Depends(get_settings)], - rescan: bool = False, cytoscape: bool = False, ) -> Mapping[str, Any]: - if rescan: - cache_clear() - library = get_library(settings) - graphs = get_qgraphs(library) return { graph_name: graph.serialize(cytoscape=cytoscape) for graph_name, graph in graphs.items() diff --git a/qualibrate_runner/core/run_job.py b/qualibrate_runner/core/run_job.py index a66350e..e56151a 100644 --- a/qualibrate_runner/core/run_job.py +++ b/qualibrate_runner/core/run_job.py @@ -7,8 +7,8 @@ from qualibrate.qualibration_graph import QualibrationGraph from qualibrate.qualibration_library import QualibrationLibrary from qualibrate.qualibration_node import QualibrationNode - from qualibrate.run_summary.node import NodeRunSummary + from qualibrate_runner.config import State from qualibrate_runner.core.models.last_run import ( LastRun, @@ -103,9 +103,12 @@ def run_workflow( try: library = get_active_library_or_error() workflow = library.graphs[workflow.name] - result = library.run_graph( - workflow.name, - workflow.full_parameters_class(**passed_input_parameters), + input_parameters = workflow.full_parameters_class( + **passed_input_parameters + ) + result = workflow.run( + nodes=input_parameters.nodes.model_dump(), + **input_parameters.parameters.model_dump(), ) print("Graph completed. Result:", result) except Exception as ex: