2023-07-10 10:18:33 +08:00
|
|
|
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
|
|
|
|
#ifndef JOBMANAGER1SERVICE_H
|
|
|
|
#define JOBMANAGER1SERVICE_H
|
|
|
|
|
|
|
|
#include <QObject>
|
2023-07-17 14:49:35 +08:00
|
|
|
#include <QMap>
|
2023-07-10 10:18:33 +08:00
|
|
|
#include <QDBusObjectPath>
|
2023-07-17 14:49:35 +08:00
|
|
|
#include <QSharedPointer>
|
2023-07-18 15:55:29 +08:00
|
|
|
#include <QMutex>
|
|
|
|
#include <QMutexLocker>
|
|
|
|
#include <QtConcurrent>
|
|
|
|
#include <QFuture>
|
|
|
|
#include <QUuid>
|
|
|
|
#include "jobadaptor.h"
|
|
|
|
#include "global.h"
|
|
|
|
|
2023-07-17 14:49:35 +08:00
|
|
|
class JobManager1Service final : public QObject
|
2023-07-10 10:18:33 +08:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
2023-07-17 14:49:35 +08:00
|
|
|
JobManager1Service();
|
|
|
|
JobManager1Service(const JobManager1Service &) = delete;
|
2023-07-21 14:47:40 +08:00
|
|
|
JobManager1Service(JobManager1Service &&) = delete;
|
2023-07-17 14:49:35 +08:00
|
|
|
JobManager1Service &operator=(const JobManager1Service &) = delete;
|
|
|
|
JobManager1Service &operator=(JobManager1Service &&) = delete;
|
|
|
|
|
|
|
|
~JobManager1Service() override;
|
2023-07-18 15:55:29 +08:00
|
|
|
template <typename F>
|
|
|
|
void addJob(const QDBusObjectPath &source, F func, QVariantList args)
|
|
|
|
{
|
|
|
|
static_assert(std::is_invocable_v<F, QVariant>, "param type must be QVariant.");
|
|
|
|
|
2023-07-19 17:56:45 +08:00
|
|
|
QString objectPath{DDEApplicationManager1JobObjectPath + QUuid::createUuid().toString(QUuid::Id128)};
|
2023-07-21 14:47:40 +08:00
|
|
|
auto future = QtConcurrent::mappedReduced(args.begin(),
|
|
|
|
args.end(),
|
|
|
|
func,
|
|
|
|
qOverload<QVariantList::parameter_type>(&QVariantList::append),
|
|
|
|
QVariantList{},
|
|
|
|
QtConcurrent::ReduceOption::OrderedReduce);
|
2023-07-18 15:55:29 +08:00
|
|
|
QSharedPointer<JobService> job{new JobService{future}};
|
|
|
|
auto path = QDBusObjectPath{objectPath};
|
|
|
|
{
|
|
|
|
QMutexLocker locker{&m_mutex};
|
|
|
|
m_jobs.insert(path, job); // Insertion is always successful
|
|
|
|
}
|
|
|
|
emit JobNew(path, source);
|
2023-07-19 17:56:45 +08:00
|
|
|
registerObjectToDbus(new JobAdaptor(job.data()), objectPath, getDBusInterface<JobAdaptor>());
|
2023-07-21 14:47:40 +08:00
|
|
|
auto emitRemove = [this, job, path, future](QVariantList value) {
|
2023-07-18 15:55:29 +08:00
|
|
|
decltype(m_jobs)::size_type removeCount{0};
|
|
|
|
{
|
|
|
|
QMutexLocker locker{&m_mutex};
|
|
|
|
removeCount = m_jobs.remove(path);
|
|
|
|
}
|
|
|
|
// removeCount means m_jobs can't find value which corresponding with path
|
|
|
|
// and we shouldn't emit jobRemoved signal because this signal may already has been emit
|
2023-07-19 17:56:45 +08:00
|
|
|
if (removeCount == 0) {
|
2023-07-18 15:55:29 +08:00
|
|
|
qCritical() << "Job already has been removed: " << path.path();
|
2023-07-19 17:56:45 +08:00
|
|
|
return value;
|
2023-07-18 15:55:29 +08:00
|
|
|
}
|
|
|
|
QString result{job->status()};
|
|
|
|
for (const auto &val : future.result()) {
|
2023-07-19 17:56:45 +08:00
|
|
|
if (val.template canConvert<QDBusError>()) {
|
2023-07-18 15:55:29 +08:00
|
|
|
result = "failed";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
emit this->JobRemoved(path, result, future.result());
|
2023-07-19 17:56:45 +08:00
|
|
|
return value;
|
2023-07-18 15:55:29 +08:00
|
|
|
};
|
2023-07-19 17:56:45 +08:00
|
|
|
future.then(emitRemove);
|
2023-07-18 15:55:29 +08:00
|
|
|
}
|
2023-07-10 10:18:33 +08:00
|
|
|
|
|
|
|
Q_SIGNALS:
|
|
|
|
void JobNew(const QDBusObjectPath &job, const QDBusObjectPath &source);
|
2023-07-18 15:55:29 +08:00
|
|
|
void JobRemoved(const QDBusObjectPath &job, const QString &status, const QVariantList &result);
|
2023-07-17 14:49:35 +08:00
|
|
|
|
|
|
|
private:
|
2023-07-18 15:55:29 +08:00
|
|
|
QMutex m_mutex;
|
2023-07-17 14:49:35 +08:00
|
|
|
QMap<QDBusObjectPath, QSharedPointer<JobService>> m_jobs;
|
2023-07-10 10:18:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|