From 2f383af5550ad9fc89dd2426f11cbec3f9ba4316 Mon Sep 17 00:00:00 2001 From: Davide Punzo Date: Tue, 4 Jun 2024 12:10:52 +0200 Subject: [PATCH] ENH: Improve signals throttling --- Libs/Core/ctkJobScheduler.cpp | 51 ++++++++++++++++++++++++++++++----- Libs/Core/ctkJobScheduler_p.h | 1 + 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/Libs/Core/ctkJobScheduler.cpp b/Libs/Core/ctkJobScheduler.cpp index e67311b85d..707ba38da8 100644 --- a/Libs/Core/ctkJobScheduler.cpp +++ b/Libs/Core/ctkJobScheduler.cpp @@ -861,12 +861,51 @@ void ctkJobScheduler::emitThrottledSignals() { Q_D(ctkJobScheduler); - emit this->jobStarted(d->BatchedJobsStarted); - emit this->jobUserStopped(d->BatchedJobsUserStopped); - emit this->jobFinished(d->BatchedJobsFinished); - emit this->jobAttemptFailed(d->BatchedJobsAttemptFailed); - emit this->jobFailed(d->BatchedJobsFailed); + int totalEmitted = 0; + if (!d->BatchedJobsStarted.isEmpty() && totalEmitted < d->MaximumBatchedSignalsForTimeInterval) + { + int count = qMin(d->MaximumBatchedSignalsForTimeInterval - totalEmitted, d->BatchedJobsStarted.size()); + emit this->jobStarted(d->BatchedJobsStarted.mid(0, count)); + d->BatchedJobsStarted = d->BatchedJobsStarted.mid(count); + totalEmitted += count; + } + if (!d->BatchedJobsUserStopped.isEmpty() && totalEmitted < d->MaximumBatchedSignalsForTimeInterval) + { + int count = qMin(d->MaximumBatchedSignalsForTimeInterval - totalEmitted, d->BatchedJobsUserStopped.size()); + emit this->jobUserStopped(d->BatchedJobsUserStopped.mid(0, count)); + d->BatchedJobsUserStopped = d->BatchedJobsUserStopped.mid(count); + totalEmitted += count; + } + if (!d->BatchedJobsFinished.isEmpty() && totalEmitted < d->MaximumBatchedSignalsForTimeInterval) + { + int count = qMin(d->MaximumBatchedSignalsForTimeInterval - totalEmitted, d->BatchedJobsFinished.size()); + emit this->jobFinished(d->BatchedJobsFinished.mid(0, count)); + d->BatchedJobsFinished = d->BatchedJobsFinished.mid(count); + totalEmitted += count; + } + if (!d->BatchedJobsAttemptFailed.isEmpty() && totalEmitted < d->MaximumBatchedSignalsForTimeInterval) + { + int count = qMin(d->MaximumBatchedSignalsForTimeInterval - totalEmitted, d->BatchedJobsAttemptFailed.size()); + emit this->jobAttemptFailed(d->BatchedJobsAttemptFailed.mid(0, count)); + d->BatchedJobsAttemptFailed = d->BatchedJobsAttemptFailed.mid(count); + totalEmitted += count; + } + if (!d->BatchedJobsFailed.isEmpty() && totalEmitted < d->MaximumBatchedSignalsForTimeInterval) + { + int count = qMin(d->MaximumBatchedSignalsForTimeInterval - totalEmitted, d->BatchedJobsFailed.size()); + emit this->jobFailed(d->BatchedJobsFailed.mid(0, count)); + d->BatchedJobsFailed = d->BatchedJobsFailed.mid(count); + totalEmitted += count; + } + emit this->progressJobDetail(d->BatchedJobsProgress); + d->BatchedJobsProgress.clear(); - d->clearBactchedJobsLists(); + int numberOfSignalsNotSent = d->BatchedJobsStarted.size() + d->BatchedJobsUserStopped.size() + + d->BatchedJobsFinished.size() + d->BatchedJobsAttemptFailed.size() + + d->BatchedJobsFailed.size(); + if (numberOfSignalsNotSent != 0 && !d->ThrottleTimer->isActive()) + { + d->ThrottleTimer->start(d->ThrottleTimeInterval); + } } diff --git a/Libs/Core/ctkJobScheduler_p.h b/Libs/Core/ctkJobScheduler_p.h index 2df195d66a..f6a792abac 100644 --- a/Libs/Core/ctkJobScheduler_p.h +++ b/Libs/Core/ctkJobScheduler_p.h @@ -83,6 +83,7 @@ public Q_SLOTS: QList BatchedJobsProgress; QSharedPointer ThrottleTimer; int ThrottleTimeInterval{300}; + int MaximumBatchedSignalsForTimeInterval{20}; }; #endif