2021-08-30 22:17:59 +01:00
|
|
|
#include "InsightWorker.hpp"
|
|
|
|
|
|
2021-04-04 21:04:12 +01:00
|
|
|
#include <QObject>
|
2022-04-27 21:27:59 +01:00
|
|
|
|
2022-09-07 22:25:28 +01:00
|
|
|
#include "src/Insight/InsightSignals.hpp"
|
2021-08-30 22:17:59 +01:00
|
|
|
#include "src/Logger/Logger.hpp"
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
namespace Bloom
|
|
|
|
|
{
|
|
|
|
|
using namespace Bloom::Exceptions;
|
2021-05-24 20:58:49 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
using Bloom::Targets::TargetState;
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-02-05 15:32:08 +00:00
|
|
|
void InsightWorker::startup() {
|
2022-09-08 19:54:37 +01:00
|
|
|
auto* insightSignals = InsightSignals::instance();
|
|
|
|
|
|
2022-09-17 20:18:03 +01:00
|
|
|
QObject::connect(
|
|
|
|
|
insightSignals,
|
|
|
|
|
&InsightSignals::taskQueued,
|
|
|
|
|
this,
|
|
|
|
|
&InsightWorker::executeTasks,
|
|
|
|
|
Qt::ConnectionType::QueuedConnection
|
|
|
|
|
);
|
|
|
|
|
QObject::connect(
|
|
|
|
|
insightSignals,
|
|
|
|
|
&InsightSignals::taskProcessed,
|
|
|
|
|
this,
|
|
|
|
|
&InsightWorker::executeTasks,
|
|
|
|
|
Qt::ConnectionType::QueuedConnection
|
|
|
|
|
);
|
2021-10-18 01:03:22 +01:00
|
|
|
|
2022-09-14 19:46:16 +01:00
|
|
|
Logger::debug("InsightWorker" + std::to_string(this->id) + " ready");
|
2022-02-05 15:32:08 +00:00
|
|
|
emit this->ready();
|
|
|
|
|
}
|
2021-08-30 22:17:59 +01:00
|
|
|
|
2022-09-07 22:25:28 +01:00
|
|
|
void InsightWorker::queueTask(InsightWorkerTask* task) {
|
2022-09-08 19:54:37 +01:00
|
|
|
static std::atomic<QueuedTaskId> lastQueuedTaskId = 0;
|
2022-09-07 22:25:28 +01:00
|
|
|
task->moveToThread(nullptr);
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-09-07 22:25:28 +01:00
|
|
|
{
|
2022-09-08 19:54:37 +01:00
|
|
|
const auto taskQueueLock = InsightWorker::queuedTasksById.acquireLock();
|
|
|
|
|
InsightWorker::queuedTasksById.getValue().insert(std::pair(++(lastQueuedTaskId), task));
|
2022-04-23 17:30:14 +01:00
|
|
|
}
|
|
|
|
|
|
2022-09-07 22:25:28 +01:00
|
|
|
emit InsightSignals::instance()->taskQueued();
|
2022-02-05 15:32:08 +00:00
|
|
|
}
|
2021-05-30 16:53:24 +01:00
|
|
|
|
2022-09-08 19:54:37 +01:00
|
|
|
void InsightWorker::executeTasks() {
|
|
|
|
|
static const auto getQueuedTask = [] () -> std::optional<InsightWorkerTask*> {
|
|
|
|
|
const auto taskQueueLock = InsightWorker::queuedTasksById.acquireLock();
|
|
|
|
|
auto& queuedTasks = InsightWorker::queuedTasksById.getValue();
|
2022-02-05 15:32:08 +00:00
|
|
|
|
2022-09-08 19:54:37 +01:00
|
|
|
if (!queuedTasks.empty()) {
|
|
|
|
|
const auto taskGroupsLock = InsightWorker::taskGroupsInExecution.acquireLock();
|
|
|
|
|
auto& taskGroupsInExecution = InsightWorker::taskGroupsInExecution.getValue();
|
2021-10-06 21:12:31 +01:00
|
|
|
|
2022-09-08 19:54:37 +01:00
|
|
|
const auto canExecuteTask = [&taskGroupsInExecution] (InsightWorkerTask* task) {
|
|
|
|
|
for (const auto taskGroup : task->getTaskGroups()) {
|
|
|
|
|
if (taskGroupsInExecution.contains(taskGroup)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (auto [queuedTaskId, task] : queuedTasks) {
|
|
|
|
|
if (canExecuteTask(task)) {
|
|
|
|
|
const auto taskGroups = task->getTaskGroups();
|
|
|
|
|
taskGroupsInExecution.insert(taskGroups.begin(), taskGroups.end());
|
|
|
|
|
queuedTasks.erase(queuedTaskId);
|
|
|
|
|
return task;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return std::nullopt;
|
|
|
|
|
};
|
2022-06-05 17:00:56 +01:00
|
|
|
|
2022-09-07 22:25:28 +01:00
|
|
|
auto queuedTask = std::optional<InsightWorkerTask*>();
|
|
|
|
|
|
2022-09-08 19:54:37 +01:00
|
|
|
while ((queuedTask = getQueuedTask())) {
|
2022-09-07 22:25:28 +01:00
|
|
|
auto* task = queuedTask.value();
|
|
|
|
|
task->moveToThread(this->thread());
|
|
|
|
|
task->setParent(this);
|
|
|
|
|
task->execute(this->targetControllerConsole);
|
2022-09-08 19:54:37 +01:00
|
|
|
|
|
|
|
|
{
|
|
|
|
|
const auto taskGroupsLock = InsightWorker::taskGroupsInExecution.acquireLock();
|
|
|
|
|
auto& taskGroupsInExecution = InsightWorker::taskGroupsInExecution.getValue();
|
|
|
|
|
|
|
|
|
|
for (const auto& taskGroup : task->getTaskGroups()) {
|
|
|
|
|
taskGroupsInExecution.erase(taskGroup);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-07 22:25:28 +01:00
|
|
|
task->deleteLater();
|
2022-09-08 19:57:04 +01:00
|
|
|
emit InsightSignals::instance()->taskProcessed();
|
2022-02-05 15:32:08 +00:00
|
|
|
}
|
2021-10-06 21:12:31 +01:00
|
|
|
}
|
|
|
|
|
}
|