@@ -64,45 +64,12 @@ QtService::Service::CommandMode DatasyncService::onStart()
6464 return Synchronous;
6565 }
6666
67- // before anything else: read log level
68- // NOTE move to service lib
69- auto logLevel = _config->value (QStringLiteral (" loglevel" ),
70- #ifndef QT_NO_DEBUG
71- 4
72- #else
73- 3
74- #endif
75- ).toInt ();
76- QString logStr;
77- switch (logLevel) {
78- case 0 : // log nothing
79- logStr.prepend (QStringLiteral (" \n *.critical=false" ));
80- Q_FALLTHROUGH ();
81- case 1 : // log critical
82- logStr.prepend (QStringLiteral (" \n *.warning=false" ));
83- Q_FALLTHROUGH ();
84- case 2 : // log warn
85- logStr.prepend (QStringLiteral (" \n *.info=false" ));
86- Q_FALLTHROUGH ();
87- case 3 : // log info
88- logStr.prepend (QStringLiteral (" *.debug=false" ));
89- QLoggingCategory::setFilterRules (logStr);
90- break ;
91- case 4 : // log any
92- default :
93- break ;
94- }
95-
67+ // before anything else: set the log level
68+ setLogLevel ();
9669 qDebug () << " Using configuration:" << _config->fileName ();
9770
9871 _mainPool = new QThreadPool (this );
99- _mainPool->setMaxThreadCount (_config->value (QStringLiteral (" threads/count" ),
100- QThread::idealThreadCount ()).toInt ());
101- auto timeoutMin = _config->value (QStringLiteral (" threads/expire" ), 10 ).toInt (); // in minutes
102- _mainPool->setExpiryTimeout (static_cast <int >(duration_cast<milliseconds>(minutes (timeoutMin)).count ()));
103- qDebug () << " Running with max" << _mainPool->maxThreadCount ()
104- << " threads and an expiry timeout of" << timeoutMin
105- << " minutes" ;
72+ setupThreadPool ();
10673
10774 _database = new DatabaseController (this );
10875 _connector = new ClientConnector (_database, this );
@@ -124,7 +91,7 @@ QtService::Service::CommandMode DatasyncService::onStart()
12491QtService::Service::CommandMode DatasyncService::onStop (int &exitCode)
12592{
12693 qDebug () << " Stopping server..." ;
127- _connector->disconnectAll ();
94+ emit _connector->disconnectAll ();
12895 _mainPool->clear ();
12996 _mainPool->waitForDone ();
13097 exitCode = EXIT_SUCCESS;
@@ -134,8 +101,33 @@ QtService::Service::CommandMode DatasyncService::onStop(int &exitCode)
134101
135102QtService::Service::CommandMode DatasyncService::onReload ()
136103{
137- // TODO implement
138- Q_UNIMPLEMENTED ();
104+ // cast is ok here as I own the settings - they are only const to prevent accidental writes
105+ const_cast <QSettings*>(_config)->sync ();
106+ if (_config->status () != QSettings::NoError) {
107+ qCritical () << " Failed reload configuration file"
108+ << _config->fileName ()
109+ << " with error:"
110+ << (_config->status () == QSettings::AccessError ? " Access denied" : " Invalid format" );
111+ qCritical () << " Reload failed. Configuration has not been reloaded!" ;
112+ return Synchronous;
113+ }
114+
115+ // before anything else: set the log level
116+ setLogLevel ();
117+ // adjust threadpool parameters
118+ setupThreadPool ();
119+
120+ // update the database
121+ _database->reload ();
122+ // recreate and connect the server
123+ _connector->recreateServer ();
124+ if (!_connector->setupWss () ||
125+ !_connector->listen ()) {
126+ qApp->exit (EXIT_FAILURE);
127+ return Synchronous;
128+ }
129+
130+ qDebug () << QCoreApplication::applicationName () << " completed reloading" ;
139131 return Synchronous;
140132}
141133
@@ -219,6 +211,49 @@ void DatasyncService::command(int cmd)
219211 }
220212}
221213
214+ void DatasyncService::setLogLevel ()
215+ {
216+ auto logLevel = _config->value (QStringLiteral (" loglevel" ),
217+ #ifndef QT_NO_DEBUG
218+ 4
219+ #else
220+ 3
221+ #endif
222+ ).toInt ();
223+ QString logStr;
224+ switch (logLevel) {
225+ case 0 : // log nothing
226+ logStr.prepend (QStringLiteral (" \n *.critical=false" ));
227+ Q_FALLTHROUGH ();
228+ case 1 : // log critical
229+ logStr.prepend (QStringLiteral (" \n *.warning=false" ));
230+ Q_FALLTHROUGH ();
231+ case 2 : // log warn
232+ logStr.prepend (QStringLiteral (" \n *.info=false" ));
233+ Q_FALLTHROUGH ();
234+ case 3 : // log info
235+ logStr.prepend (QStringLiteral (" *.debug=false" ));
236+ QLoggingCategory::setFilterRules (logStr);
237+ break ;
238+ case 4 : // log any
239+ QLoggingCategory::setFilterRules ({});
240+ break ;
241+ default :
242+ break ;
243+ }
244+ }
245+
246+ void DatasyncService::setupThreadPool ()
247+ {
248+ _mainPool->setMaxThreadCount (_config->value (QStringLiteral (" threads/count" ),
249+ QThread::idealThreadCount ()).toInt ());
250+ auto timeoutMin = _config->value (QStringLiteral (" threads/expire" ), 10 ).toInt (); // in minutes
251+ _mainPool->setExpiryTimeout (static_cast <int >(duration_cast<milliseconds>(minutes (timeoutMin)).count ()));
252+ qDebug () << " Running with max" << _mainPool->maxThreadCount ()
253+ << " threads and an expiry timeout of" << timeoutMin
254+ << " minutes" ;
255+ }
256+
222257int main (int argc, char *argv[])
223258{
224259 // check if version
0 commit comments