Skip to content

Commit

Permalink
Merge pull request #23 from qua-platform/fix/run-modes
Browse files Browse the repository at this point in the history
avoid running node through library
  • Loading branch information
maxim-v4s authored Sep 25, 2024
2 parents 83fb120 + ff8bb40 commit 1ee7acf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 40 deletions.
21 changes: 10 additions & 11 deletions qualibrate_runner/api/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,29 @@ def get_state() -> State:


@cache
def get_library(
def get_cached_library(
settings: Annotated[QualibrateRunnerSettings, Depends(get_settings)],
) -> QualibrationLibrary:
return settings.calibration_library_resolver(
settings.calibration_library_folder
)


@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]:
Expand All @@ -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()
20 changes: 0 additions & 20 deletions qualibrate_runner/api/routes/get_runnables.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -20,38 +16,22 @@
from qualibrate_runner.api.dependencies import (
get_nodes as get_qnodes,
)
from qualibrate_runner.config import (
QualibrateRunnerSettings,
get_settings,
)

get_runnables_router = APIRouter()


@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()
Expand Down
20 changes: 11 additions & 9 deletions qualibrate_runner/core/run_job.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
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
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 (
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -104,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:
Expand Down

0 comments on commit 1ee7acf

Please sign in to comment.