Allow shutdown via CLI

This commit is contained in:
Martchus 2016-10-02 22:16:43 +02:00
parent 1f21c2dc52
commit f82ed3ba47
6 changed files with 42 additions and 3 deletions

View File

@ -34,6 +34,7 @@ Application::Application() :
// setup argument callbacks // setup argument callbacks
m_args.status.setCallback(bind(&Application::printStatus, this, _1)); m_args.status.setCallback(bind(&Application::printStatus, this, _1));
m_args.log.setCallback(bind(&Application::requestLog, this, _1)); m_args.log.setCallback(bind(&Application::requestLog, this, _1));
m_args.stop.setCallback(bind(&Application::requestShutdown, this, _1));
m_args.restart.setCallback(bind(&Application::requestRestart, this, _1)); m_args.restart.setCallback(bind(&Application::requestRestart, this, _1));
m_args.rescan.setCallback(bind(&Application::requestRescan, this, _1)); m_args.rescan.setCallback(bind(&Application::requestRescan, this, _1));
m_args.rescanAll.setCallback(bind(&Application::requestRescanAll, this, _1)); m_args.rescanAll.setCallback(bind(&Application::requestRescanAll, this, _1));
@ -168,6 +169,14 @@ void Application::requestLog(const ArgumentOccurrence &)
cerr.flush(); cerr.flush();
} }
void Application::requestShutdown(const ArgumentOccurrence &)
{
connect(&m_connection, &SyncthingConnection::shutdownTriggered, &QCoreApplication::quit);
m_connection.shutdown();
cerr << "Request shutdown " << m_settings.syncthingUrl.toLocal8Bit().data() << " ...";
cerr.flush();
}
void Application::requestRestart(const ArgumentOccurrence &) void Application::requestRestart(const ArgumentOccurrence &)
{ {
connect(&m_connection, &SyncthingConnection::restartTriggered, &QCoreApplication::quit); connect(&m_connection, &SyncthingConnection::restartTriggered, &QCoreApplication::quit);

View File

@ -27,6 +27,7 @@ private slots:
private: private:
void requestLog(const ArgumentOccurrence &); void requestLog(const ArgumentOccurrence &);
void requestShutdown(const ArgumentOccurrence &);
void requestRestart(const ArgumentOccurrence &); void requestRestart(const ArgumentOccurrence &);
void requestRescan(const ArgumentOccurrence &occurrence); void requestRescan(const ArgumentOccurrence &occurrence);
void requestRescanAll(const ArgumentOccurrence &); void requestRescanAll(const ArgumentOccurrence &);

View File

@ -6,6 +6,7 @@ Args::Args() :
help(parser), help(parser),
status("status", 's', "shows the status"), status("status", 's', "shows the status"),
log("log", 'l', "shows the Syncthing log"), log("log", 'l', "shows the Syncthing log"),
stop("stop", '\0', "stops Syncthing"),
restart("restart", '\0', "restarts Syncthing"), restart("restart", '\0', "restarts Syncthing"),
rescan("rescan", 'r', "rescans the specified directories"), rescan("rescan", 'r', "rescans the specified directories"),
rescanAll("rescan-all", '\0', "rescans all directories"), rescanAll("rescan-all", '\0', "rescans all directories"),
@ -31,8 +32,7 @@ Args::Args() :
resume.setValueNames({"dev ID"}); resume.setValueNames({"dev ID"});
resume.setRequiredValueCount(-1); resume.setRequiredValueCount(-1);
parser.setMainArguments({&status, &log, &restart, &rescan, &rescanAll, &pause, &pauseAll, parser.setMainArguments({&status, &log, &stop, &restart, &rescan, &rescanAll, &pause, &pauseAll, &resume, &resumeAll,
&resume, &resumeAll,
&configFile, &apiKey, &url, &credentials, &certificate, &help}); &configFile, &apiKey, &url, &credentials, &certificate, &help});
// allow setting default values via environment // allow setting default values via environment

View File

@ -12,7 +12,7 @@ struct Args
Args(); Args();
ArgumentParser parser; ArgumentParser parser;
HelpArgument help; HelpArgument help;
OperationArgument status, log, restart, rescan, rescanAll, pause, pauseAll, resume, resumeAll; OperationArgument status, log, stop, restart, rescan, rescanAll, pause, pauseAll, resume, resumeAll;
ConfigValueArgument dir, dev; ConfigValueArgument dir, dev;
ConfigValueArgument configFile, apiKey, url, credentials, certificate; ConfigValueArgument configFile, apiKey, url, credentials, certificate;
}; };

View File

@ -359,6 +359,16 @@ void SyncthingConnection::restart()
QObject::connect(postData(QStringLiteral("system/restart"), QUrlQuery()), &QNetworkReply::finished, this, &SyncthingConnection::readRestart); QObject::connect(postData(QStringLiteral("system/restart"), QUrlQuery()), &QNetworkReply::finished, this, &SyncthingConnection::readRestart);
} }
/*!
* \brief Requests Syncthing to exit and not restart.
*
* The signal error() is emitted when the request was not successful.
*/
void SyncthingConnection::shutdown()
{
QObject::connect(postData(QStringLiteral("system/shutdown"), QUrlQuery()), &QNetworkReply::finished, this, &SyncthingConnection::readShutdown);
}
/*! /*!
* \brief Considers all notifications as read; hence might trigger a status update. * \brief Considers all notifications as read; hence might trigger a status update.
*/ */
@ -1396,6 +1406,22 @@ void SyncthingConnection::readRestart()
} }
} }
/*!
* \brief Reads results of shutdown().
*/
void SyncthingConnection::readShutdown()
{
auto *reply = static_cast<QNetworkReply *>(sender());
reply->deleteLater();
switch(reply->error()) {
case QNetworkReply::NoError:
emit shutdownTriggered();
break;
default:
emit error(tr("Unable to request shutdown: ") + reply->errorString());
}
}
/*! /*!
* \brief Sets the connection status. Ensures statusChanged() is emitted. * \brief Sets the connection status. Ensures statusChanged() is emitted.
* \param status Specifies the status; should be either SyncthingStatus::Disconnected or SyncthingStatus::Default. There is no use * \param status Specifies the status; should be either SyncthingStatus::Disconnected or SyncthingStatus::Default. There is no use

View File

@ -212,6 +212,7 @@ public Q_SLOTS:
void rescan(const QString &dirId); void rescan(const QString &dirId);
void rescanAllDirs(); void rescanAllDirs();
void restart(); void restart();
void shutdown();
void considerAllNotificationsRead(); void considerAllNotificationsRead();
Q_SIGNALS: Q_SIGNALS:
@ -232,6 +233,7 @@ Q_SIGNALS:
void pauseTriggered(const QString &devId); void pauseTriggered(const QString &devId);
void resumeTriggered(const QString &devId); void resumeTriggered(const QString &devId);
void restartTriggered(); void restartTriggered();
void shutdownTriggered();
private Q_SLOTS: private Q_SLOTS:
void requestConfig(); void requestConfig();
@ -262,6 +264,7 @@ private Q_SLOTS:
void readRescan(); void readRescan();
void readPauseResume(); void readPauseResume();
void readRestart(); void readRestart();
void readShutdown();
void continueConnecting(); void continueConnecting();
void continueReconnecting(); void continueReconnecting();