4
0
mirror of https://github.com/QuasarApp/Patronum.git synced 2025-05-11 06:29:35 +00:00

fix start new service

This commit is contained in:
Andrei Yankovich 2021-10-13 19:33:30 +03:00
parent 3294521d90
commit 751aabb2bd
8 changed files with 58 additions and 6 deletions

@ -90,6 +90,13 @@ bool Controller::send() {
return true;
}
bool Controller::sendStop() {
if (!d_ptr)
return false;
return d_ptr->stop();
}
QuasarAppUtils::Help::Section Controller::help() const {
QuasarAppUtils::Help::Section help {
{QObject::tr("Options that available after start"), {

@ -77,6 +77,12 @@ public:
*/
bool send();
/**
* @brief sendStop This method send stop command to service;
* @return trie if command sendet successfull
*/
bool sendStop();
/**
* @brief help This method return help of the Controller.
* @return Available otions list.

@ -15,6 +15,8 @@
#include <pcommon.h>
#include "PController.h"
#include "serviceprivate.h"
#include <chrono>
#include <thread>
namespace Patronum {
@ -163,7 +165,7 @@ int ServiceBase::exec() {
bool fDaemon = QuasarAppUtils::Params::isEndable("daemon") || QuasarAppUtils::Params::isEndable("d");
if (QuasarAppUtils::Params::isEndable("install") || QuasarAppUtils::Params::isEndable("i")) {
if (!d_ptr->install(QuasarAppUtils::Params::getArg("install", "root")))
if (!d_ptr->install(QuasarAppUtils::Params::getArg("install")))
return Patronum::PatronumError::UnsupportedPlatform;
return 0;
}
@ -183,7 +185,13 @@ int ServiceBase::exec() {
}
QTimer::singleShot(0, nullptr, [this]() {
d_ptr->start();
controller()->sendStop();
std::this_thread::sleep_for (std::chrono::milliseconds(500));
if (!d_ptr->start()) {
QCoreApplication::exit(SocketIsBusy);
}
});
} else {
QTimer::singleShot(0, nullptr, [this]() {

@ -71,6 +71,10 @@ bool LocalSocket::listen() {
this, &LocalSocket::handleIncomming);
}
if (!m_server->removeServer(m_target)) {
return false;
}
if (!m_server->listen(m_target)) {
QuasarAppUtils::Params::log("listen is failed! " + m_server->errorString(),
QuasarAppUtils::Error);

@ -1,6 +1,7 @@
#include "pcommon.h"
#include "QCoreApplication"
#include <QFile>
#include <QStandardPaths>
namespace Patronum {
@ -16,6 +17,15 @@ QString PCommon::getPidfile() const {
return getPWD() + "/" + getServiceName() + ".pid";
}
qint64 PCommon::getPidFromPidfile() const {
QFile file = getPidfile();
if (!file.exists())
return 0;
return file.readAll().toLongLong();
}
QString PCommon::getPWD() const {
return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
}

@ -30,6 +30,12 @@ public:
*/
QString getPidfile() const;
/**
* @brief getPidFromPidfile This method return the pid from pidFile.
* @return pid number from pidFile.
*/
qint64 getPidFromPidfile() const;
/**
* @brief getPWD This method return PWD of the service.
* @return absalute path of the work directory.

@ -78,7 +78,7 @@ bool ServicePrivate::sendCloseConnection() {
return _socket->send(_parser->createPackage(Command::CloseConnection));
}
bool ServicePrivate::install(const QString& user) {
bool ServicePrivate::install(QString user) {
if (!_installer) {
QuasarAppUtils::Params::log(errorToString(UnsupportedPlatform),
@ -86,6 +86,9 @@ bool ServicePrivate::install(const QString& user) {
return false;
}
if (user.isEmpty())
user = DEFAULT_USER;
if (!_installer->install(getServiceLauncher(), user)) {
return false;
}
@ -113,7 +116,6 @@ bool ServicePrivate::start() {
if (!_socket->listen()) {
QuasarAppUtils::Params::log("Fail to create a terminal socket!",
QuasarAppUtils::Error);
QCoreApplication::exit(SocketIsBusy);
return false;
};
@ -129,6 +131,7 @@ bool ServicePrivate::start() {
}
bool ServicePrivate::startDeamon() {
if (_socket->isRunning()) {
QuasarAppUtils::Params::log("Failed to start a service because an another service alredy started",
QuasarAppUtils::Error);
@ -142,12 +145,20 @@ bool ServicePrivate::startDeamon() {
proc.setProcessEnvironment(QProcessEnvironment::systemEnvironment());
proc.setProcessChannelMode(QProcess::SeparateChannels);
if (!proc.startDetached()) {
proc.start();
if (!proc.waitForStarted(1000)) {
QuasarAppUtils::Params::log("fail to start detached process: " + proc.errorString(),
QuasarAppUtils::Error);
return false;
}
if (proc.waitForFinished(1000) && proc.exitCode()) {
QuasarAppUtils::Params::log("fail to start detached process: " + proc.errorString() +
" Exit Code: " + QString::number(proc.exitCode()),
QuasarAppUtils::Error);
return false;
}
QuasarAppUtils::Params::log("The service started successful", QuasarAppUtils::Info);
return true;

@ -51,7 +51,7 @@ public:
* @param user This is name of custom user that will be run your service after reboot system.
* @return return true if the service installed succesful
*/
bool install(const QString &user);
bool install(QString user);
/**
* @brief uninstall This method unistall your service in thec current system.