Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qprocessscheduler.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include <QtQmlDom/private/qqmldom_utils_p.h>
6
8
9namespace QmlLsp {
10
11Q_STATIC_LOGGING_CATEGORY(schedulerLog, "qt.languageserver.qprocessscheduler")
12
13using namespace Qt::StringLiterals;
14
15/*!
16 \internal
17 \class QProcessScheduler
18
19 \brief Runs multiple processes sequentially via a QProcess, and signals once they are done.
20
21 QProcessScheduler runs multiple programs sequentially through a QProcess, and emits the \c done
22 signal once all programs finished. Use schedule(programs, id) to add new programs to be run. The
23 id is used to signal their completion. Duplicate programs that already are in the queue are not
24 re-added to the queue.
25 */
27{
28 QObject::connect(&m_process, &QProcess::finished, this, &QProcessScheduler::processNext);
29 QObject::connect(&m_process, &QProcess::errorOccurred, this,
30 &QProcessScheduler::onErrorOccurred);
31}
33{
34 QObject::disconnect(&m_process, nullptr);
35 m_process.kill();
36 m_process.waitForFinished();
37}
38
39void QProcessScheduler::schedule(const QList<Command> &list, const Id &id)
40{
41 for (const auto &x : list) {
42 const QueueElement queueElement{ x };
43 if (!m_queue.contains(queueElement))
44 m_queue.enqueue(queueElement);
45 }
46 m_queue.enqueue(id);
47 if (!m_isRunning)
48 processNext();
49}
50
51void QProcessScheduler::processNext()
52{
53 m_isRunning = m_queue.size() > 0;
54 if (!m_isRunning)
55 return;
56
57 std::visit(qOverloadedVisitor{ [this](const Id &id) {
58 emit done(id);
59 processNext();
60 },
61 [this](const Command &command) {
62 m_process.setProgram(command.program);
63 m_process.setArguments(command.arguments);
64 m_process.setProcessEnvironment(command.customEnvironment);
65 m_process.start();
66 }
67
68 },
69 m_queue.dequeue());
70}
71
72void QProcessScheduler::onErrorOccurred(QProcess::ProcessError error)
73{
74 qCDebug(schedulerLog) << "Process" << m_process.program() << m_process.arguments().join(" "_L1)
75 << "had an error:" << error;
76 processNext();
77}
78
79} // namespace QmlLsp
80
81QT_END_NAMESPACE
Runs multiple processes sequentially via a QProcess, and signals once they are done.