Mercurial > hg > orthanc
annotate Core/JobsEngine/JobsEngine.cpp @ 2585:4c809711149e jobs
reorganization
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 14 May 2018 21:24:51 +0200 |
parents | 8da2cffc2378 |
children | 441f23af9d89 |
rev | line source |
---|---|
2569 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
11 * | |
12 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
18 * modify file(s) with this exception, you may extend this exception to | |
19 * your version of the file(s), but you are not obligated to do so. If | |
20 * you do not wish to do so, delete this exception statement from your | |
21 * version. If you delete this exception statement from all source files | |
22 * in the program, then also delete it here. | |
23 * | |
24 * This program is distributed in the hope that it will be useful, but | |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
33 | |
34 #include "../PrecompiledHeaders.h" | |
35 #include "JobsEngine.h" | |
36 | |
37 #include "JobStepRetry.h" | |
38 | |
39 #include "../Logging.h" | |
40 #include "../OrthancException.h" | |
41 | |
42 namespace Orthanc | |
43 { | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
44 bool JobsEngine::IsRunning() |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
45 { |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
46 boost::mutex::scoped_lock lock(stateMutex_); |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
47 return (state_ == State_Running); |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
48 } |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
49 |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
50 |
2569 | 51 bool JobsEngine::ExecuteStep(JobsRegistry::RunningJob& running, |
52 size_t workerIndex) | |
53 { | |
54 assert(running.IsValid()); | |
55 | |
56 if (running.IsPauseScheduled()) | |
57 { | |
58 running.GetJob().ReleaseResources(); | |
59 running.MarkPause(); | |
60 return false; | |
61 } | |
62 | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
63 if (running.IsCancelScheduled()) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
64 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
65 running.GetJob().ReleaseResources(); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
66 running.MarkCanceled(); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
67 return false; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
68 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
69 |
2569 | 70 std::auto_ptr<JobStepResult> result; |
71 | |
72 { | |
73 try | |
74 { | |
75 result.reset(running.GetJob().ExecuteStep()); | |
76 | |
77 if (result->GetCode() == JobStepCode_Failure) | |
78 { | |
79 running.UpdateStatus(ErrorCode_InternalError); | |
80 } | |
81 else | |
82 { | |
83 running.UpdateStatus(ErrorCode_Success); | |
84 } | |
85 } | |
86 catch (OrthancException& e) | |
87 { | |
88 running.UpdateStatus(e.GetErrorCode()); | |
89 } | |
90 catch (boost::bad_lexical_cast&) | |
91 { | |
92 running.UpdateStatus(ErrorCode_BadFileFormat); | |
93 } | |
94 catch (...) | |
95 { | |
96 running.UpdateStatus(ErrorCode_InternalError); | |
97 } | |
98 | |
99 if (result.get() == NULL) | |
100 { | |
101 result.reset(new JobStepResult(JobStepCode_Failure)); | |
102 } | |
103 } | |
104 | |
105 switch (result->GetCode()) | |
106 { | |
107 case JobStepCode_Success: | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
108 running.GetJob().ReleaseResources(); |
2569 | 109 running.MarkSuccess(); |
110 return false; | |
111 | |
112 case JobStepCode_Failure: | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
113 running.GetJob().ReleaseResources(); |
2569 | 114 running.MarkFailure(); |
115 return false; | |
116 | |
117 case JobStepCode_Retry: | |
2570
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
118 running.GetJob().ReleaseResources(); |
2569 | 119 running.MarkRetry(dynamic_cast<JobStepRetry&>(*result).GetTimeout()); |
120 return false; | |
121 | |
122 case JobStepCode_Continue: | |
123 return true; | |
124 | |
125 default: | |
126 throw OrthancException(ErrorCode_InternalError); | |
127 } | |
128 } | |
129 | |
130 | |
131 void JobsEngine::RetryHandler(JobsEngine* engine) | |
132 { | |
133 assert(engine != NULL); | |
134 | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
135 while (engine->IsRunning()) |
2569 | 136 { |
137 boost::this_thread::sleep(boost::posix_time::milliseconds(200)); | |
138 engine->GetRegistry().ScheduleRetries(); | |
139 } | |
140 } | |
141 | |
142 | |
143 void JobsEngine::Worker(JobsEngine* engine, | |
144 size_t workerIndex) | |
145 { | |
146 assert(engine != NULL); | |
147 | |
148 LOG(INFO) << "Worker thread " << workerIndex << " has started"; | |
149 | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
150 while (engine->IsRunning()) |
2569 | 151 { |
152 JobsRegistry::RunningJob running(engine->GetRegistry(), 100); | |
153 | |
154 if (running.IsValid()) | |
155 { | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
156 LOG(INFO) << "Executing job with priority " << running.GetPriority() |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
157 << " in worker thread " << workerIndex << ": " << running.GetId(); |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
158 |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
159 while (engine->IsRunning()) |
2569 | 160 { |
161 if (!engine->ExecuteStep(running, workerIndex)) | |
162 { | |
163 break; | |
164 } | |
165 } | |
166 } | |
167 } | |
168 } | |
169 | |
170 | |
171 JobsEngine::JobsEngine() : | |
172 state_(State_Setup), | |
173 workers_(1) | |
174 { | |
175 } | |
176 | |
177 | |
178 JobsEngine::~JobsEngine() | |
179 { | |
180 if (state_ != State_Setup && | |
181 state_ != State_Done) | |
182 { | |
183 LOG(ERROR) << "INTERNAL ERROR: JobsEngine::Stop() should be invoked manually to avoid mess in the destruction order!"; | |
184 Stop(); | |
185 } | |
186 } | |
187 | |
188 | |
189 void JobsEngine::SetWorkersCount(size_t count) | |
190 { | |
191 boost::mutex::scoped_lock lock(stateMutex_); | |
192 | |
193 if (state_ != State_Setup) | |
194 { | |
195 // Can only be invoked before calling "Start()" | |
196 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
197 } | |
198 | |
199 workers_.resize(count); | |
200 } | |
201 | |
202 | |
203 void JobsEngine::Start() | |
204 { | |
205 boost::mutex::scoped_lock lock(stateMutex_); | |
206 | |
207 if (state_ != State_Setup) | |
208 { | |
209 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
210 } | |
211 | |
212 retryHandler_ = boost::thread(RetryHandler, this); | |
213 | |
2570
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
214 if (workers_.size() == 0) |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
215 { |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
216 // Use all the available CPUs |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
217 size_t n = boost::thread::hardware_concurrency(); |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
218 |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
219 if (n == 0) |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
220 { |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
221 n = 1; |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
222 } |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
223 |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
224 workers_.resize(n); |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
225 } |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
226 |
2569 | 227 for (size_t i = 0; i < workers_.size(); i++) |
228 { | |
229 workers_[i] = boost::thread(Worker, this, i); | |
230 } | |
231 | |
232 state_ = State_Running; | |
233 | |
2570
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
234 LOG(WARNING) << "The jobs engine has started with " << workers_.size() << " threads"; |
2569 | 235 } |
236 | |
237 | |
238 void JobsEngine::Stop() | |
239 { | |
240 { | |
241 boost::mutex::scoped_lock lock(stateMutex_); | |
242 | |
243 if (state_ != State_Running) | |
244 { | |
245 return; | |
246 } | |
247 | |
248 state_ = State_Stopping; | |
249 } | |
250 | |
251 LOG(INFO) << "Stopping the jobs engine"; | |
252 | |
253 if (retryHandler_.joinable()) | |
254 { | |
255 retryHandler_.join(); | |
256 } | |
257 | |
258 for (size_t i = 0; i < workers_.size(); i++) | |
259 { | |
260 if (workers_[i].joinable()) | |
261 { | |
262 workers_[i].join(); | |
263 } | |
264 } | |
265 | |
266 { | |
267 boost::mutex::scoped_lock lock(stateMutex_); | |
268 state_ = State_Done; | |
269 } | |
270 | |
271 LOG(WARNING) << "The jobs engine has stopped"; | |
272 } | |
273 } |