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

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

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)
 
ISimulationgetSimulation (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 emmits 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...
 

Detailed Description

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

Definition at line 28 of file JobQueueData.h.

Constructor & Destructor Documentation

◆ JobQueueData()

JobQueueData::JobQueueData ( JobModel jobModel)

Definition at line 28 of file JobQueueData.cpp.

28 : m_jobModel(jobModel) {}
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 201 of file JobQueueData.cpp.

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

References ASSERT, 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 186 of file JobQueueData.cpp.

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

References 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:98

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 216 of file JobQueueData.cpp.

217 {
218  auto simulation = getSimulation(identifier);
219  m_simulations.remove(identifier);
220  delete simulation;
221 }
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 263 of file JobQueueData.cpp.

264 {
265  auto it = m_simulations.find(identifier);
266  return it != m_simulations.end() ? it.value() : nullptr;
267 }

References m_simulations.

Referenced by clearSimulation(), processFinishedJob(), and runJob().

◆ getThread()

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

Returns the thread for given identifier.

Definition at line 247 of file JobQueueData.cpp.

248 {
249  auto it = m_threads.find(identifier);
250  return it != m_threads.end() ? it.value() : nullptr;
251 }

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 255 of file JobQueueData.cpp.

256 {
257  auto it = m_workers.find(identifier);
258  return it != m_workers.end() ? it.value() : nullptr;
259 }

References m_workers.

Referenced by cancelJob().

◆ globalProgress

void JobQueueData::globalProgress ( int  )
signal

◆ hasUnfinishedJobs()

bool JobQueueData::hasUnfinishedJobs ( )

Definition at line 30 of file JobQueueData.cpp.

31 {
32  return m_simulations.size();
33 }

References m_simulations.

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

◆ onCancelAllJobs

void JobQueueData::onCancelAllJobs ( )
slot

Cancels all running jobs.

Definition at line 178 of file JobQueueData.cpp.

179 {
180  for (const auto& key : m_threads.keys())
181  cancelJob(key);
182 }
void cancelJob(const QString &identifier)
Cancels running job.

References cancelJob(), and m_threads.

Referenced by JobModel::onCancelAllJobs().

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:60
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:204
void updateGlobalProgress()
Estimates global progress from the progress of multiple running jobs and emmits 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("Running");
111  jobItem->setBeginTime(GUIHelpers::currentDateTime());
112  jobItem->setEndTime(QString());
113 }
QString currentDateTime()
Definition: GUIHelpers.cpp:210

References GUIHelpers::currentDateTime(), JobModel::getJobItemForIdentifier(), m_jobModel, 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 225 of file JobQueueData.cpp.

226 {
228  jobItem->setDuration(worker->simulationDuration());
229 
230  // propagating status of runner
231  if (worker->status() == "Failed") {
232  jobItem->setComments(worker->failureMessage());
233  } else {
234  // propagating simulation results
235  auto simulation = getSimulation(worker->identifier());
236  jobItem->setResults(simulation);
237  }
238  jobItem->setStatus(worker->status());
239 
240  // fixing job progress (if job was successfull, but due to wrong estimation, progress not 100%)
241  if (jobItem->isCompleted())
242  jobItem->setProgress(100);
243 }
void setComments(const QString &comments)
Definition: JobItem.cpp:194
void setResults(const ISimulation *simulation)
Definition: JobItem.cpp:229
bool isCompleted() const
Definition: JobItem.cpp:150
void setDuration(int duration)
Definition: JobItem.cpp:181
void setStatus(const QString &status)
Definition: JobItem.cpp:128
void setEndTime(const QString &end_time)
Definition: JobItem.cpp:175
QString failureMessage() const
Definition: JobWorker.cpp:86
QString identifier() const
Definition: JobWorker.cpp:30
QString status() const
Definition: JobWorker.cpp:81
int simulationDuration() const
Definition: JobWorker.cpp:91

References GUIHelpers::currentDateTime(), JobWorker::failureMessage(), getSimulation(), JobWorker::identifier(), JobItem::isCompleted(), JobItem::setComments(), JobItem::setDuration(), JobItem::setEndTime(), JobItem::setProgress(), JobItem::setResults(), JobItem::setStatus(), JobWorker::simulationDuration(), 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 37 of file JobQueueData.cpp.

38 {
39  QString identifier = jobItem->getIdentifier();
40  if (getThread(identifier))
41  return;
42 
43  if (getSimulation(identifier))
44  throw GUIHelpers::Error(
45  "JobQueueData::runJob() -> Error. ISimulation is already existing.");
46 
47  try {
49  jobItem->multiLayerItem(), 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("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 }
InstrumentItem * instrumentItem()
Definition: JobItem.cpp:224
QString getIdentifier() const
Definition: JobItem.cpp:103
SimulationOptionsItem * simulationOptionsItem()
Definition: JobItem.cpp:290
MultiLayerItem * multiLayerItem()
Definition: JobItem.cpp:219
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:24
void progressUpdate()
void start()
Definition: JobWorker.cpp:40
void finished()
void started()
std::unique_ptr< ISimulation > createSimulation(const MultiLayerItem *sampleItem, const InstrumentItem *instrumentItem, const SimulationOptionsItem *optionsItem=nullptr)
Creates domain simulation from sample and instrument items.

References DomainSimulationBuilder::createSimulation(), JobWorker::finished(), focusRequest(), JobItem::getIdentifier(), getSimulation(), getThread(), JobItem::instrumentItem(), m_simulations, m_threads, m_workers, JobItem::multiLayerItem(), onFinishedJob(), onFinishedThread(), onProgressUpdate(), onStartedJob(), JobWorker::progressUpdate(), 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 emmits appropriate signal.

Definition at line 154 of file JobQueueData.cpp.

155 {
156  int global_progress(0);
157  int nRunningJobs(0);
158  QModelIndex parentIndex;
159  for (int i_row = 0; i_row < m_jobModel->rowCount(parentIndex); ++i_row) {
160  QModelIndex itemIndex = m_jobModel->index(i_row, 0, parentIndex);
161  JobItem* jobItem = m_jobModel->getJobItemForIndex(itemIndex);
162  if (jobItem->isRunning()) {
163  global_progress += jobItem->getProgress();
164  nRunningJobs++;
165  }
166  }
167 
168  if (nRunningJobs)
169  global_progress /= nRunningJobs;
170  else
171  global_progress = -1;
172 
173  emit globalProgress(global_progress);
174 }
int getProgress() const
Definition: JobItem.cpp:199
bool isRunning() const
Definition: JobItem.cpp:145
const JobItem * getJobItemForIndex(const QModelIndex &index) const
Definition: JobModel.cpp:46
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const
virtual int rowCount(const QModelIndex &parent) const

References JobModel::getJobItemForIndex(), JobItem::getProgress(), globalProgress(), SessionModel::index(), JobItem::isRunning(), m_jobModel, and SessionModel::rowCount().

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: