Akonadi

processcontrol.h
1/***************************************************************************
2 * SPDX-FileCopyrightText: 2006 Tobias Koenig <tokoe@kde.org> *
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
14namespace 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 */
20class ProcessControl : public QObject
21{
23
24public:
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 */
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 [[nodiscard]] 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
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 */
109
110private Q_SLOTS:
111 void slotError(QProcess::ProcessError);
112 void slotFinished(int, QProcess::ExitStatus);
113 void resetCrashCount();
114
115private:
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}
This class starts and observes a process.
~ProcessControl() override
Destroys the process control.
void setCrashPolicy(CrashPolicy policy)
Sets the crash policy.
void processErrorMessages(const QString &errorMsg)
This signal is emitted whenever the observed application writes something to stderr.
void start()
Starts the process with the previously set application and arguments.
void stop()
Stops the currently running application.
void unableToStart()
Emitted if the process could not be started since it terminated too often.
bool isRunning() const
Returns true if the process is currently running.
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.
ProcessControl(QObject *parent=nullptr)
Creates a new process control.
CrashPolicy
These enums describe the behaviour when the observed application crashed.
void restarted()
This signal is emitted when the server is restarted after a crash.
void restartOnceWhenFinished()
Restart the application the next time it exits normally.
Helper integration between Akonadi and Qt.
Q_OBJECTQ_OBJECT
Q_SIGNALSQ_SIGNALS
Q_SLOTSQ_SLOTS
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:00:59 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.