Search code examples
c++qtservice

Qt Service does not enter the event loop issue


I used the old QtService code from: https://github.com/qtproject/qt-solutions/tree/master/qtservice and created a service to run npm start command which will start my website on Windows startup. When adding -exec argument, starting in app mode it works well but when it runs as a service it does nothing. During a debugging I found out that it does not entering the service event loop correctly. So, I created a small example which should write some text to a txt file to illustrate this issue.

Code:

insuranceservice.h

#ifndef INSURANCESERVICE_H
#define INSURANCESERVICE_H

#include <QCoreApplication>
#include <QObject>
#include <QThread>
#include <QDebug>
#include "qtservice.h"
#include "servicelogger.h"

class InsuranceService : public QtService<QCoreApplication>
{
    Q_GADGET
public:
    InsuranceService(int argc, char *argv[]);
    ~InsuranceService();

private:
    void start();
    void stop();
    void pause();
    void resume();
    void processCommand(int code);
    void createApplication(int &argc, char *argv[]);
};

#endif // INSURANCESERVICE_H

insuranceservice.cpp

#include "insuranceservice.h"

InsuranceService::InsuranceService(int argc, char *argv[]) : QtService<QCoreApplication>(argc, argv, "InsuranceService")
{
    std::cout << serviceName().toStdString().c_str() << std::endl;
    setServiceDescription("Test service");
    setServiceFlags(QtServiceBase::CanBeSuspended);
    //setStartupType(QtServiceController::AutoStartup);
}

void InsuranceService::start()
{
    try {
        QCoreApplication *insuranceApp = application();
        QFile *logFile = new QFile(ServiceLogger::getLogPath(), insuranceApp); 
        logFile->open(QIODevice::WriteOnly | QIODevice::Append);
        ServiceLogger::writeLog(logFile, QString("Test...").toUtf8());
        logFile->close();
        logFile->deleteLater();
    } catch (...) {
        qCritical() << "Failed to start the service!!!";
    }
}

void InsuranceService::stop()
{

}

void InsuranceService::pause()
{

}

void InsuranceService::resume()
{

}

void InsuranceService::processCommand(int code)
{
    Q_UNUSED(code);
}

void InsuranceService::createApplication(int &argc, char *argv[])
{
    QtService::createApplication(argc, argv);
}

InsuranceService::~InsuranceService()
{

}

main.cpp

#include <QCoreApplication>
#include "insuranceservice.h"

int main(int argc, char *argv[])
{
    InsuranceService service(argc, argv);
    return service.exec();
}

Any ideas how to enter the service event loop correctly?

Updated:

I tried to run this service with the built-in logMessage method:

void InsuranceService::start()
{
    logMessage("The start method executing....", QtServiceBase::Success, 0, 0);
    QFile *logFile = new QFile(ServiceLogger::getLogPath());
    logFile->open(QIODevice::WriteOnly | QIODevice::Append);
    ServiceLogger::writeLog(logFile, QString("Test...").toUtf8());
    logFile->close();
    logFile->deleteLater();
}

In the Event Viewer it reports: The start method executing....

But for some reason everything below logMessage() does not execute. The log file does not exists which should be created. It could be the issue that logMessage() runs on one instance and QFile creates/runs on another instance. I need somehow to connect to the right instance.

Thank you.


Solution

  • Ok. I have properly debug the app and found out that QProcess, QStandardPaths::writableLocation and qgetenv are not working when app is a service. It seems that service functionality is limited. So, I decided to set my app to run at OS startup instead of a service. Now, it works well. The issue is resolved. Thanks.