cutelyst 4.3.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
windowsfork.cpp
1/*
2 * SPDX-FileCopyrightText: (C) 2017-2018 Daniel Nicoletti <dantti12@gmail.com>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5#include "windowsfork.h"
6
7#include <windows.h>
8
9#include <QCoreApplication>
10#include <QLoggingCategory>
11#include <QTimer>
12
13Q_LOGGING_CATEGORY(C_SERVER_WIN, "cutelyst.server.windows", QtWarningMsg)
14
15WindowsFork::WindowsFork(QObject *parent)
16 : AbstractFork(parent)
17{
18}
19
21{
22 installTouchReload();
23
24 m_masterChildProcess = new QProcess(this);
25 connect(m_masterChildProcess,
26 static_cast<void (QProcess::*)(int, QProcess::ExitStatus)>(&QProcess::finished),
27 this,
28 &WindowsFork::childFinished);
29
31 env.insert(QStringLiteral("CUTELYST_WSGI_IGNORE_MASTER"), QStringLiteral("1"));
32 m_masterChildProcess->setProcessEnvironment(env);
33
35
37
38 m_masterChildProcess->setArguments(QCoreApplication::arguments());
39
40 startChild();
41
42 int ret = qApp->exec();
43 if (exit) {
44 *exit = ret;
45 }
46 return false;
47}
48
49int WindowsFork::exec(bool lazy, bool master)
50{
51 Q_UNUSED(lazy)
52 Q_UNUSED(master)
53
54 Q_EMIT forked(0);
55 return qApp->exec();
56}
57
59{
60 if (m_masterChildProcess) {
61 m_masterChildProcess->kill();
62 }
63}
64
66{
67 if (m_masterChildProcess) {
68 m_masterChildProcess->terminate();
69 }
70}
71
73{
74 restartTerminate();
75
76 if (!m_materChildRestartTimer) {
77 m_materChildRestartTimer = new QTimer(this);
78 m_materChildRestartTimer->setInterval(1 * 1000);
79 m_materChildRestartTimer->setSingleShot(false);
80
81 connect(m_materChildRestartTimer, &QTimer::timeout, this, &WindowsFork::restartTerminate);
82 }
83 m_materChildRestartTimer->start();
84}
85
86void WindowsFork::startChild()
87{
88 m_masterChildProcess->start();
89}
90
91void WindowsFork::childFinished(int exitCode, QProcess::ExitStatus exitStatus)
92{
93 qCDebug(C_SERVER_WIN) << "Master child finished" << exitCode << exitStatus;
94 if (m_materChildRestartTimer || exitStatus == QProcess::CrashExit) {
95 startChild();
96 } else {
97 qApp->exit(exitCode);
98 }
99}
100
101void WindowsFork::restartTerminate()
102{
103 if (++m_autoReloadCount > 5) {
104 killChild();
105 } else {
107 }
108}
virtual void restart() override
virtual void terminateChild() override
virtual int exec(bool lazy, bool master) override
virtual bool continueMaster(int *exit=nullptr) override
virtual void killChild() override
QString applicationFilePath()
QStringList arguments()
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void finished(int exitCode, QProcess::ExitStatus exitStatus)
void kill()
void setArguments(const QStringList &arguments)
void setProcessChannelMode(QProcess::ProcessChannelMode mode)
void setProcessEnvironment(const QProcessEnvironment &environment)
void setProgram(const QString &program)
void start(QIODeviceBase::OpenMode mode)
void terminate()
QProcessEnvironment systemEnvironment()
void setInterval(int msec)
void setSingleShot(bool singleShot)
void start()
void timeout()