diff --git a/sources/queued/include/queued/QueuedAdvancedSettings.h b/sources/queued/include/queued/QueuedAdvancedSettings.h index 724ac7d..7a3ef15 100644 --- a/sources/queued/include/queued/QueuedAdvancedSettings.h +++ b/sources/queued/include/queued/QueuedAdvancedSettings.h @@ -51,6 +51,12 @@ public: * @return value by key if found */ QVariant get(const QString &_key) const; + /** + * @brief get database value ID + * @param _key key to search in + * @return database id or -1 if not found + */ + long long id(const QString &_key) const; /** * @brief set value * @param _key key to change @@ -71,6 +77,10 @@ private: * @brief stored values */ QVariantHash m_values; + /** + * @brief ids of values + */ + QHash m_ids; }; diff --git a/sources/queued/include/queued/QueuedCore.h b/sources/queued/include/queued/QueuedCore.h index 95ad4ee..387bbb4 100644 --- a/sources/queued/include/queued/QueuedCore.h +++ b/sources/queued/include/queued/QueuedCore.h @@ -26,6 +26,7 @@ #include +#include "QueuedEnums.h" #include "QueuedLimits.h" @@ -83,14 +84,41 @@ public: * @brief deinit subclasses */ void deinit(); + /** + * @brief edit advanced settings + * @param _key advanced settings key + * @param _value advanced settings value + * @return true on successful option edition + */ + bool editOption(const QString &_key, const QVariant &_value); + /** + * @brief edit task + * @param _id task ID to edit + * @param _taskData task data to edit + * @remark _taskData should contain only fields defined in schema, any other + * fields will be ignored. No need to pass all properties here + * @return true on successful task edition + */ + bool editTask(const long long _id, const QVariantHash &_taskData); /** * @brief edit user * @param _id user ID to edit * @param _userData user data to edit - * @remark ref - * @return + * @remark _userData should contain only fields defined in schema, any other + * fields will be ignored. No need to pass all properties here + * @return true on successful user edition */ bool editUser(const long long _id, const QVariantHash &_userData); + /** + * @brief edit user permissions + * @param _id user ID to edit + * @param _permission permission to add or remove + * @param _add indicates whether it should be added or removed + * @return true on successful user permission edition + */ + bool editUserPermission(const long long _id, + const QueuedEnums::Permission &_permission, + const bool _add); /** * @brief init subclasses * @param _configuration path to configuration file @@ -98,6 +126,12 @@ public: void init(const QString &_configuration); private slots: + /** + * @brief notify clients about settings update + * @param _key updated key + * @param _value new value + */ + void updateSettings(const QString &_key, const QVariant &_value); /** * @brief update process time * @param _id task id diff --git a/sources/queued/include/queued/QueuedDatabaseSchema.h b/sources/queued/include/queued/QueuedDatabaseSchema.h index 2ccb999..94fceac 100644 --- a/sources/queued/include/queued/QueuedDatabaseSchema.h +++ b/sources/queued/include/queued/QueuedDatabaseSchema.h @@ -89,13 +89,14 @@ const QueuedDBSchema DBSchema = { {TASKS_TABLE, {{"_id", {"_id", "INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE", QVariant::LongLong}}, - {"userId", {"userId", "INT NOT NULL DEFAULT 0", QVariant::LongLong}}, + {"user", {"user", "INT NOT NULL DEFAULT 0", QVariant::LongLong}}, {"command", {"command", "TEXT", QVariant::String}}, - {"arguments", {"arguments", "TEXT", QVariant::String}}, + {"commandArguments", {"commandArguments", "TEXT", QVariant::String}}, {"workDirectory", {"workDirectory", "TEXT", QVariant::String}}, {"nice", {"nice", "INT", QVariant::UInt}}, {"uid", {"uid", "INT", QVariant::UInt}}, {"gid", {"gid", "INT", QVariant::UInt}}, + {"state", {"state", "INT", QVariant::UInt}}, {"limits", {"limits", "TEXT", QVariant::String}}, {"startTime", {"startTime", "INT", QVariant::LongLong}}, {"endTime", {"endTime", "INT", QVariant::LongLong}}}}, @@ -110,7 +111,7 @@ const QueuedDBSchema DBSchema = { {{"_id", {"_id", "INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE", QVariant::LongLong}}, {"name", {"name", "TEXT NOT NULL UNIQUE DEFAULT '0'", QVariant::String}}, - {"passwordSHA512", {"passwordSHA512", "TEXT", QVariant::String}}, + {"password", {"password", "TEXT", QVariant::String}}, {"email", {"email", "TEXT", QVariant::String}}, {"lastLogin", {"lastLogin", "TEXT", QVariant::String}}, {"limits", {"limits", "TEXT", QVariant::String}}, diff --git a/sources/queued/include/queued/QueuedEnums.h b/sources/queued/include/queued/QueuedEnums.h index 3327071..8dbc6bd 100644 --- a/sources/queued/include/queued/QueuedEnums.h +++ b/sources/queued/include/queued/QueuedEnums.h @@ -83,6 +83,22 @@ enum class Permission { }; Q_DECLARE_FLAGS(Permissions, Permission) Q_DECLARE_OPERATORS_FOR_FLAGS(Permissions) +/** + * @ingroup QueuedEnums + * @enum ProcessState + * @brief process state description + * @var ProcessState::NotRunning + * process is not running yet + * @var ProcessState::Running + * process running at the moment + * @var ProcessState::Exited + * process exit + */ +enum class ProcessState { + NotRunning = 1 << 0, + Running = 1 << 1, + Exited = 1 << 2 +}; }; diff --git a/sources/queued/include/queued/QueuedProcess.h b/sources/queued/include/queued/QueuedProcess.h index ef1710e..886745d 100644 --- a/sources/queued/include/queued/QueuedProcess.h +++ b/sources/queued/include/queued/QueuedProcess.h @@ -27,6 +27,7 @@ #include #include +#include "QueuedEnums.h" #include "QueuedLimits.h" @@ -38,6 +39,19 @@ class QueuedProcess : public QProcess Q_OBJECT Q_PROPERTY(long long index READ index) Q_PROPERTY(QString name READ name) + // mutable properties + Q_PROPERTY(QString command READ command WRITE setCommand) + Q_PROPERTY(QStringList commandArguments READ commandArguments WRITE + setCommandArguments) + Q_PROPERTY(QDateTime endTime READ endTime WRITE setEndTime) + Q_PROPERTY(unsigned int gid READ uid WRITE setGid) + Q_PROPERTY(QueuedLimits::Limits limits READ limits WRITE setLimits) + Q_PROPERTY(unsigned int nice READ nice WRITE setNice) + Q_PROPERTY(QueuedEnums::ProcessState pstate READ pstate WRITE setPState) + Q_PROPERTY(QDateTime startTime READ startTime WRITE setStartTime) + Q_PROPERTY(unsigned int uid READ uid WRITE setUid) + Q_PROPERTY(long long user READ user WRITE setUser) + Q_PROPERTY(QString workDirectory READ workDirectory WRITE setWorkDirectory) public: /** @@ -63,6 +77,8 @@ public: * task owner ID * @var limits * task limits + * @var state + * current task state */ typedef struct { QString command; @@ -75,6 +91,7 @@ public: QDateTime endTime; long long user; QueuedLimits::Limits limits; + QueuedEnums::ProcessState state; } QueuedProcessDefinitions; /** @@ -100,6 +117,117 @@ public: * @return generated name of process */ QString name() const; + // mutable properties + /** + * @brief command line + * @return process command line + */ + QString command() const; + /** + * @brief command line arguments + * @return process command line arguments + */ + QStringList commandArguments() const; + /** + * @brief process end time + * @return process end time + */ + QDateTime endTime() const; + /** + * @brief process GID + * @return process GID + */ + unsigned int gid() const; + /** + * @brief process limits + * @return process defined limits + */ + QueuedLimits::Limits limits() const; + /** + * @brief process nice + * @return process nice + */ + unsigned int nice() const; + /** + * @brief process state + * @return process defined state + */ + QueuedEnums::ProcessState pstate() const; + /** + * @brief process start time + * @return process start time + */ + QDateTime startTime() const; + /** + * @brief process UID + * @return process UID + */ + unsigned int uid() const; + /** + * @brief user + * @return process owner ID + */ + long long user() const; + /** + * @brief working directory + * @return process working directory + */ + QString workDirectory() const; + /** + * @brief set command line + * @param _command new command line + */ + void setCommand(const QString &_command); + /** + * @brief set command line arguments + * @param _commandArguments new command line arguments + */ + void setCommandArguments(const QStringList &_commandArguments); + /** + * @brief set end time + * @param _time process end time + */ + void setEndTime(const QDateTime &_time); + /** + * @brief set process GID + * @param _gid new process GID + */ + void setGid(const unsigned int _gid); + /** + * @brief set process limits + * @param _limits new process limits + */ + void setLimits(const QueuedLimits::Limits &_limits); + /** + * @brief set process nice + * @param _nice new process nice + */ + void setNice(const unsigned int _nice); + /** + * @brief set process state + * @param _limits new process state + */ + void setPState(const QueuedEnums::ProcessState _state); + /** + * @brief set start time + * @param _time process start time + */ + void setStartTime(const QDateTime &_time); + /** + * @brief set process UID + * @param _uid new process UID + */ + void setUid(const unsigned int _uid); + /** + * @brief set user ID + * @param _user new user ID + */ + void setUser(const long long _user); + /** + * @brief set working directory + * @param _workDirectory new process working directory + */ + void setWorkDirectory(const QString &_workDirectory); /** * @brief equal operator implementation * @param _other other object @@ -113,17 +241,6 @@ protected: */ void setupChildProcess(); -public slots: - /** - * @brief run process - */ - void run(); - /** - * @brief set end time - * @param _time process end time - */ - void setEndTime(const QDateTime &_time); - private: /** * @brief process definitions diff --git a/sources/queued/include/queued/QueuedProcessManager.h b/sources/queued/include/queued/QueuedProcessManager.h index b7193a7..e08baaf 100644 --- a/sources/queued/include/queued/QueuedProcessManager.h +++ b/sources/queued/include/queued/QueuedProcessManager.h @@ -37,7 +37,7 @@ class QueuedProcessManager : public QObject { Q_OBJECT - Q_PROPERTY(OnExitAction onExit READ onExit) + Q_PROPERTY(OnExitAction onExit READ onExit WRITE setOnExitAction) public: /** @@ -109,16 +109,27 @@ public: * @param _index task index */ void remove(const long long _index); + /** + * @brief force start task + * @param _index task index + */ + void start(const long long _index); /** * @brief force stop task * @param _index task index */ void stop(const long long _index); + // properties /** * @brief default action on exit * @return default action from possible ones */ OnExitAction onExit() const; + /** + * @brief set on exit action + * @param _action new on exit action + */ + void setOnExitAction(const OnExitAction _action); signals: /** diff --git a/sources/queued/include/queued/QueuedTokenManager.h b/sources/queued/include/queued/QueuedTokenManager.h index d6b10bd..5d4f564 100644 --- a/sources/queued/include/queued/QueuedTokenManager.h +++ b/sources/queued/include/queued/QueuedTokenManager.h @@ -51,17 +51,17 @@ public: * @return true if token is valid otherwise return false */ bool isTokenValid(const QString &_token); + /** + * @brief upload tokens from database + * @param _value tokens from database + */ + void loadTokens(const QList &_values); /** * @brief register new token * @param _validUntil token valid until * @return new generated token */ QString registerToken(const QDateTime _validUntil); - /** - * @brief upload tokens from database - * @param _value tokens from database - */ - void set(const QList &_values); public slots: /** diff --git a/sources/queued/include/queued/QueuedUser.h b/sources/queued/include/queued/QueuedUser.h index 2a84f64..3d1bcdd 100644 --- a/sources/queued/include/queued/QueuedUser.h +++ b/sources/queued/include/queued/QueuedUser.h @@ -39,15 +39,12 @@ class QueuedUser : public QObject Q_OBJECT Q_PROPERTY(long long index READ index) // generic properties - Q_PROPERTY(QString email READ email WRITE setEmail NOTIFY userUpdated) - Q_PROPERTY(QString name READ name WRITE setName NOTIFY userUpdated) - Q_PROPERTY( - QString password READ password WRITE setPassword NOTIFY userUpdated) - Q_PROPERTY(unsigned int permissions READ permissions WRITE setPermissions - NOTIFY userUpdated) + Q_PROPERTY(QString email READ email WRITE setEmail) + Q_PROPERTY(QString name READ name WRITE setName) + Q_PROPERTY(QString password READ password WRITE setPassword) + Q_PROPERTY(unsigned int permissions READ permissions WRITE setPermissions) // limits - Q_PROPERTY(QueuedLimits::Limits limits READ limits WRITE setLimits NOTIFY - userUpdated) + Q_PROPERTY(QueuedLimits::Limits limits READ limits WRITE setLimits) public: /** @@ -57,7 +54,7 @@ public: * user name * @var email * user email - * @var passwordSHA512 + * @var password * password hash, may be empty * @var permissions * user permissions @@ -67,7 +64,7 @@ public: typedef struct { QString name; QString email; - QString passwordSHA512; + QString password; unsigned int permissions; QueuedLimits::Limits limits; } QueuedUserDefinitions; @@ -189,12 +186,6 @@ public: */ bool operator==(const QueuedUser &_other); -signals: - /** - * @brief signal which emits on any property set action - */ - void userUpdated(); - private: /** * @brief process definitions diff --git a/sources/queued/src/QueuedAdvancedSettings.cpp b/sources/queued/src/QueuedAdvancedSettings.cpp index 007df96..2981bed 100644 --- a/sources/queued/src/QueuedAdvancedSettings.cpp +++ b/sources/queued/src/QueuedAdvancedSettings.cpp @@ -53,7 +53,18 @@ QVariant QueuedAdvancedSettings::get(const QString &_key) const { qCDebug(LOG_LIB) << "Looking for key" << _key; - return m_values[_key]; + return m_values.value(_key.toLower(), QVariant()); +} + + +/** + * @fn id + */ +long long QueuedAdvancedSettings::id(const QString &_key) const +{ + qCDebug(LOG_LIB) << "Looking for ID for key" << _key; + + return m_ids.value(_key.toLower(), -1); } @@ -64,7 +75,7 @@ void QueuedAdvancedSettings::set(const QString &_key, const QVariant &_value) { qCDebug(LOG_LIB) << "Set value" << _value << "for key" << _key; - m_values[_key] = _value; + m_values[_key.toLower()] = _value; emit(valueUpdated(_key, _value)); } @@ -76,6 +87,9 @@ void QueuedAdvancedSettings::set(const QList &_values) { qCDebug(LOG_LIB) << "Set values from" << _values; - for (auto &pair : _values) - set(pair[QString("key")].toString(), pair[QString("value")]); + for (auto &pair : _values) { + QString key = pair[QString("key")].toString().toLower(); + m_ids[key] = pair[QString("_id")].toLongLong(); + set(key, pair[QString("value")]); + } } diff --git a/sources/queued/src/QueuedCore.cpp b/sources/queued/src/QueuedCore.cpp index 98ea7df..6993fab 100644 --- a/sources/queued/src/QueuedCore.cpp +++ b/sources/queued/src/QueuedCore.cpp @@ -66,11 +66,14 @@ bool QueuedCore::addTask(const QString &_command, const QStringList &_arguments, _limits, m_users->user(_userId)->limits(), QueuedLimits::Limits( m_advancedSettings->get(QString("DefaultLimits")).toString())); - QVariantHash properties - = {{"userId", _userId}, {"command", _command}, - {"arguments", _arguments}, {"workDirectory", _workingDirectory}, - {"nice", _nice}, {"uid", ids.first}, - {"gid", ids.second}, {"limits", taskLimits.toString()}}; + QVariantHash properties = {{"user", _userId}, + {"command", _command}, + {"commandArguments", _arguments}, + {"workDirectory", _workingDirectory}, + {"nice", _nice}, + {"uid", ids.first}, + {"gid", ids.second}, + {"limits", taskLimits.toString()}}; auto id = m_database->add(QueuedDB::TASKS_TABLE, properties); if (id == -1) { qCWarning(LOG_LIB) << "Could not add task" << _command; @@ -95,7 +98,7 @@ bool QueuedCore::addUser(const QString &_name, const QString &_email, QVariantHash properties = {{"name", _name}, - {"passwordSHA512", QueuedUser::hashFromPassword(_password)}, + {"password", QueuedUser::hashFromPassword(_password)}, {"email", _email}, {"permissions", _permissions}, {"limits", _limits.toString()}}; @@ -136,6 +139,136 @@ void QueuedCore::deinit() } +/** + * @fn editOption + */ +bool QueuedCore::editOption(const QString &_key, const QVariant &_value) +{ + qCDebug(LOG_LIB) << "Set key" << _key << "to" << _value; + + long long id = m_advancedSettings->id(_key); + QVariantHash payload = { + {"key", _key}, {"value", _value}, + }; + + bool status = false; + if (id == -1) { + id = m_database->add(QueuedDB::SETTINGS_TABLE, payload); + qCInfo(LOG_LIB) << "Added new key with ID" << id; + status = (id != -1); + } else { + status = m_database->modify(QueuedDB::SETTINGS_TABLE, id, payload); + qCInfo(LOG_LIB) << "Value for" << _key + << "has been modified with status" << status; + } + + if (status) + m_advancedSettings->set(_key, _value); + return status; +} + + +/** + * @fn editTask + */ +bool QueuedCore::editTask(const long long _id, const QVariantHash &_taskData) +{ + qCDebug(LOG_LIB) << "Edit task with ID" << _id; + + auto task = m_processes->process(_id); + if (!task) { + qCWarning(LOG_LIB) << "Could not find task with ID" << _id; + return false; + } + + // modify record in database first + bool status = m_database->modify(QueuedDB::TASKS_TABLE, _id, _taskData); + if (!status) { + qCWarning(LOG_LIB) << "Could not modify task record" << _id + << "in database, do not edit it in memory"; + return false; + } + + // modify values stored in memory + for (auto &property : _taskData.keys()) + task->setProperty(property.toLocal8Bit().constData(), + _taskData[property]); + + return true; +} + + +/** + * @fn editUser + */ +bool QueuedCore::editUser(const long long _id, const QVariantHash &_userData) +{ + qCDebug(LOG_LIB) << "Edit user with ID" << _id; + + auto user = m_users->user(_id); + if (!user) { + qCWarning(LOG_LIB) << "Could not find user with ID" << _id; + return false; + } + + // modify record in database first + bool status = m_database->modify(QueuedDB::USERS_TABLE, _id, _userData); + if (!status) { + qCWarning(LOG_LIB) << "Could not modify user record" << _id + << "in database, do not edit it in memory"; + return false; + } + + // modify values stored in memory + for (auto &property : _userData.keys()) + user->setProperty(property.toLocal8Bit().constData(), + _userData[property]); + + return true; +} + + +/** + * @fn editUserPermission + */ +bool QueuedCore::editUserPermission(const long long _id, + const QueuedEnums::Permission &_permission, + const bool _add) +{ + qCDebug(LOG_LIB) << "Edit permissions" << static_cast(_permission) + << "for user" << _id << "add" << _add; + + auto user = m_users->user(_id); + if (!user) { + qCWarning(LOG_LIB) << "Could not find user with ID" << _id; + return false; + } + + if (_add) + user->addPermissions(_permission); + else + user->removePermissions(_permission); + unsigned int permissions = user->permissions(); + qCInfo(LOG_LIB) << "New user permissions"; + + // modify in database now + QVariantHash payload = {{"permissions", permissions}}; + bool status = m_database->modify(QueuedDB::USERS_TABLE, _id, payload); + if (!status) { + qCWarning(LOG_LIB) << "Could not modify user record" << _id + << "in database, do not edit it in memory"; + // rollback in-memory values + if (_add) + user->removePermissions(_permission); + else + user->addPermissions(_permission); + return false; + } + + return true; +} + + /** * @fn init */ @@ -189,6 +322,33 @@ void QueuedCore::init(const QString &_configuration) [this](const long long _index, const QDateTime &_time) { return updateTaskTime(_index, QDateTime(), _time); }); + + // settings update notifier + m_connections += connect( + m_advancedSettings, + SIGNAL(valueUpdated(const QString &, const QVariant &)), this, + SLOT(updateSettings(const QString &, const QVariant &))); +} + + +/** + * @fn updateSettings + */ +void QueuedCore::updateSettings(const QString &_key, const QVariant &_value) +{ + qCDebug(LOG_LIB) << "Received update for" << _key << "with value" << _value; + + // FIXME propbably there is a better way to change settings + QString key = _key.toLower(); + if (key == QString("defaultlimits")) + ; + else if (key == QString("tokenexpiration")) + m_users->setTokenExpiration(_value.toLongLong()); + else if (key == QString("onexitaction")) + m_processes->setOnExitAction( + static_cast(_value.toInt())); + else + qCInfo(LOG_LIB) << "Unused key" << _key; } diff --git a/sources/queued/src/QueuedDatabase.cpp b/sources/queued/src/QueuedDatabase.cpp index 617796a..5799f92 100644 --- a/sources/queued/src/QueuedDatabase.cpp +++ b/sources/queued/src/QueuedDatabase.cpp @@ -354,6 +354,10 @@ QueuedDatabase::getQueryPayload(const QString &_table, << _table; continue; } + if (key == QString("_id")) { + qCWarning(LOG_LIB) << "Modifying record ID is not allowed"; + continue; + } keys.append(key); values.append(QString("'%1'").arg(_value[key].toString())); } diff --git a/sources/queued/src/QueuedProcess.cpp b/sources/queued/src/QueuedProcess.cpp index d7f8531..14a9427 100644 --- a/sources/queued/src/QueuedProcess.cpp +++ b/sources/queued/src/QueuedProcess.cpp @@ -45,9 +45,10 @@ QueuedProcess::QueuedProcess(QObject *parent, { qCDebug(LOG_LIB) << __PRETTY_FUNCTION__; - setArguments(m_definitions.arguments); - setProgram(m_definitions.command); - setWorkingDirectory(m_definitions.workingDirectory); + // update QProcess related values as well + setCommand(m_definitions.command); + setCommandArguments(m_definitions.arguments); + setWorkDirectory(m_definitions.workingDirectory); } @@ -78,6 +79,229 @@ QString QueuedProcess::name() const } +/** + * @fn command + */ +QString QueuedProcess::command() const +{ + return m_definitions.command; +} + + +/** + * @fn commandArguments + */ +QStringList QueuedProcess::commandArguments() const +{ + return m_definitions.arguments; +} + + +/** + * @fn endTime + */ +QDateTime QueuedProcess::endTime() const +{ + return m_definitions.endTime; +} + + +/** + * @fn gid + */ +unsigned int QueuedProcess::gid() const +{ + return m_definitions.gid; +} + + +/** + * @fn limits + */ +QueuedLimits::Limits QueuedProcess::limits() const +{ + return m_definitions.limits; +} + + +/** + * @fn nice + */ +unsigned int QueuedProcess::nice() const +{ + return m_definitions.nice; +} + + +/** + * @fn pstate + */ +QueuedEnums::ProcessState QueuedProcess::pstate() const +{ + return m_definitions.state; +} + + +/** + * @fn startTime + */ +QDateTime QueuedProcess::startTime() const +{ + return m_definitions.startTime; +} + + +/** + * @fn uid + */ +unsigned int QueuedProcess::uid() const +{ + return m_definitions.uid; +} + + +/** + * @fn user + */ +long long QueuedProcess::user() const +{ + return m_definitions.user; +} + + +/** + * @fn workDirectory + */ +QString QueuedProcess::workDirectory() const +{ + return m_definitions.workingDirectory; +} + + +/** + * @fn setCommand + */ +void QueuedProcess::setCommand(const QString &_command) +{ + qCDebug(LOG_LIB) << "Set command to" << _command; + + m_definitions.command = _command; + setProgram(_command); +} + + +/** + * @fn setCommandArguments + */ +void QueuedProcess::setCommandArguments(const QStringList &_commandArguments) +{ + qCDebug(LOG_LIB) << "Set command line arguments to" << _commandArguments; + + m_definitions.arguments = _commandArguments; + setArguments(_commandArguments); +} + + +/** + * @fn setEndTime + */ +void QueuedProcess::setEndTime(const QDateTime &_time) +{ + qCDebug(LOG_LIB) << "Set end time to" << _time; + + m_definitions.endTime = _time; +} + + +/** + * @fn setGid + */ +void QueuedProcess::setGid(const unsigned int _gid) +{ + qCDebug(LOG_LIB) << "Set process GID to" << _gid; + + m_definitions.gid = _gid; +} + + +/** + * setLimits + */ +void QueuedProcess::setLimits(const QueuedLimits::Limits &_limits) +{ + qCDebug(LOG_LIB) << "Set process limits" << _limits.toString(); + + m_definitions.limits = _limits; +} + + +/** + * @fn setNice + */ +void QueuedProcess::setNice(const unsigned int _nice) +{ + qCDebug(LOG_LIB) << "Set nice level to" << _nice; + + m_definitions.nice = _nice; +} + + +/** + * @fn setPState + */ +void QueuedProcess::setPState(const QueuedEnums::ProcessState _state) +{ + qCDebug(LOG_LIB) << "Set process state to" << static_cast(_state); + + m_definitions.state = _state; +} + + +/** + * @fn setStartTime + */ +void QueuedProcess::setStartTime(const QDateTime &_time) +{ + qCDebug(LOG_LIB) << "Set start time to" << _time; + + m_definitions.startTime = _time; +} + + +/** + * @fn setUid + */ +void QueuedProcess::setUid(const unsigned int _uid) +{ + qCDebug(LOG_LIB) << "Set process UID to" << _uid; + + m_definitions.uid = _uid; +} + + +/** + * @fn setUser + */ +void QueuedProcess::setUser(const long long _user) +{ + qCDebug(LOG_LIB) << "Set user to" << _user; + + m_definitions.user = _user; +} + + +/** + * @fn setWorkDirectory + */ +void QueuedProcess::setWorkDirectory(const QString &_workDirectory) +{ + qCDebug(LOG_LIB) << "Set working directory to" << _workDirectory; + + m_definitions.workingDirectory = _workDirectory; + setWorkingDirectory(_workDirectory); +} + + /** * @fn operator== */ @@ -95,24 +319,3 @@ void QueuedProcess::setupChildProcess() ::setgid(m_definitions.gid); ::setuid(m_definitions.uid); } - - -/** - * @fn run - */ -void QueuedProcess::run() -{ - m_definitions.startTime = QDateTime::currentDateTimeUtc(); - return start(); -} - - -/** - * @fn setEndTime - */ -void QueuedProcess::setEndTime(const QDateTime &_time) -{ - qCDebug(LOG_LIB) << "Set end time to" << _time; - - m_definitions.endTime = _time; -} diff --git a/sources/queued/src/QueuedProcessManager.cpp b/sources/queued/src/QueuedProcessManager.cpp index 899bfb6..9f5e291 100644 --- a/sources/queued/src/QueuedProcessManager.cpp +++ b/sources/queued/src/QueuedProcessManager.cpp @@ -31,9 +31,10 @@ QueuedProcessManager::QueuedProcessManager(QObject *parent, const OnExitAction onExit) : QObject(parent) - , m_onExit(onExit) { qCDebug(LOG_LIB) << __PRETTY_FUNCTION__; + + setOnExitAction(onExit); } @@ -62,8 +63,8 @@ QueuedProcess *QueuedProcessManager::add(const QVariantHash &_properties, QueuedProcess::QueuedProcessDefinitions defs; // parameters defs.command = _properties[QString("command")].toString(); - defs.arguments - = _properties[QString("arguments")].toString().split(QChar('\x01')); + defs.arguments = _properties[QString("commandArguments")].toString().split( + QChar('\x01')); defs.workingDirectory = _properties[QString("workDirectory")].toString(); defs.nice = _properties[QString("nice")].toUInt(); defs.limits @@ -116,8 +117,13 @@ void QueuedProcessManager::loadProcesses(const QList &_processes) { qCDebug(LOG_LIB) << "Add tasks from" << _processes; - for (auto &processData : _processes) + for (auto &processData : _processes) { + if (static_cast( + processData[QString("state")].toUInt()) + == QueuedEnums::ProcessState::Exited) + continue; add(processData, processData[QString("_id")].toLongLong()); + } } @@ -169,6 +175,23 @@ void QueuedProcessManager::remove(const long long _index) } +/** + * @fn start + */ + void QueuedProcessManager::start(const long long _index) +{ + qCDebug(LOG_LIB) << "Start task" << _index; + + auto pr = process(_index); + if (!pr) { + qCWarning(LOG_LIB) << "No task" << _index << "found"; + return; + } + + pr->start(); +} + + /** * @fn stop */ @@ -203,6 +226,17 @@ QueuedProcessManager::OnExitAction QueuedProcessManager::onExit() const } +/** + * @fn setOnExitAction + */ +void QueuedProcessManager::setOnExitAction(const OnExitAction _action) +{ + qCDebug(LOG_LIB) << "New action on exit" << static_cast(_action); + + m_onExit = _action; +} + + /** * @fn taskFinished */ diff --git a/sources/queued/src/QueuedTokenManager.cpp b/sources/queued/src/QueuedTokenManager.cpp index 32baf67..8be4061 100644 --- a/sources/queued/src/QueuedTokenManager.cpp +++ b/sources/queued/src/QueuedTokenManager.cpp @@ -61,29 +61,9 @@ bool QueuedTokenManager::isTokenValid(const QString &_token) /** - * @fn registerToken + * @fn loadTokens */ -QString QueuedTokenManager::registerToken(const QDateTime _validUntil) -{ - // generate from uuid - QString token - = QUuid::createUuid().toString().remove(QChar('{')).remove(QChar('}')); - qCInfo(LOG_LIB) << "Registered token" << token << "valid until" - << _validUntil; - - // append to internal storage - m_tokens[token] = _validUntil; - emit(tokenRegistered(token, _validUntil)); - - // and return requester - return token; -} - - -/** - * @fn set - */ -void QueuedTokenManager::set(const QList &_values) +void QueuedTokenManager::loadTokens(const QList &_values) { qCDebug(LOG_LIB) << "Set values from" << _values; @@ -103,6 +83,26 @@ void QueuedTokenManager::set(const QList &_values) } +/** + * @fn registerToken + */ +QString QueuedTokenManager::registerToken(const QDateTime _validUntil) +{ + // generate from uuid + QString token + = QUuid::createUuid().toString().remove(QChar('{')).remove(QChar('}')); + qCInfo(LOG_LIB) << "Registered token" << token << "valid until" + << _validUntil; + + // append to internal storage + m_tokens[token] = _validUntil; + emit(tokenRegistered(token, _validUntil)); + + // and return requester + return token; +} + + /** * @fn expireToken */ diff --git a/sources/queued/src/QueuedUser.cpp b/sources/queued/src/QueuedUser.cpp index 169c56d..3d32b4f 100644 --- a/sources/queued/src/QueuedUser.cpp +++ b/sources/queued/src/QueuedUser.cpp @@ -122,7 +122,7 @@ QPair QueuedUser::ids() */ bool QueuedUser::isPasswordValid(const QString &_password) const { - return (m_definitions.passwordSHA512.toUtf8() + return (m_definitions.password.toUtf8() == QCryptographicHash::hash(_password.toUtf8(), QCryptographicHash::Sha512)); } @@ -173,7 +173,7 @@ QString QueuedUser::name() const */ QString QueuedUser::password() const { - return m_definitions.passwordSHA512; + return m_definitions.password; } @@ -224,7 +224,7 @@ void QueuedUser::setPassword(const QString _password) { qCDebug(LOG_LIB) << "New user passoword SHA" << _password; - m_definitions.passwordSHA512 = _password; + m_definitions.password = _password; } diff --git a/sources/queued/src/QueuedUserManager.cpp b/sources/queued/src/QueuedUserManager.cpp index 1dedd13..3081ddc 100644 --- a/sources/queued/src/QueuedUserManager.cpp +++ b/sources/queued/src/QueuedUserManager.cpp @@ -64,7 +64,7 @@ QueuedUser *QueuedUserManager::add(const QVariantHash &_properties, QueuedUser::QueuedUserDefinitions defs; defs.name = _properties[QString("name")].toString(); defs.email = _properties[QString("email")].toString(); - defs.passwordSHA512 = _properties[QString("passwordSHA512")].toString(); + defs.password = _properties[QString("password")].toString(); defs.permissions = _properties[QString("permissions")].toUInt(); defs.limits = QueuedLimits::Limits(_properties[QString("limits")].toString()); @@ -169,7 +169,7 @@ void QueuedUserManager::loadTokens(const QList &_tokens) { qCDebug(LOG_LIB) << "Set tokens from" << _tokens; - m_tokens->set(_tokens); + m_tokens->loadTokens(_tokens); }