33Using 'retrying' library to do polling:
44https://pypi.org/project/retrying/
55"""
6- from typing import List , Callable
7- from retrying import retry , RetryError # pip install retrying
8- from cloudshell .sandbox_rest .sandbox_api import SandboxRestApiSession
96from enum import Enum
7+ from typing import Callable , List
8+
9+ from retrying import RetryError , retry # pip install retrying
10+
11+ from cloudshell .sandbox_rest .sandbox_api import SandboxRestApiSession
1012
1113
1214class OrchestrationPollingTimeout (Exception ):
@@ -18,8 +20,8 @@ class CommandPollingTimeout(Exception):
1820
1921
2022class SandboxStates (Enum ):
21- before_setup_state = "BeforeSetup"
22- running_setup_state = "Setup"
23+ BEFORE_SETUP_STATE = "BeforeSetup"
24+ RUNNING_SETUP_STATE = "Setup"
2325 error_state = "Error"
2426 ready_state = "Ready"
2527 teardown_state = "Teardown"
@@ -33,19 +35,16 @@ class ExecutionStatuses(Enum):
3335 failed_status = "Failed"
3436
3537
36- SANDBOX_SETUP_STATES = [SandboxStates .before_setup_state .value ,
37- SandboxStates .running_setup_state .value ]
38+ SANDBOX_SETUP_STATES = [SandboxStates .BEFORE_SETUP_STATE .value , SandboxStates .RUNNING_SETUP_STATE .value ]
3839
39- SANDBOX_ACTIVE_STATES = [SandboxStates .ready_state .value ,
40- SandboxStates .error_state .value ]
40+ SANDBOX_ACTIVE_STATES = [SandboxStates .ready_state .value , SandboxStates .error_state .value ]
4141
42- UNFINISHED_EXECUTION_STATUSES = [ExecutionStatuses .running_status .value ,
43- ExecutionStatuses .pending_status .value ]
42+ UNFINISHED_EXECUTION_STATUSES = [ExecutionStatuses .running_status .value , ExecutionStatuses .pending_status .value ]
4443
4544
4645def _should_we_keep_polling_setup (sandbox_details : dict ) -> bool :
4746 """ if still in setup keep polling """
48- setup_states = [SandboxStates .before_setup_state .value , SandboxStates .running_setup_state .value ]
47+ setup_states = [SandboxStates .BEFORE_SETUP_STATE .value , SandboxStates .RUNNING_SETUP_STATE .value ]
4948 if sandbox_details ["state" ] in setup_states :
5049 return True
5150 return False
@@ -58,13 +57,19 @@ def _should_we_keep_polling_teardown(sandbox_details: dict) -> bool:
5857 return False
5958
6059
61- def _poll_sandbox_state (api : SandboxRestApiSession , reservation_id : str , polling_func : Callable ,
62- max_polling_minutes : int , polling_frequency_seconds : int ) -> str :
60+ def _poll_sandbox_state (
61+ api : SandboxRestApiSession ,
62+ reservation_id : str ,
63+ polling_func : Callable ,
64+ max_polling_minutes : int ,
65+ polling_frequency_seconds : int ,
66+ ) -> str :
6367 """ Create blocking polling process """
6468
6569 # retry wait times are in milliseconds
66- @retry (retry_on_result = polling_func , wait_fixed = polling_frequency_seconds * 1000 ,
67- stop_max_delay = max_polling_minutes * 60000 )
70+ @retry (
71+ retry_on_result = polling_func , wait_fixed = polling_frequency_seconds * 1000 , stop_max_delay = max_polling_minutes * 60000
72+ )
6873 def get_sandbox_details ():
6974 return api .get_sandbox_details (reservation_id )
7075
@@ -75,19 +80,23 @@ def get_sandbox_details():
7580 return sandbox_details
7681
7782
78- def poll_sandbox_setup (api : SandboxRestApiSession , reservation_id : str , max_polling_minutes = 20 ,
79- polling_frequency_seconds = 30 ) -> dict :
83+ def poll_sandbox_setup (
84+ api : SandboxRestApiSession , reservation_id : str , max_polling_minutes = 20 , polling_frequency_seconds = 30
85+ ) -> dict :
8086 """ poll until completion """
81- sandbox_details = _poll_sandbox_state (api , reservation_id , _should_we_keep_polling_setup , max_polling_minutes ,
82- polling_frequency_seconds )
87+ sandbox_details = _poll_sandbox_state (
88+ api , reservation_id , _should_we_keep_polling_setup , max_polling_minutes , polling_frequency_seconds
89+ )
8390 return sandbox_details
8491
8592
86- def poll_sandbox_teardown (api : SandboxRestApiSession , reservation_id : str , max_polling_minutes = 20 ,
87- polling_frequency_seconds = 30 ) -> dict :
93+ def poll_sandbox_teardown (
94+ api : SandboxRestApiSession , reservation_id : str , max_polling_minutes = 20 , polling_frequency_seconds = 30
95+ ) -> dict :
8896 """ poll until completion """
89- sandbox_details = _poll_sandbox_state (api , reservation_id , _should_we_keep_polling_teardown , max_polling_minutes ,
90- polling_frequency_seconds )
97+ sandbox_details = _poll_sandbox_state (
98+ api , reservation_id , _should_we_keep_polling_teardown , max_polling_minutes , polling_frequency_seconds
99+ )
91100 return sandbox_details
92101
93102
@@ -98,15 +107,19 @@ def _should_we_keep_polling_execution(exc_data: dict) -> bool:
98107 return False
99108
100109
101- def poll_execution_for_completion (sandbox_rest : SandboxRestApiSession , command_execution_id : str ,
102- max_polling_in_minutes = 20 , polling_frequency_in_seconds = 30 ) -> str :
110+ def poll_execution_for_completion (
111+ sandbox_rest : SandboxRestApiSession , command_execution_id : str , max_polling_in_minutes = 20 , polling_frequency_in_seconds = 30
112+ ) -> str :
103113 """
104114 poll execution for "Completed" status, then return the execution output
105115 """
106116
107117 # retry wait times are in milliseconds
108- @retry (retry_on_result = _should_we_keep_polling_execution , wait_fixed = polling_frequency_in_seconds * 1000 ,
109- stop_max_delay = max_polling_in_minutes * 60000 )
118+ @retry (
119+ retry_on_result = _should_we_keep_polling_execution ,
120+ wait_fixed = polling_frequency_in_seconds * 1000 ,
121+ stop_max_delay = max_polling_in_minutes * 60000 ,
122+ )
110123 def get_execution_data ():
111124 exc_data = sandbox_rest .get_execution_details (command_execution_id )
112125 return exc_data
0 commit comments