Akonadi

processcontrol.h
1 /***************************************************************************
2  * SPDX-FileCopyrightText: 2006 Tobias Koenig <[email protected]> *
3  * *
4  * SPDX-License-Identifier: LGPL-2.0-or-later *
5  ***************************************************************************/
6 
7 #pragma once
8 
9 #include <QObject>
10 #include <QProcess>
11 
12 #include <chrono>
13 
14 namespace Akonadi
15 {
16 /**
17  * This class starts and observes a process. Depending on the
18  * policy it also restarts the process when it crashes.
19  */
20 class ProcessControl : public QObject
21 {
22  Q_OBJECT
23 
24 public:
25  /**
26  * These enums describe the behaviour when the observed
27  * application crashed.
28  *
29  * @li StopOnCrash - The application won't be restarted.
30  * @li RestartOnCrash - The application is restarted with the same arguments.
31  */
32  enum CrashPolicy {
33  StopOnCrash,
34  RestartOnCrash,
35  };
36 
37  /**
38  * Creates a new process control.
39  *
40  * @param parent The parent object.
41  */
42  explicit ProcessControl(QObject *parent = nullptr);
43 
44  /**
45  * Destroys the process control.
46  */
47  ~ProcessControl() override;
48 
49  /**
50  * Starts the @p application with the given list of @p arguments.
51  */
52  void start(const QString &application, const QStringList &arguments = QStringList(), CrashPolicy policy = RestartOnCrash);
53 
54  /**
55  * Starts the process with the previously set application and arguments.
56  */
57  void start();
58 
59  /**
60  * Stops the currently running application.
61  */
62  void stop();
63 
64  /**
65  * Sets the crash policy.
66  */
67  void setCrashPolicy(CrashPolicy policy);
68 
69  /**
70  * Restart the application the next time it exits normally.
71  */
73  {
74  mRestartOnceOnExit = true;
75  }
76 
77  /**
78  * Returns true if the process is currently running.
79  */
80  Q_REQUIRED_RESULT bool isRunning() const;
81 
82  /**
83  * Sets the time (in msecs) we wait for the process to shut down before we send terminate/kill signals.
84  * Default is 1 second.
85  * Note that it is your responsiblility to ask the process to quit, otherwise this is just
86  * pointless waiting.
87  */
88  void setShutdownTimeout(std::chrono::milliseconds timeout);
89 
90 Q_SIGNALS:
91  /**
92  * This signal is emitted whenever the observed application
93  * writes something to stderr.
94  *
95  * @param errorMsg The error output of the observed application.
96  */
97  void processErrorMessages(const QString &errorMsg);
98 
99  /**
100  * This signal is emitted when the server is restarted after a crash.
101  */
102  void restarted();
103 
104  /**
105  * Emitted if the process could not be started since it terminated
106  * too often.
107  */
108  void unableToStart();
109 
110 private Q_SLOTS:
111  void slotError(QProcess::ProcessError);
112  void slotFinished(int, QProcess::ExitStatus);
113  void resetCrashCount();
114 
115 private:
116  QProcess mProcess;
117  QString mApplication;
118  QStringList mArguments;
119  CrashPolicy mPolicy = RestartOnCrash;
120  bool mFailedToStart = false;
121  int mCrashCount = 0;
122  bool mRestartOnceOnExit = false;
123  std::chrono::milliseconds mShutdownTimeout;
124 };
125 
126 }
127 
Q_OBJECTQ_OBJECT
void start()
Starts the process with the previously set application and arguments.
void processErrorMessages(const QString &errorMsg)
This signal is emitted whenever the observed application writes something to stderr.
Q_SLOTSQ_SLOTS
CrashPolicy
These enums describe the behaviour when the observed application crashed.
ProcessControl(QObject *parent=nullptr)
Creates a new process control.
void unableToStart()
Emitted if the process could not be started since it terminated too often.
void setShutdownTimeout(std::chrono::milliseconds timeout)
Sets the time (in msecs) we wait for the process to shut down before we send terminate/kill signals.
bool isRunning() const
Returns true if the process is currently running.
Q_SIGNALSQ_SIGNALS
~ProcessControl() override
Destroys the process control.
void restartOnceWhenFinished()
Restart the application the next time it exits normally.
void stop()
Stops the currently running application.
void restarted()
This signal is emitted when the server is restarted after a crash.
void setCrashPolicy(CrashPolicy policy)
Sets the crash policy.
QObject * parent() const const
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jun 30 2022 03:51:46 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.