BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
JobQueueData Class Reference

Description

The JobQueueData class holds all objects/logic to run simulation in a thread.

Definition at line 28 of file JobQueueData.h.

Inheritance diagram for JobQueueData:
[legend]
Collaboration diagram for JobQueueData:
[legend]

Public Slots

void cancelJob (const QString &identifier)
 Cancels running job. More...
 
void onCancelAllJobs ()
 Cancels all running jobs. More...
 
void onFinishedJob ()
 Performs necessary actions when job is finished. More...
 
void onFinishedThread ()
 
void onProgressUpdate ()
 
void onStartedJob ()
 Sets JobItem properties when the job is going to start. More...
 
void removeJob (const QString &identifier)
 Remove job from list completely. More...
 
void runJob (JobItem *jobItem)
 Submits job and run it in a thread. More...
 

Signals

void focusRequest (JobItem *jobItem)
 
void globalProgress (int)
 

Public Member Functions

 JobQueueData (JobModel *jobModel)
 
bool hasUnfinishedJobs ()
 

Private Member Functions

void assignForDeletion (JobWorker *worker)
 Removes JobRunner from the map of known runners, assigns it for deletion. More...
 
void assignForDeletion (QThread *thread)
 Removes QThread from the map of known threads, assigns it for deletion. More...
 
void clearSimulation (const QString &identifier)
 
ISimulation * getSimulation (const QString &identifier)
 Returns the simulation (if exists) for given identifier. More...
 
QThread * getThread (const QString &identifier)
 Returns the thread for given identifier. More...
 
JobWorkergetWorker (const QString &identifier)
 Returns job runner for given identifier. More...
 
void processFinishedJob (JobWorker *worker, JobItem *jobItem)
 Set all data of finished job. More...
 
void updateGlobalProgress ()
 Estimates global progress from the progress of multiple running jobs and emits appropriate signal. More...
 

Private Attributes

JobModelm_jobModel
 job identifier to simulation More...
 
QMap< QString, ISimulation * > m_simulations
 job identifier to jobWorker More...
 
QMap< QString, QThread * > m_threads
 
QMap< QString, JobWorker * > m_workers
 job identifier to the thread More...
 

Constructor & Destructor Documentation

◆ JobQueueData()

JobQueueData::JobQueueData ( JobModel jobModel)

Definition at line 26 of file JobQueueData.cpp.

27  : m_jobModel(jobModel)
28 {
29 }
JobModel * m_jobModel
job identifier to simulation
Definition: JobQueueData.h:67

Member Function Documentation

◆ assignForDeletion() [1/2]

void JobQueueData::assignForDeletion ( JobWorker worker)
private

Removes JobRunner from the map of known runners, assigns it for deletion.

Definition at line 197 of file JobQueueData.cpp.

198 {
199  ASSERT(worker);
200  worker->disconnect();
201  for (auto it = m_workers.begin(); it != m_workers.end(); ++it) {
202  if (it.value() == worker) {
203  m_workers.erase(it);
204  delete worker;
205  return;
206  }
207  }
208 
209  throw Error("JobQueueData::assignForDeletion() -> Error! Can't find the runner.");
210 }
QMap< QString, JobWorker * > m_workers
job identifier to the thread
Definition: JobQueueData.h:64

References Error, and m_workers.

◆ assignForDeletion() [2/2]

void JobQueueData::assignForDeletion ( QThread *  thread)
private

Removes QThread from the map of known threads, assigns it for deletion.

Definition at line 182 of file JobQueueData.cpp.

183 {
184  for (auto it = m_threads.begin(); it != m_threads.end(); ++it) {
185  if (it.value() == thread) {
186  thread->deleteLater();
187  m_threads.erase(it);
188  return;
189  }
190  }
191 
192  throw Error("JobQueueData::assignForDeletion() -> Error! Can't find thread.");
193 }
QMap< QString, QThread * > m_threads
Definition: JobQueueData.h:63

References Error, and m_threads.

Referenced by onFinishedJob(), and onFinishedThread().

◆ cancelJob

void JobQueueData::cancelJob ( const QString &  identifier)
slot

Cancels running job.

Definition at line 88 of file JobQueueData.cpp.

89 {
90  if (getThread(identifier))
91  getWorker(identifier)->terminate();
92 }
JobWorker * getWorker(const QString &identifier)
Returns job runner for given identifier.
QThread * getThread(const QString &identifier)
Returns the thread for given identifier.
void terminate()
Sets request for JobRunner to terminate underlying domain simulation.
Definition: JobWorker.cpp:109

