2020from uuid import uuid4
2121
2222from aiohttp import web
23- from common_library .json_serialization import json_dumps , json_loads
23+ from common_library .json_serialization import json_dumps
2424from models_library .api_schemas_clusters_keeper .ec2_instances import EC2InstanceTypeGet
2525from models_library .api_schemas_directorv2 .dynamic_services import (
2626 DynamicServiceGet ,
@@ -1831,16 +1831,24 @@ async def add_project_states_for_user(
18311831 f"{ user_id = } " ,
18321832 f"{ project ['uuid' ]= } " ,
18331833 )
1834- # for templates: the project is never locked and never opened. also the running state is always unknown
1835- share_state = await _get_project_share_state (user_id , project ["uuid" ], app )
1836- project_running_state = RunningState .UNKNOWN
1837- computational_node_states : dict [NodeID , NodeState ] = {}
1838- if computation_task := await director_v2_service .get_computation_task (
1839- app , user_id , project ["uuid" ]
1840- ):
1841- project_running_state = computation_task .state
1842- computational_node_states = computation_task .pipeline_details .node_states
1834+ project_share_state , user_computation_task = await asyncio .gather (
1835+ _get_project_share_state (user_id , project ["uuid" ], app ),
1836+ director_v2_service .get_computation_task (app , user_id , project ["uuid" ]),
1837+ )
1838+ # retrieve the project computational state
1839+ # if the user has no computation task, we assume the project is not running
1840+ project_running_state = (
1841+ user_computation_task .state
1842+ if user_computation_task
1843+ else RunningState .NOT_STARTED
1844+ )
1845+ computational_node_states = (
1846+ user_computation_task .pipeline_details .node_states
1847+ if user_computation_task
1848+ else {}
1849+ )
18431850
1851+ # compose the node states
18441852 for node_uuid , node in project ["workbench" ].items ():
18451853 assert isinstance (node_uuid , str ) # nosec
18461854 assert isinstance (node , dict ) # nosec
@@ -1851,22 +1859,33 @@ async def add_project_states_for_user(
18511859 project_uuid = project ["uuid" ],
18521860 node_id = NodeID (node_uuid ),
18531861 )
1862+ if NodeID (node_uuid ) in computational_node_states :
1863+ node_state = computational_node_states [NodeID (node_uuid )].model_copy (
1864+ update = {"lock_state" : node_lock_state }
1865+ )
1866+ else :
1867+ # if the node is not in the computational state, we create a new one
1868+ service_is_running = node_lock_state .status is NodeShareStatus .OPENED
1869+ node_state = NodeState (
1870+ current_status = (
1871+ RunningState .STARTED
1872+ if service_is_running
1873+ else RunningState .NOT_STARTED
1874+ ),
1875+ lock_state = node_lock_state ,
1876+ )
18541877
1855- # create complete node state
1856- node_state = computational_node_states .get (
1857- NodeID (node_uuid ), NodeState (current_status = RunningState .UNKNOWN )
1858- )
1859- node_state .lock_state = node_lock_state
1860- node_state_dict = json_loads (
1878+ # upgrade the project
1879+ node .setdefault ("state" , {}).update (
18611880 node_state .model_dump_json (by_alias = True , exclude_unset = True )
18621881 )
1863- node .setdefault ("state" , {}).update (node_state_dict )
18641882 if "progress" in node ["state" ] and node ["state" ]["progress" ] is not None :
18651883 # ensure progress is a percentage
18661884 node ["progress" ] = round (node ["state" ]["progress" ] * 100.0 )
18671885
18681886 project ["state" ] = ProjectState (
1869- share_state = share_state , state = ProjectRunningState (value = project_running_state )
1887+ share_state = project_share_state ,
1888+ state = ProjectRunningState (value = project_running_state ),
18701889 ).model_dump (by_alias = True , exclude_unset = True )
18711890 return project
18721891
0 commit comments