mirror of
https://github.com/QuasarApp/Patronum.git
synced 2025-04-26 23:54:32 +00:00
fix start service
This commit is contained in:
parent
ec293ae4d6
commit
db61f01376
@ -49,8 +49,9 @@ public:
|
||||
|
||||
/**
|
||||
* @brief onStart This method invoked when service is started successful.
|
||||
* @return true if service started successful
|
||||
*/
|
||||
virtual void onStart() = 0;
|
||||
virtual bool onStart() = 0;
|
||||
|
||||
/**
|
||||
* @brief onStop This method invoked when service receive stop command from the terminal.
|
||||
|
@ -94,6 +94,10 @@ bool Controller::sendStop() {
|
||||
if (!d_ptr)
|
||||
return false;
|
||||
|
||||
if (!d_ptr->connectToHost(false)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return d_ptr->stop();
|
||||
}
|
||||
|
||||
|
@ -179,17 +179,22 @@ int ServiceBase::exec() {
|
||||
if (fStart || fDaemon) {
|
||||
|
||||
if (fDaemon) {
|
||||
if (controller()->sendStop()) {
|
||||
std::this_thread::sleep_for (std::chrono::milliseconds(500));
|
||||
}
|
||||
|
||||
if (!d_ptr->startDeamon())
|
||||
return Patronum::PatronumError::UnsupportedPlatform;
|
||||
return 0;
|
||||
}
|
||||
|
||||
QTimer::singleShot(0, nullptr, [this]() {
|
||||
controller()->sendStop();
|
||||
std::this_thread::sleep_for (std::chrono::milliseconds(500));
|
||||
if (controller()->sendStop()) {
|
||||
std::this_thread::sleep_for (std::chrono::milliseconds(500));
|
||||
}
|
||||
|
||||
if (!d_ptr->start()) {
|
||||
QCoreApplication::exit(SocketIsBusy);
|
||||
QCoreApplication::exit(FailedToStart);
|
||||
}
|
||||
|
||||
});
|
||||
|
@ -77,7 +77,7 @@ protected:
|
||||
/**
|
||||
* @brief onStart Called when get start command from terminal. Override this method work correctly work of service.
|
||||
*/
|
||||
void onStart() override = 0;
|
||||
bool onStart() override = 0;
|
||||
|
||||
/**
|
||||
* @brief onStop Called when get stop command from terminal. The default implementation of this method invoke a quit method of QCoreApplication.
|
||||
|
@ -29,8 +29,8 @@ QString errorToString(PatronumError error) {
|
||||
case Patronum::PatronumError::TimeOutError:
|
||||
return QObject::tr("Timeout error. service unavailable or not started.");
|
||||
|
||||
case Patronum::PatronumError::SocketIsBusy:
|
||||
return QObject::tr("The Socket file alredy created or application do not have a permision to the /var/tmp location.");
|
||||
case Patronum::PatronumError::FailedToStart:
|
||||
return QObject::tr("Failed to start service");
|
||||
|
||||
case Patronum::PatronumError::UnsupportedPlatform:
|
||||
return QObject::tr("The service not supportded using platform.");
|
||||
|
@ -84,15 +84,18 @@ bool ControllerPrivate::isConnected() const {
|
||||
return _socket->isValid();
|
||||
}
|
||||
|
||||
bool ControllerPrivate::connectToHost() const {
|
||||
bool ControllerPrivate::connectToHost(bool echo) const {
|
||||
if (isConnected()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!_socket->connectToTarget()) {
|
||||
QuasarAppUtils::Params::log("Connect to service fail !",
|
||||
QuasarAppUtils::Debug);
|
||||
_controller->handleError(PatronumError::ServiceUnavailable);
|
||||
|
||||
if (echo) {
|
||||
QuasarAppUtils::Params::log("Connect to service fail !",
|
||||
QuasarAppUtils::Debug);
|
||||
_controller->handleError(PatronumError::ServiceUnavailable);
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
|
@ -30,7 +30,7 @@ public:
|
||||
|
||||
QList<Feature> features() const;
|
||||
bool isConnected() const;
|
||||
bool connectToHost() const;
|
||||
bool connectToHost(bool echo = true) const;
|
||||
|
||||
signals:
|
||||
void sigListFeatures(QList<Feature>);
|
||||
|
@ -120,15 +120,7 @@ bool ServicePrivate::start() {
|
||||
return false;
|
||||
};
|
||||
|
||||
QFile pidFile(PCommon::instance()->getPidfile());
|
||||
if (pidFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
||||
pidFile.write(QByteArray::number(QCoreApplication::applicationPid()));
|
||||
pidFile.close();
|
||||
}
|
||||
|
||||
_service->onStart();
|
||||
|
||||
return true;
|
||||
return _service->onStart();
|
||||
}
|
||||
|
||||
bool ServicePrivate::startDeamon() {
|
||||
@ -146,12 +138,21 @@ bool ServicePrivate::startDeamon() {
|
||||
proc.setProcessEnvironment(QProcessEnvironment::systemEnvironment());
|
||||
proc.setProcessChannelMode(QProcess::SeparateChannels);
|
||||
|
||||
if (!proc.startDetached()) {
|
||||
qint64 pid;
|
||||
if (!proc.startDetached(&pid)) {
|
||||
QuasarAppUtils::Params::log("fail to start detached process: " + proc.errorString(),
|
||||
QuasarAppUtils::Error);
|
||||
return false;
|
||||
}
|
||||
|
||||
QFile pidFile(PCommon::instance()->getPidfile());
|
||||
if (!pidFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
pidFile.write(QByteArray::number(pid));
|
||||
pidFile.close();
|
||||
|
||||
QuasarAppUtils::Params::log("The service started successful", QuasarAppUtils::Info);
|
||||
|
||||
return true;
|
||||
|
@ -12,8 +12,9 @@ DefaultService::DefaultService():
|
||||
setCore(new QCoreApplication(argc, arg));
|
||||
}
|
||||
|
||||
void DefaultService::onStart() {
|
||||
bool DefaultService::onStart() {
|
||||
QuasarAppUtils::Params::log("Server started!", QuasarAppUtils::Info);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DefaultService::handleReceive(const Patronum::Feature &data) {
|
||||
|
@ -10,7 +10,7 @@ public:
|
||||
|
||||
// QtServiceBase interface
|
||||
protected:
|
||||
void onStart() override;
|
||||
bool onStart() override;
|
||||
|
||||
// IService interface
|
||||
public:
|
||||
|
Loading…
x
Reference in New Issue
Block a user