cutelyst  4.4.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
unixfork.h
1 /*
2  * SPDX-FileCopyrightText: (C) 2016-2017 Daniel Nicoletti <dantti12@gmail.com>
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 #ifndef UNIXFORK_H
6 #define UNIXFORK_H
7 
8 #include "abstractfork.h"
9 
10 #include <QHash>
11 #include <QObject>
12 #include <QVector>
13 
14 struct Worker {
15  bool null = true;
16  int id;
17  int restart = 0;
18  int respawn = 0;
19 };
20 
21 namespace Cutelyst {
22 class Server;
23 }
24 
25 class QTimer;
26 class QSocketNotifier;
27 class UnixFork final : public AbstractFork
28 {
29  Q_OBJECT
30 public:
31  explicit UnixFork(int process, int threads, bool setupSignals, QObject *parent = nullptr);
32  ~UnixFork() override;
33 
34  virtual bool continueMaster(int *exit = nullptr) override;
35 
36  virtual int exec(bool lazy, bool master) override;
37 
38  virtual void restart() override;
39 
40  int internalExec();
41 
42  bool createProcess(bool respawn);
43  void decreaseWorkerRespawn();
44 
45  virtual void killChild() override;
46  void killChild(qint64 pid);
47 
48  virtual void terminateChild() override;
49  void terminateChild(qint64 pid);
50 
51  static void stopWSGI(const QString &pidfile);
52 
53  static bool setUmask(const QByteArray &valueStr);
54  static bool setGidUid(const QString &gid, const QString &uid, bool noInitgroups);
55  static void chownSocket(const QString &filename, const QString &uidGid);
56 
57  static int idealProcessCount();
58  static int idealThreadCount();
59 
60  void handleSigHup();
61  void handleSigTerm();
62  void handleSigInt();
63  void handleSigChld();
64 
65  static void setSched(Cutelyst::Server *wsgi, int workerId, int workerCore);
66 
67 private:
68  int setupUnixSignalHandlers();
69  void setupSocketPair(bool closeSignalsFD, bool createPair);
70  bool createChild(const Worker &worker, bool respawn);
71  static void signalHandler(int signal);
72  void setupCheckChildTimer();
73  void postFork(int workerId);
74 
75  QHash<qint64, Worker> m_childs;
76  QVector<Worker> m_recreateWorker;
77  QSocketNotifier *m_signalNotifier = nullptr;
78  QTimer *m_checkChildRestart = nullptr;
79  int m_threads;
80  int m_processes;
81  bool m_child = false;
82  bool m_terminating = false;
83 };
84 
85 #endif // UNIXFORK_H
Implements a web server.
Definition: server.h:60
virtual void terminateChild() override
Definition: unixfork.cpp:186
virtual bool continueMaster(int *exit=nullptr) override
Definition: unixfork.cpp:66
virtual void restart() override
Definition: unixfork.cpp:99
virtual int exec(bool lazy, bool master) override
Definition: unixfork.cpp:72
virtual void killChild() override
Definition: unixfork.cpp:172
The Cutelyst namespace holds all public Cutelyst API.
Q_OBJECTQ_OBJECT
QObject * parent() const const