Commit 7ae180db356ee6ee5211a90351b17d85cf7f4a51
1 parent
4f983034
Exists in
master
Thread para executar o gercols e semáforo com timeout.
Showing
6 changed files
with
124 additions
and
50 deletions
Show diff stats
cacic-daemon/cacicD/cacicD.pro
| @@ -19,26 +19,29 @@ win32 { | @@ -19,26 +19,29 @@ win32 { | ||
| 19 | LIBS += -L/usr/lib -lcryptopp | 19 | LIBS += -L/usr/lib -lcryptopp |
| 20 | } | 20 | } |
| 21 | TEMPLATE = app | 21 | TEMPLATE = app |
| 22 | -#TEMPLATE = lib | ||
| 23 | 22 | ||
| 24 | SOURCES += main.cpp \ | 23 | SOURCES += main.cpp \ |
| 25 | cacicd.cpp \ | 24 | cacicd.cpp \ |
| 26 | cacictimer.cpp \ | 25 | cacictimer.cpp \ |
| 26 | + cacicthread.cpp \ | ||
| 27 | ../../src/cacic_comm.cpp \ | 27 | ../../src/cacic_comm.cpp \ |
| 28 | ../../src/ccacic.cpp \ | 28 | ../../src/ccacic.cpp \ |
| 29 | ../../src/wmi.cpp \ | 29 | ../../src/wmi.cpp \ |
| 30 | ../../src/cacic_computer.cpp \ | 30 | ../../src/cacic_computer.cpp \ |
| 31 | ../../src/operatingsystem.cpp \ | 31 | ../../src/operatingsystem.cpp \ |
| 32 | - ../../src/QLogger.cpp | 32 | + ../../src/QLogger.cpp \ |
| 33 | + | ||
| 33 | 34 | ||
| 34 | HEADERS += cacicd.h \ | 35 | HEADERS += cacicd.h \ |
| 35 | cacictimer.h \ | 36 | cacictimer.h \ |
| 37 | + cacicthread.h \ | ||
| 36 | ../../src/ccacic.h \ | 38 | ../../src/ccacic.h \ |
| 37 | ../../src/wmi.h \ | 39 | ../../src/wmi.h \ |
| 38 | ../../src/cacic_computer.h \ | 40 | ../../src/cacic_computer.h \ |
| 39 | ../../src/operatingsystem.h \ | 41 | ../../src/operatingsystem.h \ |
| 40 | ../../src/cacic_comm.h \ | 42 | ../../src/cacic_comm.h \ |
| 41 | - ../../src/QLogger.h | 43 | + ../../src/QLogger.h \ |
| 44 | + | ||
| 42 | 45 | ||
| 43 | include(../../src/qtservice/src/qtservice.pri) | 46 | include(../../src/qtservice/src/qtservice.pri) |
| 44 | 47 |
| @@ -0,0 +1,56 @@ | @@ -0,0 +1,56 @@ | ||
| 1 | +#include "cacicthread.h" | ||
| 2 | + | ||
| 3 | +CacicThread::CacicThread(QString applicationDirPath) | ||
| 4 | +{ | ||
| 5 | + this->applicationDirPath = applicationDirPath; | ||
| 6 | + iniciarInstancias(); | ||
| 7 | +} | ||
| 8 | + | ||
| 9 | +void CacicThread::run(){ | ||
| 10 | + iniciarModulo(); | ||
| 11 | +} | ||
| 12 | + | ||
| 13 | +void CacicThread::setModuloDirPath(const QString &value) | ||
| 14 | +{ | ||
| 15 | + moduloDirPath = value; | ||
| 16 | +} | ||
| 17 | + | ||
| 18 | +void CacicThread::iniciarModulo() | ||
| 19 | +{ | ||
| 20 | + registraInicioColeta(); | ||
| 21 | + QDir::setCurrent(this->applicationDirPath); | ||
| 22 | + QProcess proc; | ||
| 23 | + proc.setWorkingDirectory(this->applicationDirPath); | ||
| 24 | + proc.execute(this->moduloDirPath); | ||
| 25 | + if((proc.atEnd()) && (proc.exitStatus() == QProcess::NormalExit)){ | ||
| 26 | + registraFimColeta("SUCESSO"); | ||
| 27 | + }else{ | ||
| 28 | + if((!proc.atEnd()) || (proc.exitStatus() == QProcess::CrashExit)){ | ||
| 29 | + registraFimColeta("ERRO"); | ||
| 30 | + proc.kill(); | ||
| 31 | + } | ||
| 32 | + } | ||
| 33 | + cMutex->unlock(); | ||
| 34 | + QLogger::QLog_Info("Cacic Daemon (Thread)", QString("Semáforo aberto com sucesso.")); | ||
| 35 | +} | ||
| 36 | + | ||
| 37 | +void CacicThread::setCMutex(QMutex *value) | ||
| 38 | +{ | ||
| 39 | + cMutex = value; | ||
| 40 | +} | ||
| 41 | + | ||
| 42 | +void CacicThread::registraInicioColeta() | ||
| 43 | +{ | ||
| 44 | + QLogger::QLog_Info("Cacic Daemon (Thread)","Coleta iniciada em: " + QDateTime::currentDateTime().toLocalTime().toString()); | ||
| 45 | +} | ||
| 46 | + | ||
| 47 | +void CacicThread::registraFimColeta(QString msg) | ||
| 48 | +{ | ||
| 49 | + QLogger::QLog_Info("Cacic Daemon (Thread)","Coleta finalizada com " + msg + " em: " + QDateTime::currentDateTime().toLocalTime().toString()); | ||
| 50 | +} | ||
| 51 | + | ||
| 52 | +void CacicThread::iniciarInstancias(){ | ||
| 53 | + logManager = QLogger::QLoggerManager::getInstance(); | ||
| 54 | + logManager->addDestination(this->applicationDirPath + "/cacicLog.txt","Cacic Daemon (Thread)",QLogger::InfoLevel); | ||
| 55 | + logManager->addDestination(this->applicationDirPath + "/cacicLog.txt","Cacic Daemon (Thread)",QLogger::ErrorLevel); | ||
| 56 | +} |
| @@ -0,0 +1,26 @@ | @@ -0,0 +1,26 @@ | ||
| 1 | +#ifndef CACICTHREAD_H | ||
| 2 | +#define CACICTHREAD_H | ||
| 3 | +#include <QtCore> | ||
| 4 | +#include <QMutex> | ||
| 5 | +#include "QLogger.h" | ||
| 6 | + | ||
| 7 | +class CacicThread : public QThread | ||
| 8 | +{ | ||
| 9 | +public: | ||
| 10 | + CacicThread(QString applicationDirPath); | ||
| 11 | + void run(); | ||
| 12 | + void setModuloDirPath(const QString &value); | ||
| 13 | + void setCMutex(QMutex *value); | ||
| 14 | + | ||
| 15 | +private: | ||
| 16 | + QString moduloDirPath; | ||
| 17 | + QString applicationDirPath; | ||
| 18 | + QLogger::QLoggerManager *logManager; | ||
| 19 | + void iniciarInstancias(); | ||
| 20 | + void registraInicioColeta(); | ||
| 21 | + void registraFimColeta(QString msg); | ||
| 22 | + void iniciarModulo(); | ||
| 23 | + QMutex *cMutex; | ||
| 24 | +}; | ||
| 25 | + | ||
| 26 | +#endif // CACICTHREAD_H |
cacic-daemon/cacicD/cacictimer.cpp
| @@ -38,36 +38,52 @@ void CacicTimer::mslot(){ | @@ -38,36 +38,52 @@ void CacicTimer::mslot(){ | ||
| 38 | }catch (...){ | 38 | }catch (...){ |
| 39 | QLogger::QLog_Info("Cacic Daemon (Timer)", QString("Não foi possivel verificar a periodicidade no getConfig.json")); | 39 | QLogger::QLog_Info("Cacic Daemon (Timer)", QString("Não foi possivel verificar a periodicidade no getConfig.json")); |
| 40 | } | 40 | } |
| 41 | - cMutex->lock(); | ||
| 42 | - QLogger::QLog_Info("Cacic Daemon (Timer)", QString("Semáforo fechado.")); | 41 | + |
| 42 | + verificarEIniciarQMutex(); | ||
| 43 | + | ||
| 43 | if(getTest()){ | 44 | if(getTest()){ |
| 44 | QLogger::QLog_Info("Cacic Daemon (Timer)", QString("getTeste() success.")); | 45 | QLogger::QLog_Info("Cacic Daemon (Timer)", QString("getTeste() success.")); |
| 45 | if(getConfig()){ | 46 | if(getConfig()){ |
| 46 | QLogger::QLog_Info("Cacic Daemon (Timer)", QString("getConfig() success.")); | 47 | QLogger::QLog_Info("Cacic Daemon (Timer)", QString("getConfig() success.")); |
| 47 | //QStringList nomesModulos = verificarModulos(); | 48 | //QStringList nomesModulos = verificarModulos(); |
| 48 | //if ( !nomesModulos.empty() ) { | 49 | //if ( !nomesModulos.empty() ) { |
| 49 | - // foreach( QString nome, nomesModulos ) { | 50 | + // foreach( QString nome, nomesModulos ) { |
| 50 | definirDirModulo(getApplicationDirPath(), "gercols"); | 51 | definirDirModulo(getApplicationDirPath(), "gercols"); |
| 51 | - iniciarModulo(getDirProgram()); | ||
| 52 | - | ||
| 53 | - // if(nome == "gercols"){ | ||
| 54 | - // // Envio do json gerado na coleta | ||
| 55 | - // bool ok; | ||
| 56 | - // QJsonObject jsonColeta = ccacic->getJsonFromFile("coleta.json"); | ||
| 57 | - // OCacicComm->comm("/ws/neo/coleta", &ok, jsonColeta ); | ||
| 58 | - // } | ||
| 59 | - // } | 52 | + cacicthread->setCMutex(cMutex); |
| 53 | + cacicthread->setModuloDirPath(getDirProgram()); | ||
| 54 | + cacicthread->start(QThread::NormalPriority); | ||
| 55 | + //if(nome == "gercols"){ | ||
| 56 | + // Envio do json gerado na coleta | ||
| 57 | + // bool ok; | ||
| 58 | + // QJsonObject jsonColeta = ccacic->getJsonFromFile("coleta.json"); | ||
| 59 | + // OCacicComm->comm("/ws/neo/coleta", &ok, jsonColeta , false); | ||
| 60 | //} | 60 | //} |
| 61 | + // } | ||
| 62 | + // } | ||
| 61 | }else{ | 63 | }else{ |
| 62 | - qDebug() << "getConfig() failed. - " + QDateTime::currentDateTime().toLocalTime().toString(); | ||
| 63 | QLogger::QLog_Error("Cacic Daemon (Timer)", "Falha na obtenção do arquivo de configuração."); | 64 | QLogger::QLog_Error("Cacic Daemon (Timer)", "Falha na obtenção do arquivo de configuração."); |
| 64 | } | 65 | } |
| 65 | }else{ | 66 | }else{ |
| 66 | - qDebug() << "getTest() failed. - " + QDateTime::currentDateTime().toLocalTime().toString(); | ||
| 67 | QLogger::QLog_Error("Cacic Daemon (Timer)", "Falha na execução do getTest()."); | 67 | QLogger::QLog_Error("Cacic Daemon (Timer)", "Falha na execução do getTest()."); |
| 68 | } | 68 | } |
| 69 | - cMutex->unlock(); | ||
| 70 | - QLogger::QLog_Info("Cacic Daemon (Timer)", QString("Semáforo aberto.")); | 69 | +} |
| 70 | + | ||
| 71 | +void CacicTimer::verificarEIniciarQMutex(){ | ||
| 72 | + if(!cacicthread->isRunning()){ | ||
| 73 | + cMutex->lock(); | ||
| 74 | + QLogger::QLog_Info("Cacic Daemon (Timer)", "Semáforo fechado com sucesso."); | ||
| 75 | + }else{ | ||
| 76 | + QLogger::QLog_Info("Cacic Daemon (Timer)", "Possivelmente o gercols travou e será finalizado."); | ||
| 77 | + try{ | ||
| 78 | + cacicthread->terminate(); | ||
| 79 | + QLogger::QLog_Info("Cacic Daemon (Timer)", "Gercols finalizado com sucesso."); | ||
| 80 | + }catch (...){ | ||
| 81 | + QLogger::QLog_Error("Cacic Daemon (Timer)", "Falha ao finalizar gercols."); | ||
| 82 | + return; | ||
| 83 | + } | ||
| 84 | + cMutex->lock(); | ||
| 85 | + QLogger::QLog_Info("Cacic Daemon (Timer)", "Semáforo fechado com sucesso."); | ||
| 86 | + } | ||
| 71 | } | 87 | } |
| 72 | 88 | ||
| 73 | QString CacicTimer::getApplicationDirPath() { | 89 | QString CacicTimer::getApplicationDirPath() { |
| @@ -167,11 +183,6 @@ void CacicTimer::lerArquivoConfig ( const QJsonObject& jsonConfig ) | @@ -167,11 +183,6 @@ void CacicTimer::lerArquivoConfig ( const QJsonObject& jsonConfig ) | ||
| 167 | } | 183 | } |
| 168 | } | 184 | } |
| 169 | 185 | ||
| 170 | -void CacicTimer::registraInicioColeta() | ||
| 171 | -{ | ||
| 172 | - QLogger::QLog_Info("Cacic Daemon (Timer)","Coleta iniciada em: " + QDateTime::currentDateTime().toLocalTime().toString()); | ||
| 173 | -} | ||
| 174 | - | ||
| 175 | QString CacicTimer::getDirProgram() const | 186 | QString CacicTimer::getDirProgram() const |
| 176 | { | 187 | { |
| 177 | return dirProgram; | 188 | return dirProgram; |
| @@ -183,34 +194,12 @@ void CacicTimer::setDirProgram(const QString &value) | @@ -183,34 +194,12 @@ void CacicTimer::setDirProgram(const QString &value) | ||
| 183 | } | 194 | } |
| 184 | 195 | ||
| 185 | 196 | ||
| 186 | -void CacicTimer::iniciarModulo(QString modulo) | ||
| 187 | -{ | ||
| 188 | - registraInicioColeta(); | ||
| 189 | - QDir::setCurrent(this->applicationDirPath); | ||
| 190 | - QProcess proc; | ||
| 191 | - proc.setWorkingDirectory(this->applicationDirPath); | ||
| 192 | - proc.execute(modulo); | ||
| 193 | - if((proc.atEnd()) && (proc.exitStatus() == QProcess::NormalExit)){ | ||
| 194 | - registraFimColeta("SUCESSO"); | ||
| 195 | - }else{ | ||
| 196 | - if((!proc.atEnd()) || (proc.exitStatus() == QProcess::CrashExit)){ | ||
| 197 | - registraFimColeta("ERRO"); | ||
| 198 | - proc.kill(); | ||
| 199 | - } | ||
| 200 | - } | ||
| 201 | -} | ||
| 202 | - | ||
| 203 | void CacicTimer::setApplicationDirPath(const QString &value) | 197 | void CacicTimer::setApplicationDirPath(const QString &value) |
| 204 | { | 198 | { |
| 205 | this->applicationDirPath = value; | 199 | this->applicationDirPath = value; |
| 206 | } | 200 | } |
| 207 | 201 | ||
| 208 | 202 | ||
| 209 | -void CacicTimer::registraFimColeta(QString msg) | ||
| 210 | -{ | ||
| 211 | - QLogger::QLog_Info("Cacic Daemon (Timer)","Coleta finalizada com " + msg + " em: " + QDateTime::currentDateTime().toLocalTime().toString()); | ||
| 212 | -} | ||
| 213 | - | ||
| 214 | bool CacicTimer::Md5IsEqual(QVariant document01,QVariant document02){ | 203 | bool CacicTimer::Md5IsEqual(QVariant document01,QVariant document02){ |
| 215 | QString getconfigMD5 = QString(QCryptographicHash::hash( | 204 | QString getconfigMD5 = QString(QCryptographicHash::hash( |
| 216 | (document01.toByteArray()),QCryptographicHash::Md5).toHex()); | 205 | (document01.toByteArray()),QCryptographicHash::Md5).toHex()); |
| @@ -230,6 +219,7 @@ void CacicTimer::iniciarInstancias(){ | @@ -230,6 +219,7 @@ void CacicTimer::iniciarInstancias(){ | ||
| 230 | ccacic = new CCacic(); | 219 | ccacic = new CCacic(); |
| 231 | timer = new QTimer(this); | 220 | timer = new QTimer(this); |
| 232 | cMutex = new QMutex(QMutex::Recursive); | 221 | cMutex = new QMutex(QMutex::Recursive); |
| 222 | + cacicthread = new CacicThread(this->applicationDirPath); | ||
| 233 | OCacicComm = new CacicComm(); | 223 | OCacicComm = new CacicComm(); |
| 234 | OCacicComm->setUrlSsl("https://teste.cacic.cc"); | 224 | OCacicComm->setUrlSsl("https://teste.cacic.cc"); |
| 235 | OCacicComm->setUrlGerente("teste.cacic.cc"); | 225 | OCacicComm->setUrlGerente("teste.cacic.cc"); |
cacic-daemon/cacicD/cacictimer.h
| @@ -12,6 +12,7 @@ | @@ -12,6 +12,7 @@ | ||
| 12 | #include "cacic_comm.h" | 12 | #include "cacic_comm.h" |
| 13 | #include "cacic_computer.h" | 13 | #include "cacic_computer.h" |
| 14 | #include "QLogger.h" | 14 | #include "QLogger.h" |
| 15 | +#include "cacicthread.h" | ||
| 15 | 16 | ||
| 16 | class CacicTimer : public QObject | 17 | class CacicTimer : public QObject |
| 17 | { | 18 | { |
| @@ -34,15 +35,14 @@ public: | @@ -34,15 +35,14 @@ public: | ||
| 34 | void setPeriodicidadeExecucao(int value); | 35 | void setPeriodicidadeExecucao(int value); |
| 35 | 36 | ||
| 36 | private: | 37 | private: |
| 37 | - void registraFimColeta(QString msg); | ||
| 38 | - void registraInicioColeta(); | 38 | + void verificarEIniciarQMutex(); |
| 39 | QStringList verificarModulos(); | 39 | QStringList verificarModulos(); |
| 40 | void reiniciarTimer(); | 40 | void reiniciarTimer(); |
| 41 | QLogger::QLoggerManager *logManager; | 41 | QLogger::QLoggerManager *logManager; |
| 42 | + CacicThread *cacicthread; | ||
| 42 | QString dirProgram; | 43 | QString dirProgram; |
| 43 | QString applicationDirPath; | 44 | QString applicationDirPath; |
| 44 | QString getDirProgram() const; | 45 | QString getDirProgram() const; |
| 45 | - void iniciarModulo(QString modulo); | ||
| 46 | void iniciarInstancias(); | 46 | void iniciarInstancias(); |
| 47 | void verificarPeriodicidadeJson(); | 47 | void verificarPeriodicidadeJson(); |
| 48 | void lerArquivoConfig( const QJsonObject &jsonConfig); | 48 | void lerArquivoConfig( const QJsonObject &jsonConfig); |