References getThread(), getWorker(), and JobWorker::terminate().

Referenced by JobModel::cancelJob(), onCancelAllJobs(), and removeJob().

Here is the call graph for this function:

◆ clearSimulation()

void JobQueueData::clearSimulation ( const QString &  identifier)
private

Definition at line 212 of file JobQueueData.cpp.

213 {
214  auto* simulation = getSimulation(identifier);
215  m_simulations.remove(identifier);
216  delete simulation;
217 }
ISimulation * getSimulation(const QString &identifier)
Returns the simulation (if exists) for given identifier.
QMap< QString, ISimulation * > m_simulations
job identifier to jobWorker
Definition: JobQueueData.h:65

References getSimulation(), and m_simulations.

Referenced by onFinishedJob(), and removeJob().

Here is the call graph for this function:

◆ focusRequest

void JobQueueData::focusRequest ( JobItem jobItem)
signal

◆ getSimulation()

ISimulation * JobQueueData::getSimulation ( const QString &  identifier)
private

Returns the simulation (if exists) for given identifier.

Definition at line 257 of file JobQueueData.cpp.

258 {
259  auto it = m_simulations.find(identifier);
260  return it != m_simulations.end() ? it.value() : nullptr;
261 }

References m_simulations.

Referenced by clearSimulation(), and runJob().

◆ getThread()

QThread * JobQueueData::getThread ( const QString &  identifier)
private

Returns the thread for given identifier.

Definition at line 241 of file JobQueueData.cpp.

242 {
243  auto it = m_threads.find(identifier);
244  return it != m_threads.end() ? it.value() : nullptr;
245 }

References m_threads.

Referenced by cancelJob(), onFinishedJob(), and runJob().

◆ getWorker()

JobWorker * JobQueueData::getWorker ( const QString &  identifier)
private

Returns job runner for given identifier.

Definition at line 249 of file JobQueueData.cpp.

250 {
251  auto it = m_workers.find(identifier);
252  return it != m_workers.end() ? it.value() : nullptr;
253 }

References m_workers.

Referenced by cancelJob().

◆ globalProgress

void JobQueueData::globalProgress ( int  )
signal

◆ hasUnfinishedJobs()

bool JobQueueData::hasUnfinishedJobs ( )

Definition at line 31 of file JobQueueData.cpp.

32 {
33  return !m_simulations.empty();
34 }

References m_simulations.

Referenced by JobModel::hasUnfinishedJobs(), and onFinishedJob().

◆ onCancelAllJobs

void JobQueueData::onCancelAllJobs ( )
slot

Cancels all running jobs.

Definition at line 174 of file JobQueueData.cpp.

175 {
176  for (const auto& key : m_threads.keys())
177  cancelJob(key);
178 }
void cancelJob(const QString &identifier)
Cancels running job.

References cancelJob(), and m_threads.

Here is the call graph for this function:

◆ onFinishedJob

void JobQueueData::onFinishedJob ( )
slot

Performs necessary actions when job is finished.

Definition at line 117 of file JobQueueData.cpp.

118 {
119  auto* worker = qobject_cast<JobWorker*>(sender());
120 
121  auto* jobItem = m_jobModel->getJobItemForIdentifier(worker->identifier());
122  processFinishedJob(worker, jobItem);
123 
124  // I tell to the thread to exit here (instead of connecting JobRunner::finished
125  // to the QThread::quit because of strange behaviour)
126  getThread(worker->identifier())->quit();
127 
128  emit focusRequest(jobItem);
129 
130  clearSimulation(worker->identifier());
131  assignForDeletion(worker);
132 
133  if (!hasUnfinishedJobs())
134  emit globalProgress(100);
135 }
JobItem * getJobItemForIdentifier(const QString &identifier)
Definition: JobModel.cpp:42
bool hasUnfinishedJobs()
void focusRequest(JobItem *jobItem)
void globalProgress(int)
void processFinishedJob(JobWorker *worker, JobItem *jobItem)
Set all data of finished job.
void clearSimulation(const QString &identifier)
void assignForDeletion(QThread *thread)
Removes QThread from the map of known threads, assigns it for deletion.

References assignForDeletion(), clearSimulation(), focusRequest(), JobModel::getJobItemForIdentifier(), getThread(), globalProgress(), hasUnfinishedJobs(), m_jobModel, and processFinishedJob().

Referenced by runJob().

Here is the call graph for this function:

◆ onFinishedThread

void JobQueueData::onFinishedThread ( )
slot

Definition at line 137 of file JobQueueData.cpp.

138 {
139  auto* thread = qobject_cast<QThread*>(sender());
140  assignForDeletion(thread);
141 }

