ResInsight/GrpcInterface/RiaGrpcApplicationInterface.cpp
Kristian Bendiksen c2b5ab8d2c
#9307 Python: avoid assigning same port number to multiple grpc sessions
* Python: avoid assigning same port number to multiple grpc sessions
* Add retry count when checking for port number file
* Use grpc to find and use port number
* Add test used to start several instances of resinsight at the same time
Testing up to 50 instances works well

* Python: allow launch_port == 0 to assign port by GRPC in Instance.launch().
Also allow longer wait before failing the port number file reading:
it can take some time to launch when launching lots of instances at
the same time.
2022-09-26 14:19:21 +02:00

130 lines
4.6 KiB
C++

////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2021- Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RiaGrpcApplicationInterface.h"
#include "RiaPreferences.h"
#include "cvfProgramOptions.h"
#include <QProcessEnvironment>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiaGrpcApplicationInterface::initializeGrpcServer( const cvf::ProgramOptions& progOpt )
{
if ( !RiaPreferences::current()->enableGrpcServer() ) return false;
int defaultPortNumber = RiaPreferences::current()->defaultGrpcPortNumber();
bool useGrpcGeneratedPort = false;
if ( cvf::Option o = progOpt.option( "server" ) )
{
if ( o.valueCount() == 1 )
{
defaultPortNumber = o.value( 0 ).toInt( defaultPortNumber );
// If the port number is 0 it will be selected randomly by grpc.
if ( defaultPortNumber == 0 ) useGrpcGeneratedPort = true;
}
}
// Try to find an available port number starting from the default port
int portNumber = defaultPortNumber;
if ( !useGrpcGeneratedPort )
{
portNumber = RiaGrpcServer::findAvailablePortNumber( defaultPortNumber );
}
m_grpcServer = std::make_unique<RiaGrpcServer>( portNumber );
return true;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaGrpcApplicationInterface::launchGrpcServer()
{
if ( m_grpcServer ) m_grpcServer->runInThread();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiaGrpcServer* RiaGrpcApplicationInterface::grpcServer() const
{
return m_grpcServer.get();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QProcessEnvironment RiaGrpcApplicationInterface::grpcProcessEnvironment() const
{
QProcessEnvironment penv = QProcessEnvironment::systemEnvironment();
if ( m_grpcServer )
{
penv.insert( "RESINSIGHT_GRPC_PORT", QString( "%1" ).arg( m_grpcServer->portNumber() ) );
penv.insert( "RESINSIGHT_EXECUTABLE", QCoreApplication::applicationFilePath() );
QStringList ripsLocations;
QString separator;
#ifdef WIN32
ripsLocations << QCoreApplication::applicationDirPath() + "\\Python";
separator = ";";
#else
ripsLocations << QCoreApplication::applicationDirPath() + "/Python";
separator = ":";
#endif
penv.insert( "PYTHONPATH",
QString( "%1%2%3" ).arg( penv.value( "PYTHONPATH" ) ).arg( separator ).arg( ripsLocations.join( separator ) ) );
}
return penv;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RiaGrpcApplicationInterface::processRequests()
{
if ( RiaGrpcServer::receivedExitRequest() )
{
if ( m_grpcServer ) m_grpcServer->quit();
return -1;
}
if ( m_grpcServer )
{
size_t requestsProcessed = m_grpcServer->processAllQueuedRequests();
return static_cast<int>( requestsProcessed );
}
return 0;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
int RiaGrpcApplicationInterface::portNumber() const
{
if ( m_grpcServer ) return m_grpcServer->portNumber();
return -1;
}