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 | 19 | LIBS += -L/usr/lib -lcryptopp |
20 | 20 | } |
21 | 21 | TEMPLATE = app |
22 | -#TEMPLATE = lib | |
23 | 22 | |
24 | 23 | SOURCES += main.cpp \ |
25 | 24 | cacicd.cpp \ |
26 | 25 | cacictimer.cpp \ |
26 | + cacicthread.cpp \ | |
27 | 27 | ../../src/cacic_comm.cpp \ |
28 | 28 | ../../src/ccacic.cpp \ |
29 | 29 | ../../src/wmi.cpp \ |
30 | 30 | ../../src/cacic_computer.cpp \ |
31 | 31 | ../../src/operatingsystem.cpp \ |
32 | - ../../src/QLogger.cpp | |
32 | + ../../src/QLogger.cpp \ | |
33 | + | |
33 | 34 | |
34 | 35 | HEADERS += cacicd.h \ |
35 | 36 | cacictimer.h \ |
37 | + cacicthread.h \ | |
36 | 38 | ../../src/ccacic.h \ |
37 | 39 | ../../src/wmi.h \ |
38 | 40 | ../../src/cacic_computer.h \ |
39 | 41 | ../../src/operatingsystem.h \ |
40 | 42 | ../../src/cacic_comm.h \ |
41 | - ../../src/QLogger.h | |
43 | + ../../src/QLogger.h \ | |
44 | + | |
42 | 45 | |
43 | 46 | include(../../src/qtservice/src/qtservice.pri) |
44 | 47 | ... | ... |
... | ... | @@ -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 @@ |
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 | 38 | }catch (...){ |
39 | 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 | 44 | if(getTest()){ |
44 | 45 | QLogger::QLog_Info("Cacic Daemon (Timer)", QString("getTeste() success.")); |
45 | 46 | if(getConfig()){ |
46 | 47 | QLogger::QLog_Info("Cacic Daemon (Timer)", QString("getConfig() success.")); |
47 | 48 | //QStringList nomesModulos = verificarModulos(); |
48 | 49 | //if ( !nomesModulos.empty() ) { |
49 | - // foreach( QString nome, nomesModulos ) { | |
50 | + // foreach( QString nome, nomesModulos ) { | |
50 | 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 | 63 | }else{ |
62 | - qDebug() << "getConfig() failed. - " + QDateTime::currentDateTime().toLocalTime().toString(); | |
63 | 64 | QLogger::QLog_Error("Cacic Daemon (Timer)", "Falha na obtenção do arquivo de configuração."); |
64 | 65 | } |
65 | 66 | }else{ |
66 | - qDebug() << "getTest() failed. - " + QDateTime::currentDateTime().toLocalTime().toString(); | |
67 | 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 | 89 | QString CacicTimer::getApplicationDirPath() { |
... | ... | @@ -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 | 186 | QString CacicTimer::getDirProgram() const |
176 | 187 | { |
177 | 188 | return dirProgram; |
... | ... | @@ -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 | 197 | void CacicTimer::setApplicationDirPath(const QString &value) |
204 | 198 | { |
205 | 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 | 203 | bool CacicTimer::Md5IsEqual(QVariant document01,QVariant document02){ |
215 | 204 | QString getconfigMD5 = QString(QCryptographicHash::hash( |
216 | 205 | (document01.toByteArray()),QCryptographicHash::Md5).toHex()); |
... | ... | @@ -230,6 +219,7 @@ void CacicTimer::iniciarInstancias(){ |
230 | 219 | ccacic = new CCacic(); |
231 | 220 | timer = new QTimer(this); |
232 | 221 | cMutex = new QMutex(QMutex::Recursive); |
222 | + cacicthread = new CacicThread(this->applicationDirPath); | |
233 | 223 | OCacicComm = new CacicComm(); |
234 | 224 | OCacicComm->setUrlSsl("https://teste.cacic.cc"); |
235 | 225 | OCacicComm->setUrlGerente("teste.cacic.cc"); | ... | ... |
cacic-daemon/cacicD/cacictimer.h
... | ... | @@ -12,6 +12,7 @@ |
12 | 12 | #include "cacic_comm.h" |
13 | 13 | #include "cacic_computer.h" |
14 | 14 | #include "QLogger.h" |
15 | +#include "cacicthread.h" | |
15 | 16 | |
16 | 17 | class CacicTimer : public QObject |
17 | 18 | { |
... | ... | @@ -34,15 +35,14 @@ public: |
34 | 35 | void setPeriodicidadeExecucao(int value); |
35 | 36 | |
36 | 37 | private: |
37 | - void registraFimColeta(QString msg); | |
38 | - void registraInicioColeta(); | |
38 | + void verificarEIniciarQMutex(); | |
39 | 39 | QStringList verificarModulos(); |
40 | 40 | void reiniciarTimer(); |
41 | 41 | QLogger::QLoggerManager *logManager; |
42 | + CacicThread *cacicthread; | |
42 | 43 | QString dirProgram; |
43 | 44 | QString applicationDirPath; |
44 | 45 | QString getDirProgram() const; |
45 | - void iniciarModulo(QString modulo); | |
46 | 46 | void iniciarInstancias(); |
47 | 47 | void verificarPeriodicidadeJson(); |
48 | 48 | void lerArquivoConfig( const QJsonObject &jsonConfig); | ... | ... |