Skip to content
Snippets Groups Projects
Commit c994d4b7 authored by Nick Draper's avatar Nick Draper
Browse files

remove any finished algorithms from the managed list

when creating a new algorithm
re #28093
parent 1d30456c
No related merge requests found
......@@ -78,6 +78,9 @@ private:
/// Unimplemented assignment operator
AlgorithmManagerImpl &operator=(const AlgorithmManagerImpl &);
/// Removes any finished algorithms from the list of managed algorithms
size_t removeFinishedAlgorithms();
/// The maximum size of the algorithm store
int m_max_no_algs;
/// The list of managed algorithms
......
......@@ -82,6 +82,11 @@ IAlgorithm_sptr AlgorithmManagerImpl::create(const std::string &algName,
else
alg = unmanagedAlg;
auto count = removeFinishedAlgorithms();
g_log.debug()
<< count
<< " Finished algorithms removed from the managed algorithms list. "
<< m_managed_algs.size() << " remaining.\n";
// If this takes us beyond the maximum size, then remove the oldest one(s)
while (m_managed_algs.size() >=
static_cast<std::deque<IAlgorithm_sptr>::size_type>(m_max_no_algs)) {
......@@ -241,6 +246,25 @@ void AlgorithmManagerImpl::cancelAll() {
}
}
/// Removes all of the finished algorithms
/// this does not lock the mutex as the locking is already assumed to be in place
size_t AlgorithmManagerImpl::removeFinishedAlgorithms() {
std::vector<IAlgorithm_const_sptr> theCompletedInstances;
std::copy_if(m_managed_algs.cbegin(), m_managed_algs.cend(),
std::back_inserter(theCompletedInstances),
[](const auto &algorithm) { return (algorithm->executionState() == ExecutionState::Finished); });
for (auto completedAlg : theCompletedInstances) {
auto itend = m_managed_algs.end();
for (auto it = m_managed_algs.begin(); it != itend; ++it) {
if ((**it).getAlgorithmID() == completedAlg->getAlgorithmID()) {
m_managed_algs.erase(it);
break;
}
}
}
return theCompletedInstances.size();
}
void AlgorithmManagerImpl::shutdown() { clear(); }
} // namespace API
} // namespace Mantid
......@@ -86,6 +86,13 @@ public:
bool isRunning() const override { return isRunningFlag; }
void setIsRunningTo(bool runningFlag) { isRunningFlag = runningFlag; }
void cancel() override { isRunningFlag = false; }
ExecutionState executionState() const override {
return isRunningFlag ? ExecutionState::Running : ExecutionState::Finished;
}
/// Gets the current result State
ResultState resultState() const override {
return isRunningFlag ? ResultState::NotFinished : ResultState::Failed;
}
};
DECLARE_ALGORITHM(AlgTest)
......
......@@ -44,10 +44,11 @@ class AlgorithmManagerTest(unittest.TestCase):
self.assertTrue(isinstance(alg, Algorithm))
def test_size_reports_number_of_managed_algorithms(self):
old_size = AlgorithmManager.size()
new_alg = AlgorithmManager.create("ConvertUnits")
new_size = AlgorithmManager.size()
self.assertEqual(new_size, old_size + 1)
# no longer deterministically possible to have a correct answer for size
# if test are run multi threaded
# just check we got an integer back
size = AlgorithmManager.size()
self.assertTrue(isinstance(size, int))
def test_getAlgorithm_returns_correct_instance(self):
returned_instance = AlgorithmManager.create("ConvertUnits")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment