Never block the kded

Never block the kded even if things go wrong.

In order to do this we need to switch to a completely asynchronous
way  of doing things, meaning that instead of returning bool on
start/stop we have to emit signals.
REVIEW: 116575
This commit is contained in:
Àlex Fiestas 2014-03-03 19:58:05 +01:00
parent 10a186795f
commit f4dce72d41
2 changed files with 32 additions and 22 deletions

View file

@ -44,50 +44,38 @@ Kded::~Kded()
kDebug(kdeconnect_kded()) << "kded_kdeconnect stopped";
}
bool Kded::start()
void Kded::start()
{
if (m_daemon) {
return true;
return;
}
const QString daemon = KStandardDirs::locate("exe", "kdeconnectd");
kDebug(kdeconnect_kded()) << "Starting daemon " << daemon;
m_daemon = new KProcess(this);
connect(m_daemon, SIGNAL(started()), SLOT(daemonStarted()));
connect(m_daemon, SIGNAL(error(QProcess::ProcessError)), this, SLOT(onError(QProcess::ProcessError)));
connect(m_daemon, SIGNAL(finished(int,QProcess::ExitStatus)), this, SLOT(onFinished(int,QProcess::ExitStatus)));
connect(m_daemon, SIGNAL(finished(int,QProcess::ExitStatus)), m_daemon, SLOT(deleteLater()));
m_daemon->setProgram(daemon);
m_daemon->setOutputChannelMode(KProcess::SeparateChannels);
m_daemon->start();
if (!m_daemon->waitForStarted(2000)) {//FIXME: KDEDs should be non-blocking, do we really need to wait for it to start?
kError(kdeconnect_kded()) << "Can't start " << daemon;
return false;
}
m_daemon->closeReadChannel(KProcess::StandardOutput);
kDebug(kdeconnect_kded()) << "Daemon successfuly started";
return true;
m_daemon->start();
}
void Kded::stop()
{
if (!m_daemon) {
return true;
return;
}
m_daemon->terminate();
if (m_daemon->waitForFinished(10000)) {
kDebug(kdeconnect_kded()) << "Daemon successfuly stopped";
} else {
m_daemon->kill();
kWarning(kdeconnect_kded()) << "Daemon killed";
}
m_daemon = 0;
m_daemon->setProperty("terminate", true);
QTimer::singleShot(10000, this, SLOT(checkIfDaemonTerminated()));
}
bool Kded::restart()
void Kded::restart()
{
stop();
return start();
@ -98,6 +86,12 @@ void Kded::onError(QProcess::ProcessError errorCode)
kError(kdeconnect_kded()) << "Process error code=" << errorCode;
}
void Kded::daemonStarted()
{
kDebug(kdeconnect_kded()) << "Daemon successfuly started";
Q_EMIT started();
}
void Kded::onFinished(int exitCode, QProcess::ExitStatus status)
{
if (status == QProcess::CrashExit) {
@ -109,6 +103,16 @@ void Kded::onFinished(int exitCode, QProcess::ExitStatus status)
kWarning(kdeconnect_kded()) << "Process finished with code=" << exitCode;
}
Q_EMIT stopped();
m_daemon = 0;
}
void Kded::checkIfDaemonTerminated()
{
if (!m_daemon || !m_daemon->property("terminate").isValid()) {
return;
}
m_daemon->kill();
kWarning(kdeconnect_kded()) << "Daemon killed";
}

View file

@ -36,13 +36,19 @@ public:
public Q_SLOTS:
Q_SCRIPTABLE bool start();
Q_SCRIPTABLE void start();
Q_SCRIPTABLE void stop();
Q_SCRIPTABLE bool restart();
Q_SCRIPTABLE void restart();
Q_SIGNALS:
Q_SCRIPTABLE void started();
Q_SCRIPTABLE void stopped();
private Q_SLOTS:
void onError(QProcess::ProcessError);
void onFinished(int, QProcess::ExitStatus);
void daemonStarted();
void checkIfDaemonTerminated();
private:
KProcess* m_daemon;