Skip to content

Commit

Permalink
More JobManager/JobWorker/AbstractJob work.
Browse files Browse the repository at this point in the history
  • Loading branch information
dloman77 committed Jan 11, 2010
1 parent e7c1a56 commit 09e5d05
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 3 deletions.
3 changes: 3 additions & 0 deletions include/GS/Jobs/AbstractJob.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ class AbstractJob {
virtual ~AbstractJob();

JobResult doJob();
JobStatus getStatus();

private:
unsigned int jobID;

JobStatus status;

virtual JobResult _doJob();
};

Expand Down
13 changes: 11 additions & 2 deletions src/GS/Jobs/AbstractJob.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,24 @@

AbstractJob::AbstractJob()
{
this->status = JOB_NOTSTARTED;
}

AbstractJob::~AbstractJob()
{
}

bool AbstractJob::doJob()
JobResult AbstractJob::doJob()
{
return false;
this->status = JOB_RUNNING;
JobResult retVal = this->_doJob();
this->status = JOB_FINISHED;
return retVal;
}

JobStatus AbstractJob::getStatus()
{
return this->status;
}

// Local Variables: ***
Expand Down
59 changes: 58 additions & 1 deletion src/GS/Jobs/JobManager.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,77 @@
*/
/** @file JobManager.cxx
*
* Brief description
* Singleton.
*
*/

#include "GS/Jobs/JobManager.h"
#include <QMutexLocker>

JobManager* JobManager::pInstance = NULL;

JobManager::JobManager()
{

this->jobQueue = new QList<AbstractJob*> ();
this->queueLock = new QMutex();

this->jobWorkers = new QList<JobWorker*> ();

for (quint32 i = 0; i < MAX_JOBWORKERS; ++i)
{
JobWorker* jw = new JobWorker();
this->jobWorkers->append(jw);
jw->start();
}
}

JobManager::~JobManager()
{
delete jobQueue;
//TODO Should I loop through jobs, destroying them as well?
delete queueLock;

delete jobWorkers;
//TODO Should I loop through workers, destroying them as well?
}

JobManager* JobManager::getInstance()
{
if (!JobManager::pInstance)
{
pInstance = new JobManager();
}
return JobManager::pInstance;
}

void JobManager::submitJob(AbstractJob* job)
{
QMutexLocker locker(this->queueLock);
this->jobQueue->append(job);
}

AbstractJob* JobManager::getNextJob()
{
QMutexLocker locker(this->queueLock);
if (!this->jobQueue->isEmpty())
{
return this->jobQueue->takeFirst();
}
else
{
return NULL;
}
}

bool JobManager::hasJobsToWork()
{
QMutexLocker locker(this->queueLock);
return !this->jobQueue->isEmpty();
}

//TODO add JobWorker Monitor.

// Local Variables: ***
// mode: C++ ***
// tab-width: 8 ***
Expand Down
42 changes: 42 additions & 0 deletions src/GS/Jobs/JobWorker.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,57 @@
*/

#include "GS/Jobs/JobWorker.h"
#include "GS/Jobs/JobManager.h"

JobWorker::JobWorker()
{
this->status = WORKER_NOTREADY;
this->runCmd = true;
}

JobWorker::~JobWorker()
{
}

void JobWorker::run()
{
JobManager* jm = JobManager::getInstance();

this->status = WORKER_READY;

while (this->runCmd)
{
if (!jm->hasJobsToWork())
{
this->msleep(100);
}
else
{
AbstractJob* job = jm->getNextJob();

if (job == NULL)
{
continue;
}

JobResult result = job->doJob();

//TODO log the result?

}
}
this->status = WORKER_NOTREADY;
}

JobWorkerStatus JobWorker::getStatus()
{
return this->status;
}

void JobWorker::shutdown()
{
this->runCmd = false;
}
// Local Variables: ***
// mode: C++ ***
// tab-width: 8 ***
Expand Down

0 comments on commit 09e5d05

Please sign in to comment.