References assignForDeletion().

Referenced by runJob().

Here is the call graph for this function:

◆ onProgressUpdate

void JobQueueData::onProgressUpdate ( )
slot

Definition at line 143 of file JobQueueData.cpp.

144 {
145  auto* worker = qobject_cast<JobWorker*>(sender());
146  auto* jobItem = m_jobModel->getJobItemForIdentifier(worker->identifier());
147  jobItem->setProgress(worker->progress());
149 }
void setProgress(int progress)
Definition: JobItem.cpp:206
void updateGlobalProgress()
Estimates global progress from the progress of multiple running jobs and emits appropriate signal.

References JobModel::getJobItemForIdentifier(), m_jobModel, JobItem::setProgress(), and updateGlobalProgress().

Referenced by runJob().

Here is the call graph for this function:

◆ onStartedJob

void JobQueueData::onStartedJob ( )
slot

Sets JobItem properties when the job is going to start.

Definition at line 104 of file JobQueueData.cpp.

105 {
106  auto* worker = qobject_cast<JobWorker*>(sender());
107 
108  auto* jobItem = m_jobModel->getJobItemForIdentifier(worker->identifier());
109  jobItem->setProgress(0);
110  jobItem->setStatus(JobStatus::Running);
111  jobItem->setBeginTime(worker->simulationStart());
112  jobItem->setEndTime(QDateTime());
113 }
@ Running
the job is busy calculating

References JobModel::getJobItemForIdentifier(), m_jobModel, Running, and JobItem::setProgress().

Referenced by runJob().

Here is the call graph for this function:

◆ processFinishedJob()

void JobQueueData::processFinishedJob ( JobWorker worker,
JobItem jobItem 
)
private

Set all data of finished job.

Definition at line 221 of file JobQueueData.cpp.

222 {
223  jobItem->setEndTime(worker->simulationEnd());
224 
225  // propagating status of runner
226  if (worker->status() == JobStatus::Failed)
227  jobItem->setComments(worker->failureMessage());
228  else {
229  ASSERT(worker->result());
230  jobItem->setResults(*worker->result());
231  }
232  jobItem->setStatus(worker->status());
233 
234  // fixing job progress (if job was successfull, but due to wrong estimation, progress not 100%)
235  if (jobItem->isCompleted())
236  jobItem->setProgress(100);
237 }
@ Failed
the job aborted because it hit an error
void setComments(const QString &comments)
Definition: JobItem.cpp:195
void setEndTime(const QDateTime &end_time)
Definition: JobItem.cpp:175
void setStatus(JobStatus status)
Definition: JobItem.cpp:111
void setResults(const SimulationResult &result)
Definition: JobItem.cpp:251
bool isCompleted() const
Definition: JobItem.cpp:134
QString failureMessage() const
Definition: JobWorker.cpp:87
const SimulationResult * result() const
Definition: JobWorker.cpp:102
const QDateTime & simulationEnd() const
Definition: JobWorker.cpp:97
JobStatus status() const
Definition: JobWorker.cpp:82

References Failed, JobWorker::failureMessage(), JobItem::isCompleted(), JobWorker::result(), JobItem::setComments(), JobItem::setEndTime(), JobItem::setProgress(), JobItem::setResults(), JobItem::setStatus(), JobWorker::simulationEnd(), and JobWorker::status().

Referenced by onFinishedJob().

Here is the call graph for this function:

◆ removeJob

void JobQueueData::removeJob ( const QString &  identifier)
slot

Remove job from list completely.

Definition at line 96 of file JobQueueData.cpp.

97 {
98  cancelJob(identifier);
99  clearSimulation(identifier);
100 }

References cancelJob(), and clearSimulation().

Referenced by JobModel::removeJob().

Here is the call graph for this function:

◆ runJob

void JobQueueData::runJob ( JobItem jobItem)
slot

Submits job and run it in a thread.

Definition at line 38 of file JobQueueData.cpp.

