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

View File

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

View File

@ -77,6 +77,12 @@ public:
*/ */
bool send(); 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. * @brief help This method return help of the Controller.
* @return Available otions list. * @return Available otions list.

View File

@ -15,6 +15,8 @@
#include <pcommon.h> #include <pcommon.h>
#include "PController.h" #include "PController.h"
#include "serviceprivate.h" #include "serviceprivate.h"
#include <chrono>
#include <thread>
namespace Patronum { namespace Patronum {
@ -163,7 +165,7 @@ int ServiceBase::exec() {
bool fDaemon = QuasarAppUtils::Params::isEndable("daemon") || QuasarAppUtils::Params::isEndable("d"); bool fDaemon = QuasarAppUtils::Params::isEndable("daemon") || QuasarAppUtils::Params::isEndable("d");
if (QuasarAppUtils::Params::isEndable("install") || QuasarAppUtils::Params::isEndable("i")) { 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 Patronum::PatronumError::UnsupportedPlatform;
return 0; return 0;
} }
@ -183,7 +185,13 @@ int ServiceBase::exec() {
} }
QTimer::singleShot(0, nullptr, [this]() { 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 { } else {
QTimer::singleShot(0, nullptr, [this]() { QTimer::singleShot(0, nullptr, [this]() {

View File

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

View File

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

View File

@ -30,6 +30,12 @@ public:
*/ */
QString getPidfile() const; 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. * @brief getPWD This method return PWD of the service.
* @return absalute path of the work directory. * @return absalute path of the work directory.

View File

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

View File

@ -51,7 +51,7 @@ public:
* @param user This is name of custom user that will be run your service after reboot system. * @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 * @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. * @brief uninstall This method unistall your service in thec current system.