Skip to content
Snippets Groups Projects
Commit 2801c10d authored by Nathan Hertz's avatar Nathan Hertz Committed by Nathan Hertz
Browse files

Capability version state is now correctly set to Complete and Failed

when it is desired
parent 6c342cd4
No related branches found
No related tags found
No related merge requests found
......@@ -25,7 +25,7 @@ import json
import logging
import pathlib
import re
from typing import Dict, List
from typing import Dict, List, Optional
import pendulum
import requests
......@@ -239,15 +239,42 @@ class State(Base):
transition.actions.append(action)
self.transitions.append(transition)
def signal(self, message: Dict, execution: CapabilityExecutionIF, manager: ExecutionManagerIF) -> State:
def find_matching_transition(self, message: Dict) -> Optional[Transition]:
"""
Find matching transition for state based on a given message
:param message: Message to match transition with
:return: Matching transition if one is found; else None
"""
for transition in self.transitions:
if transition.matches(message):
logger.info("Found matching transition")
transition.action(execution, manager)
return transition.to_state
logger.info("No matching transitions found")
return transition
return None
def get_next_state(self, message: Dict) -> State:
"""
Get next state for current transition, given a message
:param message: Message to match transition with
:return: Next state if matching transition is found; else current state
"""
transition = self.find_matching_transition(message)
if transition:
return transition.to_state
return self
def perform_action(self, message: Dict, execution: CapabilityExecutionIF, manager: ExecutionManagerIF):
"""
Perform action for the current transition, if one exists
:param message: Message to match transition with
:param execution: Execution that is performing the action
:param manager: Execution manager to send to the action
"""
transition = self.find_matching_transition(message)
if transition:
transition.action(execution, manager)
def final(self) -> bool:
return not self.transitions
......@@ -708,4 +735,8 @@ class CapabilityExecution(Base, CapabilityExecutionIF, JSONSerializable):
def signal(self, manager: ExecutionManagerIF, message: Dict):
logger.info("Signalling my state %s with this message of type %s", self.state_name, message["type"])
self.state = self.state.signal(message, self, manager)
previous_state = self.state
# Transition execution state
self.state = self.state.get_next_state(message)
# Perform action
previous_state.perform_action(message, self, manager)
......@@ -70,15 +70,16 @@ class CapabilityService(CapabilityServiceIF):
def complete_request(self, **message: Dict):
logger.info(f"RECEIVED EXECUTION-COMPLETE: {message}")
# Set request state to Complete
execution = message["subject"]
# Update request state
capability_request = self.capability_info.lookup_capability_request(execution["capability_request_id"])
capability_request.determine_state()
self.capability_info.save_entity(capability_request)
# Set version state to Complete
# Set version state to Complete if the execution is indeed complete; else, leave it alone
version = self.capability_info.lookup_version(capability_request.id, execution["version_number"])
version.state = CapabilityVersionState.Complete.name
version.state = CapabilityVersionState.Complete.name if execution["state_name"] == "Complete" else version.state
self.capability_info.save_entity(version)
capability_complete_msg = CapabilityMessageArchitect(request=capability_request).compose_message(
......@@ -92,8 +93,7 @@ class CapabilityService(CapabilityServiceIF):
execution = message["subject"]
# Set request state to Failed
# TODO(nhertz): Dynamically calculate request state based on the states of its versions
# Update request state
capability_request = self.capability_info.lookup_capability_request(execution["capability_request_id"])
capability_request.determine_state()
self.capability_info.save_entity(capability_request)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment