diff --git a/app/task/task.h b/app/task/task.h index f15d58e4e..43093504d 100644 --- a/app/task/task.h +++ b/app/task/task.h @@ -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); diff --git a/app/task/taskmanager.cpp b/app/task/taskmanager.cpp index fa5d74d06..1ad215a70 100644 --- a/app/task/taskmanager.cpp +++ b/app/task/taskmanager.cpp @@ -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(sender())); break; case Task::kError: - qDebug() << sender() << "failed:" << static_cast(sender())->error(); + //qDebug() << sender() << "failed:" << static_cast(sender())->error(); + + // The Task has finished, we can start a new one StartNextWaiting(); break; } diff --git a/app/task/taskmanager.h b/app/task/taskmanager.h index 5f8f1cb82..67b22fdb5 100644 --- a/app/task/taskmanager.h +++ b/app/task/taskmanager.h @@ -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 */