9#include <QCoreApplication>
11#include <QLibraryInfo>
13#include <QStandardPaths>
17#include "serviceprivate.h"
25 QuasarAppUtils::Params::parseParams(argc, argv);
26 d_ptr =
new ServicePrivate(
this);
35 qint64 pid = PCommon::instance()->getPidFromPidfile();
36 if (pid == QCoreApplication::applicationPid()) {
37 QFile pidFile(PCommon::instance()->getPidfile());
55 for (
const auto& i: data) {
56 if (list.contains(i)) {
58 sendResuylt(QString(
"The process of a command %0 with argumets: %1 is failed")
59 .arg(i.cmd(), i.arg()));
62 commandList += i.toString() +
" ";
66 if (commandList.size()) {
69 for (
const auto&i : list) {
70 stringList += i.toString() +
" ";
75 result[
"Error"] =
"Wrong command! The commands : " + commandList +
" is not supported";
76 result[
"Available commands"] = stringList;
87 return d_ptr->sendCmdResult(result);
91 return d_ptr->sendCmdResult({{
"Result", result}});
95 return d_ptr->sendCmdResult({{
"", result}});
101 QTimer::singleShot(1000,
nullptr, [](){
102 QCoreApplication::quit();
123void ServiceBase::printDefaultHelp() {
125 serviceHelp.unite(QuasarAppUtils::Params::getHelp());
127 serviceHelp.unite({{QObject::tr(
"Options that available befor install"),{
128 {
"start / s", QObject::tr(
"Start a service in console")},
129 {
"daemon / d", QObject::tr(
"Start a service as a daemon")},
130 {
"install / i / -install username", QObject::tr(
"Install a service. Add user name if you want to run service from custom user. Example: -install username")},
131 {
"unistall / u", QObject::tr(
"unistall a service")},
135 QuasarAppUtils::Help::Options optionsList;
136 for (
const auto& cmd : features) {
137 optionsList.insert(cmd.cmd(), cmd.description());
140 serviceHelp.unite({{
"Available commands of the service:", optionsList}});
142 QuasarAppUtils::Help::print(serviceHelp);
163 bool printHelp = !QuasarAppUtils::Params::size() ||
164 QuasarAppUtils::Params::isEndable(
"h") ||
165 QuasarAppUtils::Params::isEndable(
"help");
172 bool fStart = QuasarAppUtils::Params::isEndable(
"start") || QuasarAppUtils::Params::isEndable(
"s");
173 bool fDaemon = QuasarAppUtils::Params::isEndable(
"daemon") || QuasarAppUtils::Params::isEndable(
"d");
175 if (QuasarAppUtils::Params::isEndable(
"install") || QuasarAppUtils::Params::isEndable(
"i")) {
176 if (!d_ptr->install(QuasarAppUtils::Params::getArg(
"install")))
181 if (QuasarAppUtils::Params::isEndable(
"uninstall") || QuasarAppUtils::Params::isEndable(
"u")) {
182 if (!d_ptr->uninstall())
187 if (fStart || fDaemon) {
190 if (!d_ptr->startDeamon())
195 QTimer::singleShot(0,
nullptr, [
this]() {
197 std::this_thread::sleep_for (std::chrono::milliseconds(1000));
200 if (!d_ptr->start()) {
206 QTimer::singleShot(0,
nullptr, [
this]() {
213 return core()->exec();
The Controller class provide control functionality for your service.
QuasarAppUtils::Help::Section help() const
help This method return help of the Controller.
virtual bool handleReceive(const Feature &data)=0
handleReceive This method invoked when service receive a request from terminal. Override this method ...
void setCore(QCoreApplication *core)
setCore This method sets new object of the core application.
void handleReceiveData(const QHash< QString, Feature > &data) override
handleReceiveData - This method invoice when service receive new command from terminal of controller ...
virtual int exec()
exec This is main method of the service. Use this like a QCoreApplication::exec.
void onPause() override
onPause Called when get pause command from terminal. The Default implementation do nothing.
void onResume() override
onResume Called when get resume command from terminal. The Default implementation do nothing.
bool sendRawResuylt(const QByteArray &result)
sendRawResuylt This method send raw text responce to controller.
virtual void createApplication()=0
createApplication Default implementation create a Application object and parse arguments.
bool sendResuylt(const QVariantMap &result)
sendResuylt - Call this method for send responce from service to tour controller.
QSet< Feature > supportedFeatures() override
supportedFeatures
void onStop() override
onStop Called when get stop command from terminal. The default implementation of this method invoke a...
Controller * controller()
controller This method return the local controller object. If the controller object is not inited the...
QCoreApplication * core()
core This method return a pointer to instance of the core application.
ServiceBase(int argc, char *argv[])
ServiceBase This is main constructor of the service.
The Patronum namespace - It is main name space of Patronum Library. The Patronum library support the ...
@ FailedToStart
This error ocured when user srvice failed to start.
@ ServiceUnavailable
Service is unavailable. Try send start command or restart the service manually.
@ UnsupportedPlatform
This error ocured when service not supportded using platform.