Skip to content
Permalink
Browse files
Add support for placing queued tasks on hold
  • Loading branch information
nyalldawson committed Dec 5, 2016
1 parent cf5eeb7 commit dcecf4476f4851f6e0ffa0e8f27e69edd4e1be30
Showing with 76 additions and 2 deletions.
  1. +11 −0 python/core/qgstaskmanager.sip
  2. +33 −2 src/core/qgstaskmanager.cpp
  3. +15 −0 src/core/qgstaskmanager.h
  4. +17 −0 tests/src/core/testqgstaskmanager.cpp
@@ -15,6 +15,7 @@ class QgsTask : QObject
enum TaskStatus
{
Queued, /*!< Task is queued and has not begun */
OnHold, /*!< Task is queued but on hold and will not be started */
Running, /*!< Task is currently running */
Complete, /*!< Task successfully completed */
Terminated, /*!< Task was terminated or errored */
@@ -63,6 +64,16 @@ class QgsTask : QObject
//! @see isCancelled()
void cancel();

//! Called when the task is placed on hold. If the task in not queued
//! (ie it is running or has finished) then calling this has no effect.
//! @see unhold()
void hold();

//! Called when the task should be unheld and re-added to the queue. If the
//! task in not currently being held then calling this has no effect.
//! @see unhold()
void unhold();

//! Sets the task's current progress. If task reports the CanReportProgress flag then
//! the derived class should call this method whenever the task wants to update its
//! progress. Calling will automatically emit the progressChanged signal.
@@ -45,6 +45,24 @@ void QgsTask::cancel()
mShouldTerminate = true;
}

void QgsTask::hold()
{
if ( mStatus == Queued )
{
mStatus = OnHold;
emit statusChanged( OnHold );
}
}

void QgsTask::unhold()
{
if ( mStatus == OnHold )
{
mStatus = Queued;
emit statusChanged( Queued );
}
}

void QgsTask::setProgress( double progress )
{
mProgress = progress;
@@ -107,9 +125,9 @@ long QgsTaskManager::addTask( QgsTask* task )
connect( task, SIGNAL( progressChanged( double ) ), this, SLOT( taskProgressChanged( double ) ) );
connect( task, SIGNAL( statusChanged( int ) ), this, SLOT( taskStatusChanged( int ) ) );

mTasks[ mNextTaskId ].future = QtConcurrent::run( task, &QgsTask::start );

emit taskAdded( mNextTaskId );
processQueue();

return mNextTaskId++;
}

@@ -202,6 +220,7 @@ void QgsTaskManager::taskStatusChanged( int status )
return;

emit statusChanged( id, status );
processQueue();
}

bool QgsTaskManager::cleanupAndDeleteTask( QgsTask *task )
@@ -228,3 +247,15 @@ bool QgsTaskManager::cleanupAndDeleteTask( QgsTask *task )
task->deleteLater();
return true;
}

void QgsTaskManager::processQueue()
{
for ( QMap< long, TaskInfo >::iterator it = mTasks.begin(); it != mTasks.end(); ++it )
{
QgsTask* task = it.value().task;
if ( task && task->status() == QgsTask::Queued )
{
mTasks[ it.key()].future = QtConcurrent::run( task, &QgsTask::start );
}
}
}
@@ -39,6 +39,7 @@ class CORE_EXPORT QgsTask : public QObject
enum TaskStatus
{
Queued, /*!< Task is queued and has not begun */
OnHold, /*!< Task is queued but on hold and will not be started */
Running, /*!< Task is currently running */
Complete, /*!< Task successfully completed */
Terminated, /*!< Task was terminated or errored */
@@ -87,6 +88,16 @@ class CORE_EXPORT QgsTask : public QObject
//! @see isCancelled()
void cancel();

//! Called when the task is placed on hold. If the task in not queued
//! (ie it is running or has finished) then calling this has no effect.
//! @see unhold()
void hold();

//! Called when the task should be unheld and re-added to the queue. If the
//! task in not currently being held then calling this has no effect.
//! @see unhold()
void unhold();

//! Sets the task's current progress. If task reports the CanReportProgress flag then
//! the derived class should call this method whenever the task wants to update its
//! progress. Calling will automatically emit the progressChanged signal.
@@ -267,6 +278,10 @@ class CORE_EXPORT QgsTaskManager : public QObject

bool cleanupAndDeleteTask( QgsTask* task );

//! Process the queue of outstanding jobs and starts up any
//! which are ready to go.
void processQueue();

};

#endif //QGSTASKMANAGER_H
@@ -84,6 +84,7 @@ class TestQgsTaskManager : public QObject
void taskId();
void progressChanged();
void statusChanged();
void holdTask();

private:

@@ -334,6 +335,22 @@ void TestQgsTaskManager::statusChanged()
QCOMPARE( static_cast< QgsTask::TaskStatus >( spy.last().at( 1 ).toInt() ), QgsTask::Complete );
}

void TestQgsTaskManager::holdTask()
{
QgsTaskManager manager;
TestTask* task = new TestTask();
//hold task
task->hold();
manager.addTask( task );
//should not be started
QCOMPARE( task->status(), QgsTask::OnHold );

task->unhold();
// wait for task to spin up
while ( task->status() == QgsTask::Queued ) {}
QCOMPARE( task->status(), QgsTask::Running );
}


QTEST_MAIN( TestQgsTaskManager )
#include "testqgstaskmanager.moc"

0 comments on commit dcecf44

Please sign in to comment.