automatically delete tasks that have completed successfully

This commit is contained in:
itsmattkc
2019-06-30 12:28:40 -07:00
parent ed87e467ae
commit 8e006f3883
3 changed files with 41 additions and 4 deletions
+3
View File
@@ -134,6 +134,9 @@ public:
*
* Dependencies can only be added if the Task is kWaiting.
*
* Naturally Tasks should never be dependent on each other. Circular dependencies will result in Tasks that never
* begin.
*
* @param dependency
*/
void AddDependency(Task* dependency);
+21 -4
View File
@@ -88,21 +88,38 @@ void TaskManager::StartNextWaiting()
}
}
void TaskManager::DeleteTask(Task *t)
{
// TODO Cancelling tasks?
// Remove instances of Task from queue
tasks_.removeAll(t);
// Destroy Task object
delete t;
}
void TaskManager::TaskCallback(Task::Status status)
{
switch (status) {
case Task::kWaiting:
qDebug() << sender() << "is waiting...";
//qDebug() << sender() << "is waiting...";
break;
case Task::kWorking:
qDebug() << sender() << "is working...";
//qDebug() << sender() << "is working...";
break;
case Task::kFinished:
qDebug() << sender() << "finished successfully.";
//qDebug() << sender() << "finished successfully.";
// The Task has finished, we can start a new one
StartNextWaiting();
DeleteTask(static_cast<Task*>(sender()));
break;
case Task::kError:
qDebug() << sender() << "failed:" << static_cast<Task*>(sender())->error();
//qDebug() << sender() << "failed:" << static_cast<Task*>(sender())->error();
// The Task has finished, we can start a new one
StartNextWaiting();
break;
}
+17
View File
@@ -77,6 +77,9 @@ public:
*
* NOTE: This function is NOT thread-safe and is currently intended to only be used from the main/GUI thread.
*
* NOTE: A Task object should only be added once. Adding the same Task object more than once will result in undefined
* behavior.
*
* @param t
*
* The task to add and run. TaskManager takes ownership of this Task and will be responsible for freeing it.
@@ -105,10 +108,24 @@ private:
* This function is run whenever a Task is added and whenever a Task finishes. It determines how many Tasks are
* currently running and therefore how many Tasks can be started (if any). It will then start ones that can.
*
* This function is aware of "dependency Tasks" and if a Task is waiting but has a dependency that hasn't finished,
* it will skip to the next one.
*
* Like AddTask, this function is NOT thread-safe and currently only intended to be run from the main thread.
*/
void StartNextWaiting();
/**
* @brief Removes the Task from the queue and deletes it
*
* Recommended for use after a Task has completed or errorred.
*
* @param t
*
* Task to delete
*/
void DeleteTask(Task* t);
/**
* @brief Internal task array
*/