mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
Fault Reactivation Assessment implementation (ref issue #7321)
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
|
||||
set (SOURCE_GROUP_HEADER_FILES
|
||||
${CMAKE_CURRENT_LIST_DIR}/RimProcess.h
|
||||
${CMAKE_CURRENT_LIST_DIR}/RimProcessMonitor.h
|
||||
)
|
||||
|
||||
set (SOURCE_GROUP_SOURCE_FILES
|
||||
${CMAKE_CURRENT_LIST_DIR}/RimProcess.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RimProcessMonitor.cpp
|
||||
)
|
||||
|
||||
list(APPEND CODE_HEADER_FILES
|
||||
${SOURCE_GROUP_HEADER_FILES}
|
||||
)
|
||||
|
||||
list(APPEND CODE_SOURCE_FILES
|
||||
${SOURCE_GROUP_SOURCE_FILES}
|
||||
)
|
||||
|
||||
list(APPEND COMMAND_QT_MOC_HEADERS
|
||||
${CMAKE_CURRENT_LIST_DIR}/RimProcessMonitor.h
|
||||
)
|
||||
|
||||
source_group( "ProjectDataModel\\ProcessControl" FILES ${SOURCE_GROUP_HEADER_FILES} ${SOURCE_GROUP_SOURCE_FILES} ${CMAKE_CURRENT_LIST_DIR}/CMakeLists_files.cmake )
|
||||
@@ -0,0 +1,210 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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 "RimProcess.h"
|
||||
|
||||
#include "RiaLogging.h"
|
||||
#include "RimProcessMonitor.h"
|
||||
|
||||
#include "cafPdmFieldCapability.h"
|
||||
|
||||
#include <QProcess>
|
||||
|
||||
CAF_PDM_SOURCE_INIT( RimProcess, "RimProcess" );
|
||||
|
||||
int RimProcess::m_nextProcessId = 1;
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
RimProcess::RimProcess()
|
||||
{
|
||||
int defId = m_nextProcessId++;
|
||||
m_monitor = new RimProcessMonitor( defId );
|
||||
|
||||
CAF_PDM_InitObject( "ResInsight Process", ":/Erase.png", "", "" );
|
||||
|
||||
CAF_PDM_InitFieldNoDefault( &m_command, "Command", "Command", "", "", "" );
|
||||
m_command.uiCapability()->setUiReadOnly( true );
|
||||
|
||||
CAF_PDM_InitFieldNoDefault( &m_description, "Description", "Description", "", "", "" );
|
||||
m_description.uiCapability()->setUiReadOnly( true );
|
||||
|
||||
CAF_PDM_InitField( &m_id, "ID", defId, "ID", "", "", "" );
|
||||
m_id.uiCapability()->setUiReadOnly( true );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
RimProcess::~RimProcess()
|
||||
{
|
||||
delete m_monitor;
|
||||
}
|
||||
|
||||
caf::PdmFieldHandle* RimProcess::userDescriptionField()
|
||||
{
|
||||
return &m_description;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RimProcess::addParameter( QString paramStr )
|
||||
{
|
||||
m_arguments << paramStr.trimmed();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RimProcess::setParameters( QStringList parameterList )
|
||||
{
|
||||
m_arguments.clear();
|
||||
for ( int i = 0; i < parameterList.size(); i++ )
|
||||
{
|
||||
addParameter( parameterList[i] );
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RimProcess::setCommand( QString cmdStr )
|
||||
{
|
||||
m_command = cmdStr;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
void RimProcess::setDescription( QString desc )
|
||||
{
|
||||
m_description = desc;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RimProcess::command() const
|
||||
{
|
||||
return m_command;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QStringList RimProcess::parameters() const
|
||||
{
|
||||
return m_arguments;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
int RimProcess::ID() const
|
||||
{
|
||||
return m_id;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RimProcess::execute()
|
||||
{
|
||||
QProcess* proc = new QProcess();
|
||||
QString cmd = commandLine();
|
||||
|
||||
RiaLogging::info( QString( "Start process %1: %2" ).arg( m_id ).arg( cmd ) );
|
||||
|
||||
QObject::connect( proc,
|
||||
SIGNAL( finished( int, QProcess::ExitStatus ) ),
|
||||
m_monitor,
|
||||
SLOT( finished( int, QProcess::ExitStatus ) ) );
|
||||
QObject::connect( proc, SIGNAL( readyReadStandardOutput() ), m_monitor, SLOT( readyReadStandardOutput() ) );
|
||||
QObject::connect( proc, SIGNAL( readyReadStandardError() ), m_monitor, SLOT( readyReadStandardError() ) );
|
||||
QObject::connect( proc, SIGNAL( started() ), m_monitor, SLOT( started() ) );
|
||||
|
||||
bool retval = false;
|
||||
|
||||
proc->start( cmd );
|
||||
if ( proc->waitForStarted( -1 ) )
|
||||
{
|
||||
while ( !proc->waitForFinished( 500 ) )
|
||||
{
|
||||
QApplication::processEvents( QEventLoop::ExcludeUserInputEvents );
|
||||
}
|
||||
retval = ( proc->exitCode() == 0 );
|
||||
}
|
||||
else
|
||||
{
|
||||
RiaLogging::error( QString( "Failed to start process %1." ).arg( m_id ) );
|
||||
}
|
||||
|
||||
proc->deleteLater();
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
bool RimProcess::needsCommandInterpreter() const
|
||||
{
|
||||
#ifdef WIN32
|
||||
if ( m_command.value().isNull() ) return false;
|
||||
return m_command.value().endsWith( ".cmd", Qt::CaseInsensitive ) ||
|
||||
m_command.value().endsWith( ".bat", Qt::CaseInsensitive );
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RimProcess::commandLine() const
|
||||
{
|
||||
QString cmdline;
|
||||
|
||||
if ( needsCommandInterpreter() )
|
||||
{
|
||||
cmdline += "cmd.exe /c ";
|
||||
}
|
||||
|
||||
cmdline += handleSpaces( m_command );
|
||||
|
||||
for ( int i = 0; i < m_arguments.size(); i++ )
|
||||
{
|
||||
cmdline += " ";
|
||||
cmdline += handleSpaces( m_arguments[i] );
|
||||
}
|
||||
|
||||
return cmdline;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
QString RimProcess::handleSpaces( QString arg ) const
|
||||
{
|
||||
if ( arg.contains( " " ) && !arg.startsWith( "\"" ) )
|
||||
{
|
||||
return QString( "\"" + arg + "\"" );
|
||||
}
|
||||
return arg;
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
#pragma once
|
||||
|
||||
#include "cafPdmField.h"
|
||||
#include "cafPdmObject.h"
|
||||
|
||||
#include <QProcess>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
|
||||
class RimProcessMonitor;
|
||||
|
||||
class RimProcess : public caf::PdmObject
|
||||
{
|
||||
CAF_PDM_HEADER_INIT;
|
||||
|
||||
public:
|
||||
RimProcess();
|
||||
~RimProcess() override;
|
||||
|
||||
void setDescription( QString desc );
|
||||
void setCommand( QString cmdStr );
|
||||
void addParameter( QString paramStr );
|
||||
void setParameters( QStringList parameterList );
|
||||
|
||||
QString commandLine() const;
|
||||
|
||||
QString command() const;
|
||||
QStringList parameters() const;
|
||||
int ID() const;
|
||||
|
||||
bool execute();
|
||||
|
||||
protected:
|
||||
caf::PdmFieldHandle* userDescriptionField() override;
|
||||
|
||||
private:
|
||||
bool needsCommandInterpreter() const;
|
||||
QString handleSpaces( QString argument ) const;
|
||||
|
||||
caf::PdmField<QString> m_command;
|
||||
QStringList m_arguments;
|
||||
caf::PdmField<QString> m_description;
|
||||
caf::PdmField<int> m_id;
|
||||
|
||||
static int m_nextProcessId;
|
||||
RimProcessMonitor* m_monitor;
|
||||
};
|
||||
@@ -0,0 +1,113 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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 "RimProcessMonitor.h"
|
||||
|
||||
#include "RiaLogging.h"
|
||||
|
||||
#include <QProcess>
|
||||
#include <QtCore/QtCore>
|
||||
|
||||
RimProcessMonitor::RimProcessMonitor( int processId )
|
||||
: QObject( nullptr )
|
||||
, m_processId( processId )
|
||||
{
|
||||
}
|
||||
|
||||
QString RimProcessMonitor::addPrefix( QString message )
|
||||
{
|
||||
return QString( "Process %1: %2" ).arg( m_processId ).arg( message );
|
||||
}
|
||||
|
||||
void RimProcessMonitor::error( QProcess::ProcessError error )
|
||||
{
|
||||
QString errorStr;
|
||||
|
||||
switch ( error )
|
||||
{
|
||||
case QProcess::FailedToStart:
|
||||
errorStr = "Failed to start";
|
||||
break;
|
||||
case QProcess::Crashed:
|
||||
errorStr = "Crashed";
|
||||
break;
|
||||
case QProcess::Timedout:
|
||||
errorStr = "Timed out";
|
||||
break;
|
||||
case QProcess::ReadError:
|
||||
errorStr = "Read error";
|
||||
break;
|
||||
case QProcess::WriteError:
|
||||
errorStr = "Write error";
|
||||
break;
|
||||
case QProcess::UnknownError:
|
||||
default:
|
||||
errorStr = "Unknown";
|
||||
break;
|
||||
}
|
||||
|
||||
RiaLogging::error( addPrefix( errorStr ) );
|
||||
}
|
||||
|
||||
void RimProcessMonitor::finished( int exitCode, QProcess::ExitStatus exitStatus )
|
||||
{
|
||||
QString finishStr;
|
||||
switch ( exitStatus )
|
||||
{
|
||||
case QProcess::NormalExit:
|
||||
finishStr = QString( "Normal exit, code %1" ).arg( exitCode );
|
||||
break;
|
||||
case QProcess::CrashExit:
|
||||
default:
|
||||
finishStr = QString( "Crash exit, code %1" ).arg( exitCode );
|
||||
break;
|
||||
}
|
||||
|
||||
RiaLogging::debug( addPrefix( finishStr ) );
|
||||
}
|
||||
|
||||
void RimProcessMonitor::readyReadStandardError()
|
||||
{
|
||||
QProcess* p = (QProcess*)sender();
|
||||
p->setReadChannel( QProcess::StandardError );
|
||||
while ( p->canReadLine() )
|
||||
{
|
||||
QString line = p->readLine();
|
||||
line = line.trimmed();
|
||||
if ( line.size() == 0 ) continue;
|
||||
RiaLogging::error( addPrefix( line ) );
|
||||
}
|
||||
}
|
||||
|
||||
void RimProcessMonitor::readyReadStandardOutput()
|
||||
{
|
||||
QProcess* p = (QProcess*)sender();
|
||||
p->setReadChannel( QProcess::StandardOutput );
|
||||
while ( p->canReadLine() )
|
||||
{
|
||||
QString line = p->readLine();
|
||||
line = line.trimmed();
|
||||
if ( line.size() == 0 ) continue;
|
||||
RiaLogging::info( addPrefix( line ) );
|
||||
}
|
||||
}
|
||||
|
||||
void RimProcessMonitor::started()
|
||||
{
|
||||
RiaLogging::debug( addPrefix( "Started" ) );
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
/////////////////////////////////////////////////////////////////////////////////
|
||||
#pragma once
|
||||
|
||||
#include <QObject>
|
||||
#include <QProcess>
|
||||
|
||||
class RimProcessMonitor : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit RimProcessMonitor( int processId );
|
||||
|
||||
signals:
|
||||
|
||||
public slots:
|
||||
void error( QProcess::ProcessError error );
|
||||
void finished( int exitCode, QProcess::ExitStatus exitStatus );
|
||||
void readyReadStandardError();
|
||||
void readyReadStandardOutput();
|
||||
void started();
|
||||
|
||||
private:
|
||||
QString addPrefix( QString message );
|
||||
int m_processId;
|
||||
};
|
||||
Reference in New Issue
Block a user