mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#7136 Add more reconfigurable retry policy for initial connection.
This commit is contained in:
parent
b2ec95fba4
commit
956448bd18
@ -21,7 +21,7 @@ from Definitions_pb2 import Empty
|
|||||||
import RiaVersionInfo
|
import RiaVersionInfo
|
||||||
|
|
||||||
from .project import Project
|
from .project import Project
|
||||||
|
from .retry_policy import FixedRetryPolicy
|
||||||
|
|
||||||
class Instance:
|
class Instance:
|
||||||
"""The ResInsight Instance class. Use to launch or find existing ResInsight instances
|
"""The ResInsight Instance class. Use to launch or find existing ResInsight instances
|
||||||
@ -183,12 +183,13 @@ class Instance:
|
|||||||
connection_ok = False
|
connection_ok = False
|
||||||
version_ok = False
|
version_ok = False
|
||||||
|
|
||||||
|
retry_policy = FixedRetryPolicy()
|
||||||
if self.launched:
|
if self.launched:
|
||||||
for _ in range(0, 10):
|
for num_tries in range(0, retry_policy.num_retries()):
|
||||||
connection_ok, version_ok = self.__check_version()
|
connection_ok, version_ok = self.__check_version()
|
||||||
if connection_ok:
|
if connection_ok:
|
||||||
break
|
break
|
||||||
time.sleep(1.0)
|
retry_policy.sleep(num_tries)
|
||||||
else:
|
else:
|
||||||
connection_ok, version_ok = self.__check_version()
|
connection_ok, version_ok = self.__check_version()
|
||||||
|
|
||||||
@ -196,7 +197,7 @@ class Instance:
|
|||||||
if self.launched:
|
if self.launched:
|
||||||
raise Exception('Error: Could not connect to resinsight at ',
|
raise Exception('Error: Could not connect to resinsight at ',
|
||||||
location,
|
location,
|
||||||
' after trying 10 times with 1 second apart')
|
'.', retry_policy.time_out_message())
|
||||||
raise Exception('Error: Could not connect to resinsight at ', location)
|
raise Exception('Error: Could not connect to resinsight at ', location)
|
||||||
if not version_ok:
|
if not version_ok:
|
||||||
raise Exception('Error: Wrong Version of ResInsight at ', location,
|
raise Exception('Error: Wrong Version of ResInsight at ', location,
|
||||||
|
49
ApplicationCode/GrpcInterface/Python/rips/retry_policy.py
Normal file
49
ApplicationCode/GrpcInterface/Python/rips/retry_policy.py
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
import abc
|
||||||
|
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
|
class RetryPolicy(abc.ABC):
|
||||||
|
@abc.abstractmethod
|
||||||
|
def sleep(self, retry_num):
|
||||||
|
"""
|
||||||
|
How long to sleep in milliseconds.
|
||||||
|
:param retry_num: the number of retry (starting from zero)
|
||||||
|
"""
|
||||||
|
assert retry_num >= 0
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
def time_out_message(self):
|
||||||
|
"""
|
||||||
|
Generate a error message for user on time out.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abc.abstractmethod
|
||||||
|
def num_retries(self):
|
||||||
|
"""
|
||||||
|
Max number retries.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class FixedRetryPolicy(RetryPolicy):
|
||||||
|
def __init__(self, sleep_time=1000, max_num_retries=10):
|
||||||
|
"""
|
||||||
|
Create a fixed time retry policy.
|
||||||
|
:param sleep_time: time to sleep in milliseconds.
|
||||||
|
:param max_num_retries: max number of retries.
|
||||||
|
"""
|
||||||
|
self.sleep_time = sleep_time
|
||||||
|
self.max_num_retries = max_num_retries
|
||||||
|
|
||||||
|
def sleep(self, retry_num):
|
||||||
|
time.sleep(self.sleep_time / 1000)
|
||||||
|
|
||||||
|
def time_out_message(self):
|
||||||
|
return "Tried {} times with {} milliseconds apart.".format(
|
||||||
|
self.max_num_retries, self.sleep_time
|
||||||
|
)
|
||||||
|
|
||||||
|
def num_retries(self):
|
||||||
|
return self.max_num_retries
|
Loading…
Reference in New Issue
Block a user