39 {
40  QString identifier = jobItem->getIdentifier();
41  if (getThread(identifier))
42  return;
43 
44  if (getSimulation(identifier))
45  throw Error("JobQueueData::runJob() -> Error. ISimulation is already existing.");
46 
47  try {
49  jobItem->sampleItem(), jobItem->instrumentItem(), jobItem->simulationOptionsItem());
50  m_simulations[identifier] = simulation.release();
51  } catch (const std::exception& ex) {
52  QString message("JobQueueData::runJob() -> Error. "
53  "Attempt to create sample/instrument object from user description "
54  "has failed with following error message.\n\n");
55  message += QString::fromStdString(std::string(ex.what()));
56  jobItem->setComments(message);
57  jobItem->setProgress(100);
58  jobItem->setStatus(JobStatus::Failed);
59  emit focusRequest(jobItem);
60  return;
61  }
62 
63  auto* worker = new JobWorker(identifier, m_simulations[identifier]);
64  m_workers[identifier] = worker;
65 
66  auto* thread = new QThread;
67  worker->moveToThread(thread);
68  m_threads[identifier] = thread;
69 
70  // thread will start the worker
71  connect(thread, &QThread::started, worker, &JobWorker::start);
72 
73  // finished thread will be removed from the list
74  connect(thread, &QThread::finished, this, &JobQueueData::onFinishedThread);
75 
76  // connecting the worker to started/progress slots
77  connect(worker, &JobWorker::started, this, &JobQueueData::onStartedJob);
79 
80  // finished job will do all cleanup
81  connect(worker, &JobWorker::finished, this, &JobQueueData::onFinishedJob);
82 
83  thread->start();
84 }
const SimulationOptionsItem & simulationOptionsItem() const
Definition: JobItem.cpp:415
MultiLayerItem * sampleItem()
Definition: JobItem.cpp:222
InstrumentItem * instrumentItem() const
Definition: JobItem.cpp:233
QString getIdentifier() const
Definition: JobItem.cpp:73
void onStartedJob()
Sets JobItem properties when the job is going to start.
void onFinishedJob()
Performs necessary actions when job is finished.
void onProgressUpdate()
void onFinishedThread()
The JobWorker class provides running the domain simulation in a thread.
Definition: JobWorker.h:28
void progressUpdate()
void start()
Definition: JobWorker.cpp:43
void finished()
void started()
std::unique_ptr< ISimulation > itemsToSimulation(const MultiLayerItem *sampleItem, const InstrumentItem *instrumentItem, const SimulationOptionsItem &optionsItem)
Creates domain simulation from sample and instrument items.

References Error, Failed, JobWorker::finished(), focusRequest(), JobItem::getIdentifier(), getSimulation(), getThread(), JobItem::instrumentItem(), GUI::Transform::ToCore::itemsToSimulation(), m_simulations, m_threads, m_workers, onFinishedJob(), onFinishedThread(), onProgressUpdate(), onStartedJob(), JobWorker::progressUpdate(), JobItem::sampleItem(), JobItem::setComments(), JobItem::setProgress(), JobItem::setStatus(), JobItem::simulationOptionsItem(), JobWorker::start(), and JobWorker::started().

Referenced by JobModel::runJob().

Here is the call graph for this function:

◆ updateGlobalProgress()

void JobQueueData::updateGlobalProgress ( )
private

Estimates global progress from the progress of multiple running jobs and emits appropriate signal.

Definition at line 154 of file JobQueueData.cpp.

155 {
156  int global_progress(0);
157  int nRunningJobs(0);
158  for (auto* jobItem : m_jobModel->jobItems())
159  if (jobItem->isRunning()) {
160  global_progress += jobItem->getProgress();
161  nRunningJobs++;
162  }
163 
164  if (nRunningJobs)
165  global_progress /= nRunningJobs;
166  else
167  global_progress = -1;
168 
169  emit globalProgress(global_progress);
170 }
QVector< JobItem * > jobItems() const
Definition: JobModel.cpp:77

References globalProgress(), JobModel::jobItems(), and m_jobModel.

Referenced by onProgressUpdate().

Here is the call graph for this function:

Member Data Documentation

◆ m_jobModel

JobModel* JobQueueData::m_jobModel
private

job identifier to simulation

Definition at line 67 of file JobQueueData.h.

Referenced by onFinishedJob(), onProgressUpdate(), onStartedJob(), and updateGlobalProgress().

◆ m_simulations

QMap<QString, ISimulation*> JobQueueData::m_simulations
private

job identifier to jobWorker

Definition at line 65 of file JobQueueData.h.

Referenced by clearSimulation(), getSimulation(), hasUnfinishedJobs(), and runJob().

◆ m_threads

QMap<QString, QThread*> JobQueueData::m_threads
private

Definition at line 63 of file JobQueueData.h.

Referenced by assignForDeletion(), getThread(), onCancelAllJobs(), and runJob().

◆ m_workers

QMap<QString, JobWorker*> JobQueueData::m_workers
private

job identifier to the thread

Definition at line 64 of file JobQueueData.h.

Referenced by assignForDeletion(), getWorker(), and runJob().


The documentation for this class was generated from the following files: