#7136 Add more reconfigurable retry policy for initial connection.

This commit is contained in:
Kristian Bendiksen 2020-12-18 13:36:05 +01:00
parent b2ec95fba4
commit 956448bd18
2 changed files with 54 additions and 4 deletions

View File

@ -21,7 +21,7 @@ from Definitions_pb2 import Empty
import RiaVersionInfo
from .project import Project
from .retry_policy import FixedRetryPolicy
class Instance:
"""The ResInsight Instance class. Use to launch or find existing ResInsight instances
@ -183,12 +183,13 @@ class Instance:
connection_ok = False
version_ok = False
retry_policy = FixedRetryPolicy()
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()
if connection_ok:
break
time.sleep(1.0)
retry_policy.sleep(num_tries)
else:
connection_ok, version_ok = self.__check_version()
@ -196,7 +197,7 @@ class Instance:
if self.launched:
raise Exception('Error: Could not connect to resinsight at ',
location,
' after trying 10 times with 1 second apart')
'.', retry_policy.time_out_message())
raise Exception('Error: Could not connect to resinsight at ', location)
if not version_ok:
raise Exception('Error: Wrong Version of ResInsight at ', location,

View 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