7
7
from transitions import State
8
8
9
9
from isar .mission_planner .task_selector_interface import TaskSelectorStop
10
+ from isar .config .settings import settings
10
11
from isar .services .utilities .threaded_request import (
11
12
ThreadedRequest ,
12
13
ThreadedRequestNotFinishedError ,
17
18
RobotMissionStatusException ,
18
19
RobotRetrieveInspectionException ,
19
20
RobotStepStatusException ,
21
+ RobotCommunicationTimeoutException ,
20
22
)
21
23
from robot_interface .models .inspection .inspection import Inspection
22
24
from robot_interface .models .mission .mission import Mission
@@ -32,6 +34,10 @@ class Monitor(State):
32
34
def __init__ (self , state_machine : "StateMachine" ) -> None :
33
35
super ().__init__ (name = "monitor" , on_enter = self .start , on_exit = self .stop )
34
36
self .state_machine : "StateMachine" = state_machine
37
+ self .request_status_failure_counter : int = 0
38
+ self .request_status_failure_counter_limit : int = (
39
+ settings .REQUEST_STATUS_FAILURE_COUNTER_LIMIT
40
+ )
35
41
36
42
self .logger = logging .getLogger ("state_machine" )
37
43
self .step_status_thread : Optional [ThreadedRequest ] = None
@@ -61,7 +67,6 @@ def _run(self) -> None:
61
67
status_function = self .state_machine .robot .step_status ,
62
68
thread_name = "State Machine Monitor Get Step Status" ,
63
69
)
64
-
65
70
try :
66
71
status : Union [StepStatus , MissionStatus ] = (
67
72
self .step_status_thread .get_output ()
@@ -70,6 +75,34 @@ def _run(self) -> None:
70
75
time .sleep (self .state_machine .sleep_time )
71
76
continue
72
77
78
+ except RobotCommunicationTimeoutException as e :
79
+ self .state_machine .current_mission .error_message = ErrorMessage (
80
+ error_reason = e .error_reason , error_description = e .error_description
81
+ )
82
+ self .step_status_thread = None
83
+ self .request_status_failure_counter += 1
84
+ self .logger .warning (
85
+ f"Monitoring step { self .state_machine .current_step .id } failed #: "
86
+ f"{ self .request_status_failure_counter } failed because: { e .error_description } "
87
+ )
88
+
89
+ if (
90
+ self .request_status_failure_counter
91
+ >= self .request_status_failure_counter_limit
92
+ ):
93
+ self .state_machine .current_step .error_message = ErrorMessage (
94
+ error_reason = e .error_reason ,
95
+ error_description = e .error_description ,
96
+ )
97
+ self .logger .error (
98
+ f"Step will be cancelled after failing to get step status "
99
+ f"{ self .request_status_failure_counter } times because: "
100
+ f"{ e .error_description } "
101
+ )
102
+ status = StepStatus .Failed
103
+ else :
104
+ continue
105
+
73
106
except RobotStepStatusException as e :
74
107
self .state_machine .current_step .error_message = ErrorMessage (
75
108
error_reason = e .error_reason , error_description = e .error_description
@@ -98,6 +131,8 @@ def _run(self) -> None:
98
131
f"Retrieving the status failed because: { e .error_description } "
99
132
)
100
133
134
+ self .request_status_failure_counter = 0
135
+
101
136
if isinstance (status , StepStatus ):
102
137
self .state_machine .current_step .status = status
103
138
elif isinstance (status , MissionStatus ):
0 commit comments