Python: wait for ResInsight to start

This commit is contained in:
Gaute Lindkvist 2019-06-06 09:32:44 +02:00
parent 57e282eb7e
commit c075f7b352

View File

@ -3,6 +3,7 @@ import os
import sys import sys
import socket import socket
import logging import logging
import time
sys.path.insert(1, os.path.join(sys.path[0], '../generated')) sys.path.insert(1, os.path.join(sys.path[0], '../generated'))
@ -44,7 +45,9 @@ class Instance:
if console: if console:
parameters += " --console" parameters += " --console"
pid = os.spawnl(os.P_NOWAIT, resInsightExecutable, parameters) pid = os.spawnl(os.P_NOWAIT, resInsightExecutable, parameters)
return Instance(port=port, launched=True) if pid:
return Instance(port=port, launched=True)
return None
@staticmethod @staticmethod
def find(startPort = 50051, endPort = 50071): def find(startPort = 50051, endPort = 50071):
@ -60,25 +63,42 @@ class Instance:
print('Error: Could not find any ResInsight instances responding between ports ' + str(startPort) + ' and ' + str(endPort)) print('Error: Could not find any ResInsight instances responding between ports ' + str(startPort) + ' and ' + str(endPort))
return None return None
def checkVersion(self):
try:
majorVersionOk = self.app.majorVersion() == int(RiaVersionInfo.RESINSIGHT_MAJOR_VERSION)
minorVersionOk = self.app.minorVersion() == int(RiaVersionInfo.RESINSIGHT_MINOR_VERSION)
return True, majorVersionOk and minorVersionOk
except grpc.RpcError as e:
return False, False
def __init__(self, port = 50051, launched = False): def __init__(self, port = 50051, launched = False):
logging.basicConfig() logging.basicConfig()
location = "localhost:" + str(port) location = "localhost:" + str(port)
self.channel = grpc.insecure_channel(location) self.channel = grpc.insecure_channel(location)
self.launched = launched self.launched = launched
# Main version check package # Main version check package
self.app = App(self.channel) self.app = App(self.channel)
try:
majorVersionOk = self.app.majorVersion() == int(RiaVersionInfo.RESINSIGHT_MAJOR_VERSION) connectionOk = False
minorVersionOk = self.app.minorVersion() == int(RiaVersionInfo.RESINSIGHT_MINOR_VERSION) versionOk = False
if not (majorVersionOk and minorVersionOk):
raise Exception('Version of ResInsight does not match version of Python API') if launched:
except grpc.RpcError as e: for i in range(0, 5):
if e.code() == grpc.StatusCode.UNAVAILABLE: connectionOk, versionOk = self.checkVersion()
print('Info: Could not find any instances at port ' + str(port)) if connectionOk:
except Exception as e: break
print('Error:', e) time.sleep(0.5)
else:
connectionOk, versionOk = self.checkVersion()
if not connectionOk:
raise Exception('Error: Could not connect to resinsight at ', location)
exit(1)
if not versionOk:
raise Exception('Error: Wrong Version of ResInsight at ', location)
# Service packages # Service packages
self.commands = Commands(self.channel) self.commands = Commands(self.channel)
self.project = Project(self.channel) self.project = Project(self.channel)