Skip to content

Commit

Permalink
COMP: Move startNextJob/onJobCanceled to ctkAbstractWorker
Browse files Browse the repository at this point in the history
  • Loading branch information
jcfr committed Jan 17, 2024
1 parent 40f093e commit 3d86f96
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 63 deletions.
51 changes: 49 additions & 2 deletions Libs/Core/ctkAbstractWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@
=========================================================================*/

// Qt includes
#include <QEventLoop>
#include <QTimer>

// CTK includes
#include "ctkAbstractJob.h"
#include "ctkAbstractScheduler.h"
#include "ctkAbstractWorker.h"

// --------------------------------------------------------------------------
ctkAbstractWorker::ctkAbstractWorker()
{
this->Job = nullptr;
this->Scheduler = nullptr;
this->setAutoDelete(false);
}

Expand Down Expand Up @@ -91,3 +94,47 @@ void ctkAbstractWorker::setScheduler(QSharedPointer<ctkAbstractScheduler> schedu
{
this->Scheduler = scheduler;
}

//----------------------------------------------------------------------------
void ctkAbstractWorker::startNextJob()
{
if (!this->Scheduler || !this->Job)
{
return;
}

ctkAbstractJob* newJob = this->Job->clone();
newJob->setRetryCounter(newJob->retryCounter() + 1);
this->Scheduler->addJob(newJob);
}

//----------------------------------------------------------------------------
void ctkAbstractWorker::onJobCanceled()
{
if (!this->Job)
{
return;
}

if (this->Job->retryCounter() < this->Job->maximumNumberOfRetry() &&
this->Job->status() != ctkAbstractJob::JobStatus::Stopped)
{
QTimer timer;
timer.setSingleShot(true);
QEventLoop loop;
connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
timer.start(this->Job->retryDelay());

this->startNextJob();

emit this->Job->finished();
}
else if (this->Job->status() != ctkAbstractJob::JobStatus::Stopped)
{
emit this->Job->failed();
}
else
{
emit this->Job->finished();
}
}
4 changes: 4 additions & 0 deletions Libs/Core/ctkAbstractWorker.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class CTK_CORE_EXPORT ctkAbstractWorker : public QObject, public QRunnable
void setScheduler(QSharedPointer<ctkAbstractScheduler> scheduler);
///@}

public slots:
virtual void startNextJob();
virtual void onJobCanceled();

protected:
QSharedPointer<ctkAbstractJob> Job;
QSharedPointer<ctkAbstractScheduler> Scheduler;
Expand Down
57 changes: 0 additions & 57 deletions Libs/DICOM/Core/ctkDICOMWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,67 +21,10 @@
=========================================================================*/

#include "ctkDICOMJob.h"
#include "ctkDICOMScheduler.h"
#include "ctkDICOMWorker.h"

//------------------------------------------------------------------------------
ctkDICOMWorker::ctkDICOMWorker() = default;

//------------------------------------------------------------------------------
ctkDICOMWorker::~ctkDICOMWorker() = default;

//----------------------------------------------------------------------------
void ctkDICOMWorker::startNextJob()
{
QSharedPointer<ctkDICOMScheduler> scheduler =
qobject_cast<QSharedPointer<ctkDICOMScheduler>>(this->Scheduler);
if (!scheduler)
{
return;
}

QSharedPointer<ctkDICOMJob> job =
qobject_cast<QSharedPointer<ctkDICOMJob>>(this->Job);
if (!job)
{
return;
}

ctkDICOMJob* newJob = job->clone();
newJob->setRetryCounter(newJob->retryCounter() + 1);
scheduler->addJob(newJob);
}

//----------------------------------------------------------------------------
void ctkDICOMWorker::onJobCanceled()
{
QSharedPointer<ctkDICOMJob> job =
qobject_cast<QSharedPointer<ctkDICOMJob>>(this->Job);
if (!job)
{
return;
}

if (job->retryCounter() < job->maximumNumberOfRetry() &&
job->status() != ctkAbstractJob::JobStatus::Stopped)
{
QTimer timer;
timer.setSingleShot(true);
QEventLoop loop;
connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
timer.start(job->retryDelay());

this->startNextJob();

emit job->finished();
}
else if (job->status() != ctkAbstractJob::JobStatus::Stopped)
{
emit job->failed();
}
else
{
emit job->finished();
}
}
4 changes: 0 additions & 4 deletions Libs/DICOM/Core/ctkDICOMWorker.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ class CTK_DICOM_CORE_EXPORT ctkDICOMWorker : public ctkAbstractWorker
explicit ctkDICOMWorker();
virtual ~ctkDICOMWorker();

public slots:
void startNextJob();
void onJobCanceled();

private:
Q_DISABLE_COPY(ctkDICOMWorker)
};
Expand Down

0 comments on commit 3d86f96

Please sign in to comment.