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