-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathSqlQueryWatcher.cpp
106 lines (89 loc) · 3.13 KB
/
SqlQueryWatcher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Copyright (C) 2011-2013 Klarälvdalens Datakonsult AB,
a KDAB Group company, [email protected],
author Andras Mantia <[email protected]>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library 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 Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "SqlQueryWatcher.h"
#include <QDebug>
#include <QTimer>
#include <QEventLoop>
#include <QProcess>
#include <QMutex>
#ifdef Q_OS_WIN
#include <process.h>
#else
#include <unistd.h>
#endif
#define SQL_NETWORK_TIMEOUT 30 //define how many seconds do we wait for a query finishes until the network error dialog is shown
QMutex mutex;
static bool s_dialogVisible = false;
SqlQueryWatcherHelper::SqlQueryWatcherHelper(QObject* runner):QObject(0)
{
// qDebug() << Q_FUNC_INFO << this;
m_process = 0;
m_timer = new QTimer();
m_timer->setSingleShot(true);
connect( m_timer, SIGNAL(timeout()), SLOT(timerExpired()) );
m_timer->start( SQL_NETWORK_TIMEOUT*1000 );
}
SqlQueryWatcherHelper::~SqlQueryWatcherHelper()
{
// qDebug() << Q_FUNC_INFO << this;
delete m_timer;
if ( m_process ) {
m_process->terminate();
m_process->kill();
m_process->waitForFinished();
}
emit endWatcher();
}
void SqlQueryWatcherHelper::dialogHidden()
{
QMutexLocker locker(&mutex);
s_dialogVisible = false;
}
void SqlQueryWatcherHelper::timerExpired()
{
QMutexLocker locker(&mutex);
if ( s_dialogVisible )
return;
s_dialogVisible = true;
qDebug() << Q_FUNC_INFO << this;
m_process = new QProcess(this);
connect( m_process, SIGNAL(finished(int)), this, SLOT(dialogHidden()) );
QStringList args;
args << QLatin1String("-pid");
#ifdef Q_OS_WIN
args << QString::number( _getpid() );
#else
args << QString::number( ::getpid() );
#endif
m_process->start( QLatin1String("queryWatcherHelper"), args );
m_process->waitForStarted();
if ( m_process->state() != QProcess::Running ) { //try to start from the build dir
m_process->start( QLatin1String("src/helpers/queryWatcherHelper"), args );
m_process->waitForStarted();
if ( m_process->state() != QProcess::Running ) { //try to start from the build dir
qWarning() << "Timer expired, query was too slow! Possible network error detected.";
}
}
}
void SqlQueryWatcherHelper::quit()
{
// qDebug() << Q_FUNC_INFO;
thread()->quit();
// metaObject()->invokeMethod(thread(), "quit", Qt::DirectConnection);
}
#include "moc_SqlQueryWatcher.cpp"