diff --git a/src/cpp/SingletonManager/PendingTasksManager.cpp b/src/cpp/SingletonManager/PendingTasksManager.cpp index 35c28c72b..1f8071908 100644 --- a/src/cpp/SingletonManager/PendingTasksManager.cpp +++ b/src/cpp/SingletonManager/PendingTasksManager.cpp @@ -4,13 +4,17 @@ PendingTasksManager::PendingTasksManager() + : mCheckForFinishedTimer(2000, 2000) { - + //mCheckForFinishedTimer } PendingTasksManager::~PendingTasksManager() { + Poco::ScopedLock _lock(mWorkMutex); + mCheckForFinishedTimer.stop(); + for (auto it = mPendingTasks.begin(); it != mPendingTasks.end(); it++) { delete it->second; } @@ -27,6 +31,9 @@ int PendingTasksManager::load() { // they add them self to Pending Task Manager auto pending_tasks = controller::PendingTask::loadAll(); + Poco::TimerCallback callback(*this, &PendingTasksManager::checkForFinishedTasks); + mCheckForFinishedTimer.start(callback); + return 0; } @@ -138,9 +145,11 @@ std::vector> PendingTasksManager::getTra auto list = map_it->second; for (auto list_it = list->begin(); list_it != list->end(); list_it++) { - auto transaction = dynamic_cast(list_it->get()); - if (transaction->mustSign(user)) { - transactions_to_sign.push_back(*list_it); + if ((*list_it)->getModel()->isGradidoTransaction()) { + auto transaction = dynamic_cast(list_it->get()); + if (transaction->mustSign(user)) { + transactions_to_sign.push_back(*list_it); + } } } } @@ -158,15 +167,45 @@ std::vector> PendingTasksManager::getTran auto list = map_it->second; for (auto list_it = list->begin(); list_it != list->end(); list_it++) { - auto transaction = dynamic_cast(list_it->get()); - if (transaction->needSomeoneToSign(user)) { - transactions_to_sign.push_back(*list_it); + if ((*list_it)->getModel()->isGradidoTransaction()) { + auto transaction = dynamic_cast(list_it->get()); + if (transaction->needSomeoneToSign(user)) { + transactions_to_sign.push_back(*list_it); + } } } } return transactions_to_sign; } +void PendingTasksManager::checkForFinishedTasks(Poco::Timer& timer) +{ + Poco::ScopedLock _lock(mWorkMutex); + + for (auto map_it = mPendingTasks.begin(); map_it != mPendingTasks.end(); map_it++) + { + auto list = map_it->second; + for (auto list_it = list->begin(); list_it != list->end(); list_it++) + { + if ((*list_it)->getModel()->isGradidoTransaction()) { + auto transaction = dynamic_cast(list_it->get()); + auto json = transaction->getModel()->getResultJson(); + bool removeIt = false; + if (!json.isNull()) { + if (json->get("state").toString() == "success") { + removeIt = true; + } + } + if (removeIt) { + transaction->deleteFromDB(); + list_it = list->erase(list_it); + if (!list->size()) break; + } + } + } + } +} + Poco::AutoPtr PendingTasksManager::getPendingTask(int pendingTaskId) { Poco::ScopedLock _lock(mWorkMutex); @@ -183,6 +222,7 @@ Poco::AutoPtr PendingTasksManager::getPendingTask(int p return nullptr; } + void PendingTasksManager::reportErrorToCommunityServer(Poco::AutoPtr task, std::string error, std::string errorDetails) { // TODO: choose user specific server diff --git a/src/cpp/SingletonManager/PendingTasksManager.h b/src/cpp/SingletonManager/PendingTasksManager.h index cec9bc5b6..8444f32ea 100644 --- a/src/cpp/SingletonManager/PendingTasksManager.h +++ b/src/cpp/SingletonManager/PendingTasksManager.h @@ -17,8 +17,11 @@ #include "../controller/User.h" #include "../model/gradido/Transaction.h" -class PendingTasksManager: public UniLib::lib::MultithreadContainer +class UserUpdateGroupPage; + +class PendingTasksManager: protected UniLib::lib::MultithreadContainer { + friend UserUpdateGroupPage; public: typedef std::list> PendingTaskList; @@ -34,6 +37,9 @@ public: int addTask(Poco::AutoPtr task); bool removeTask(Poco::AutoPtr task); + //! check if tasks can be removed + void checkForFinishedTasks(Poco::Timer& timer); + //! by calling this, important is to call lock to prevent vanishing the list while working with it, //! and unlock afterwards //! \return list or nullptr if no list for user exist @@ -49,7 +55,7 @@ public: protected: PendingTasksManager(); - + Poco::Timer mCheckForFinishedTimer; std::map mPendingTasks; diff --git a/src/cpp/controller/PendingTask.cpp b/src/cpp/controller/PendingTask.cpp index e36fdcc7d..2b991b580 100644 --- a/src/cpp/controller/PendingTask.cpp +++ b/src/cpp/controller/PendingTask.cpp @@ -87,10 +87,6 @@ namespace controller { { Poco::ScopedLock _lock(mWorkMutex); auto result = mDBModel->deleteFromDB(); - if (result) { - - PendingTasksManager::getInstance()->removeTask(Poco::AutoPtr(this, true)); - } return result; }