add ability to reload without reinitialization

This commit is contained in:
Evgenii Alekseev 2017-10-13 23:13:00 +03:00
parent 0d5e9a328e
commit de0653f038
12 changed files with 72 additions and 53 deletions

View File

@ -53,9 +53,10 @@ class QueuedCore : public QObject
public: public:
/** /**
* @brief QueuedCore class constructor * @brief QueuedCore class constructor
* @param parent pointer to parent item * @param _parent
* pointer to parent item
*/ */
explicit QueuedCore(QObject *parent); explicit QueuedCore(QObject *_parent);
/** /**
* @brief QueuedCore class destructor * @brief QueuedCore class destructor
*/ */
@ -382,6 +383,21 @@ private:
* @throw QueuedDBusException * @throw QueuedDBusException
*/ */
void initDBus(); void initDBus();
/**
* @brief method allows to init class if it was not created
* @tparam T
* class name
* @tparam Args
* class constructor arguments
* @param _dest
* pointer to destination
* @param _args
* class constructor arguments
*/
template<class T, typename... Args> T *initObject(T *_dest, Args... _args)
{
return _dest ? _dest : new T(this, _args...);
};
/** /**
* @brief init plugins * @brief init plugins
*/ */

View File

@ -57,6 +57,10 @@ public:
* @brief check and create database * @brief check and create database
*/ */
void checkDatabase(); void checkDatabase();
/**
* @brief close database connection
*/
void close();
/** /**
* @brief check and create queued administrator if missing * @brief check and create queued administrator if missing
* @param _user * @param _user

View File

@ -83,7 +83,7 @@ public:
* @brief path to plugin location * @brief path to plugin location
* @return full path to plugin location * @return full path to plugin location
*/ */
QStringList pluginLocations() const; static QStringList pluginLocations();
/** /**
* @brief unload plugin * @brief unload plugin
* @param _name * @param _name

View File

@ -58,15 +58,10 @@ public:
/** /**
* @brief QueuedProcessManager class constructor * @brief QueuedProcessManager class constructor
* @param parent * @param _parent
* pointer to parent item * pointer to parent item
* @param processLine
* command line pattern
* @param onExit
* default action on exit
*/ */
explicit QueuedProcessManager(QObject *parent, const QString &processLine, explicit QueuedProcessManager(QObject *_parent);
const QueuedEnums::ExitAction onExit);
/** /**
* @brief QueuedProcessManager class destructor * @brief QueuedProcessManager class destructor
*/ */

View File

@ -42,12 +42,12 @@ class QueuedSettings : public QObject
public: public:
/** /**
* @brief QueuedSettings class constructor * @brief QueuedSettings class constructor
* @param parent * @param _parent
* pointer to parent item * pointer to parent item
* @param path * @param _path
* path to configuration file * path to configuration file
*/ */
explicit QueuedSettings(QObject *parent, const QString path); explicit QueuedSettings(QObject *_parent, const QString _path);
/** /**
* @brief QueuedSettings class destructor * @brief QueuedSettings class destructor
*/ */

View File

@ -66,7 +66,7 @@ public:
* @brief check if token is valid * @brief check if token is valid
* @param _token * @param _token
* token ID * token ID
* @return token user if token is valid otherwise return false * @return token user if token is valid otherwise return empty string
*/ */
QString isTokenValid(const QString &_token) const; QString isTokenValid(const QString &_token) const;
/** /**

View File

@ -35,8 +35,8 @@
/** /**
* @fn QueuedCore * @fn QueuedCore
*/ */
QueuedCore::QueuedCore(QObject *parent) QueuedCore::QueuedCore(QObject *_parent)
: QObject(parent) : QObject(_parent)
{ {
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__; qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
} }
@ -565,23 +565,13 @@ void QueuedCore::deinit()
m_connections.clear(); m_connections.clear();
// dbus cleanup // dbus cleanup
QDBusConnection::sessionBus().unregisterObject( QDBusConnection::systemBus().unregisterObject(
QueuedConfig::DBUS_OBJECT_PATH); QueuedConfig::DBUS_OBJECT_PATH);
QDBusConnection::sessionBus().unregisterObject( QDBusConnection::systemBus().unregisterObject(
QueuedConfig::DBUS_PROPERTY_PATH); QueuedConfig::DBUS_PROPERTY_PATH);
QDBusConnection::sessionBus().unregisterObject( QDBusConnection::systemBus().unregisterObject(
QueuedConfig::DBUS_REPORTS_PATH); QueuedConfig::DBUS_REPORTS_PATH);
QDBusConnection::sessionBus().unregisterService(QueuedConfig::DBUS_SERVICE); QDBusConnection::systemBus().unregisterService(QueuedConfig::DBUS_SERVICE);
// delete objects now
delete m_databaseManager;
delete m_reports;
delete m_plugins;
delete m_processes;
delete m_users;
delete m_database;
delete m_settings;
delete m_advancedSettings;
} }
@ -787,7 +777,7 @@ void QueuedCore::initPlugins()
.split('\n'); .split('\n');
QString token = m_users->authorize(m_settings->admin().name); QString token = m_users->authorize(m_settings->admin().name);
m_plugins = new QueuedPluginManager(this, token); m_plugins = initObject(m_plugins, token);
for (auto &plugin : pluginList) for (auto &plugin : pluginList)
m_plugins->loadPlugin(plugin, pluginSettings(plugin)); m_plugins->loadPlugin(plugin, pluginSettings(plugin));
} }
@ -807,7 +797,9 @@ void QueuedCore::initProcesses()
->get(QueuedConfig::QueuedSettings::ProcessCommandLine) ->get(QueuedConfig::QueuedSettings::ProcessCommandLine)
.toString(); .toString();
m_processes = new QueuedProcessManager(this, processLine, onExitAction); m_processes = initObject(m_processes);
m_processes->setProcessLine(processLine);
m_processes->setExitAction(onExitAction);
auto dbProcesses auto dbProcesses
= m_database->get(QueuedDB::TASKS_TABLE, "WHERE endTime IS NULL"); = m_database->get(QueuedDB::TASKS_TABLE, "WHERE endTime IS NULL");
m_processes->loadProcesses(dbProcesses); m_processes->loadProcesses(dbProcesses);
@ -831,10 +823,12 @@ void QueuedCore::initProcesses()
void QueuedCore::initSettings(const QString &_configuration) void QueuedCore::initSettings(const QString &_configuration)
{ {
// read configuration first // read configuration first
m_settings = new QueuedSettings(this, _configuration); m_settings = initObject(m_settings, _configuration);
m_settings->readConfiguration();
// init database now // init database now
auto dbSetup = m_settings->db(); auto dbSetup = m_settings->db();
m_database = new QueuedDatabase(this, dbSetup.path, dbSetup.driver); m_database = initObject(m_database, dbSetup.path, dbSetup.driver);
m_database->close();
bool status = m_database->open(dbSetup.hostname, dbSetup.port, bool status = m_database->open(dbSetup.hostname, dbSetup.port,
dbSetup.username, dbSetup.password); dbSetup.username, dbSetup.password);
if (!status) { if (!status) {
@ -848,7 +842,7 @@ void QueuedCore::initSettings(const QString &_configuration)
m_database->createAdministrator(dbAdmin.name, dbAdmin.password); m_database->createAdministrator(dbAdmin.name, dbAdmin.password);
// and load advanced settings // and load advanced settings
m_advancedSettings = new QueuedAdvancedSettings(this); m_advancedSettings = initObject(m_advancedSettings);
m_advancedSettings->set(m_database->get(QueuedDB::SETTINGS_TABLE)); m_advancedSettings->set(m_database->get(QueuedDB::SETTINGS_TABLE));
if (!m_advancedSettings->checkDatabaseVersion()) { if (!m_advancedSettings->checkDatabaseVersion()) {
qCInfo(LOG_LIB) << "Bump database version to" qCInfo(LOG_LIB) << "Bump database version to"
@ -859,9 +853,9 @@ void QueuedCore::initSettings(const QString &_configuration)
} }
// report manager // report manager
m_reports = new QueuedReportManager(this, m_database); m_reports = initObject(m_reports, m_database);
// database manager // database manager
m_databaseManager = new QueuedDatabaseManager(this, m_database); m_databaseManager = initObject(m_databaseManager, m_database);
} }
@ -875,7 +869,7 @@ void QueuedCore::initUsers()
= m_advancedSettings->get(QueuedConfig::QueuedSettings::TokenExpiration) = m_advancedSettings->get(QueuedConfig::QueuedSettings::TokenExpiration)
.toLongLong(); .toLongLong();
m_users = new QueuedUserManager(this); m_users = initObject(m_users);
m_users->setTokenExpiration(expiry); m_users->setTokenExpiration(expiry);
QString now = QDateTime::currentDateTimeUtc().toString(Qt::ISODateWithMs); QString now = QDateTime::currentDateTimeUtc().toString(Qt::ISODateWithMs);
auto dbTokens = m_database->get( auto dbTokens = m_database->get(

View File

@ -52,7 +52,7 @@ QueuedDatabase::~QueuedDatabase()
{ {
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__; qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
m_database.close(); close();
} }
@ -73,6 +73,15 @@ void QueuedDatabase::checkDatabase()
} }
/**
* @fn close
*/
void QueuedDatabase::close()
{
m_database.close();
}
/** /**
* @fn createAdministrator * @fn createAdministrator
*/ */

View File

@ -99,6 +99,14 @@ bool QueuedPluginManager::loadPlugin(const QString &_name,
{ {
qCDebug(LOG_PL) << "Load plugin" << _name << "with settings" << _settings; qCDebug(LOG_PL) << "Load plugin" << _name << "with settings" << _settings;
// check if it was loaded already then call QueuedPluginInterface::init()
if (m_plugins.contains(_name)) {
m_plugins[_name]->init(_settings);
m_plugins[_name]->setToken(m_token);
return true;
}
// normal load
QString libraryName = QString("lib%2.so").arg(_name); QString libraryName = QString("lib%2.so").arg(_name);
// init plugin settings with valid keys // init plugin settings with valid keys
QVariantHash pluginSettings; QVariantHash pluginSettings;
@ -138,7 +146,7 @@ bool QueuedPluginManager::loadPlugin(const QString &_name,
/** /**
* @fn pluginLocations * @fn pluginLocations
*/ */
QStringList QueuedPluginManager::pluginLocations() const QStringList QueuedPluginManager::pluginLocations()
{ {
QStringList locations = QStandardPaths::standardLocations( QStringList locations = QStandardPaths::standardLocations(
QStandardPaths::GenericDataLocation); QStandardPaths::GenericDataLocation);

View File

@ -31,17 +31,12 @@ extern "C" {
/** /**
* @fn QueuedProcessManager * @fn QueuedProcessManager
*/ */
QueuedProcessManager::QueuedProcessManager(QObject *parent, QueuedProcessManager::QueuedProcessManager(QObject *_parent)
const QString &processLine, : QObject(_parent)
const QueuedEnums::ExitAction onExit)
: QObject(parent)
{ {
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__; qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
qRegisterMetaType<QueuedEnums::ExitAction>("QueuedEnums::ExitAction"); qRegisterMetaType<QueuedEnums::ExitAction>("QueuedEnums::ExitAction");
setExitAction(onExit);
setProcessLine(processLine);
} }

View File

@ -33,9 +33,9 @@
/** /**
* @fn QueuedSettings * @fn QueuedSettings
*/ */
QueuedSettings::QueuedSettings(QObject *parent, const QString path) QueuedSettings::QueuedSettings(QObject *_parent, const QString _path)
: QObject(parent) : QObject(_parent)
, m_path(path) , m_path(_path)
{ {
qCDebug(LOG_LIB) << __PRETTY_FUNCTION__; qCDebug(LOG_LIB) << __PRETTY_FUNCTION__;
@ -43,8 +43,6 @@ QueuedSettings::QueuedSettings(QObject *parent, const QString path)
"QueuedConfig::QueuedAdminSetup"); "QueuedConfig::QueuedAdminSetup");
qRegisterMetaType<QueuedConfig::QueuedDBSetup>( qRegisterMetaType<QueuedConfig::QueuedDBSetup>(
"QueuedConfig::QueuedDBSetup"); "QueuedConfig::QueuedDBSetup");
readConfiguration();
} }

View File

@ -109,7 +109,7 @@ QString QueuedTokenManager::registerToken(const QString &_user,
const QDateTime &_validUntil) const QDateTime &_validUntil)
{ {
// generate from uuid // generate from uuid
QString &token = QUuid::createUuid().toString().remove('{').remove('}'); QString token = QUuid::createUuid().toString().remove('{').remove('}');
qCInfo(LOG_LIB) << "Registered token" << token << "valid until" qCInfo(LOG_LIB) << "Registered token" << token << "valid until"
<< _validUntil; << _validUntil;