Mercurial > hg > orthanc
comparison OrthancFramework/Sources/JobsEngine/JobsEngine.cpp @ 4044:d25f4c0fa160 framework
splitting code into OrthancFramework and OrthancServer
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 10 Jun 2020 20:30:34 +0200 |
parents | Core/JobsEngine/JobsEngine.cpp@2d90dd30858c |
children | bf7b9edf6b81 |
comparison
equal
deleted
inserted
replaced
4043:6c6239aec462 | 4044:d25f4c0fa160 |
---|---|
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-2020 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 "../Logging.h" | |
38 #include "../OrthancException.h" | |
39 | |
40 #include <json/reader.h> | |
41 | |
42 namespace Orthanc | |
43 { | |
44 bool JobsEngine::IsRunning() | |
45 { | |
46 boost::mutex::scoped_lock lock(stateMutex_); | |
47 return (state_ == State_Running); | |
48 } | |
49 | |
50 | |
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().Stop(JobStopReason_Paused); | |
59 running.MarkPause(); | |
60 return false; | |
61 } | |
62 | |
63 if (running.IsCancelScheduled()) | |
64 { | |
65 running.GetJob().Stop(JobStopReason_Canceled); | |
66 running.MarkCanceled(); | |
67 return false; | |
68 } | |
69 | |
70 JobStepResult result; | |
71 | |
72 try | |
73 { | |
74 result = running.GetJob().Step(running.GetId()); | |
75 } | |
76 catch (OrthancException& e) | |
77 { | |
78 result = JobStepResult::Failure(e); | |
79 } | |
80 catch (boost::bad_lexical_cast&) | |
81 { | |
82 result = JobStepResult::Failure(ErrorCode_BadFileFormat, NULL); | |
83 } | |
84 catch (...) | |
85 { | |
86 result = JobStepResult::Failure(ErrorCode_InternalError, NULL); | |
87 } | |
88 | |
89 switch (result.GetCode()) | |
90 { | |
91 case JobStepCode_Success: | |
92 running.GetJob().Stop(JobStopReason_Success); | |
93 running.UpdateStatus(ErrorCode_Success, ""); | |
94 running.MarkSuccess(); | |
95 return false; | |
96 | |
97 case JobStepCode_Failure: | |
98 running.GetJob().Stop(JobStopReason_Failure); | |
99 running.UpdateStatus(result.GetFailureCode(), result.GetFailureDetails()); | |
100 running.MarkFailure(); | |
101 return false; | |
102 | |
103 case JobStepCode_Retry: | |
104 running.GetJob().Stop(JobStopReason_Retry); | |
105 running.UpdateStatus(ErrorCode_Success, ""); | |
106 running.MarkRetry(result.GetRetryTimeout()); | |
107 return false; | |
108 | |
109 case JobStepCode_Continue: | |
110 running.UpdateStatus(ErrorCode_Success, ""); | |
111 return true; | |
112 | |
113 default: | |
114 throw OrthancException(ErrorCode_InternalError); | |
115 } | |
116 } | |
117 | |
118 | |
119 void JobsEngine::RetryHandler(JobsEngine* engine) | |
120 { | |
121 assert(engine != NULL); | |
122 | |
123 while (engine->IsRunning()) | |
124 { | |
125 boost::this_thread::sleep(boost::posix_time::milliseconds(engine->threadSleep_)); | |
126 engine->GetRegistry().ScheduleRetries(); | |
127 } | |
128 } | |
129 | |
130 | |
131 void JobsEngine::Worker(JobsEngine* engine, | |
132 size_t workerIndex) | |
133 { | |
134 assert(engine != NULL); | |
135 | |
136 LOG(INFO) << "Worker thread " << workerIndex << " has started"; | |
137 | |
138 while (engine->IsRunning()) | |
139 { | |
140 JobsRegistry::RunningJob running(engine->GetRegistry(), engine->threadSleep_); | |
141 | |
142 if (running.IsValid()) | |
143 { | |
144 LOG(INFO) << "Executing job with priority " << running.GetPriority() | |
145 << " in worker thread " << workerIndex << ": " << running.GetId(); | |
146 | |
147 while (engine->IsRunning()) | |
148 { | |
149 if (!engine->ExecuteStep(running, workerIndex)) | |
150 { | |
151 break; | |
152 } | |
153 } | |
154 } | |
155 } | |
156 } | |
157 | |
158 | |
159 JobsEngine::JobsEngine(size_t maxCompletedJobs) : | |
160 state_(State_Setup), | |
161 registry_(new JobsRegistry(maxCompletedJobs)), | |
162 threadSleep_(200), | |
163 workers_(1) | |
164 { | |
165 } | |
166 | |
167 | |
168 JobsEngine::~JobsEngine() | |
169 { | |
170 if (state_ != State_Setup && | |
171 state_ != State_Done) | |
172 { | |
173 LOG(ERROR) << "INTERNAL ERROR: JobsEngine::Stop() should be invoked manually to avoid mess in the destruction order!"; | |
174 Stop(); | |
175 } | |
176 } | |
177 | |
178 | |
179 JobsRegistry& JobsEngine::GetRegistry() | |
180 { | |
181 if (registry_.get() == NULL) | |
182 { | |
183 throw OrthancException(ErrorCode_InternalError); | |
184 } | |
185 | |
186 return *registry_; | |
187 } | |
188 | |
189 | |
190 void JobsEngine::LoadRegistryFromJson(IJobUnserializer& unserializer, | |
191 const Json::Value& serialized) | |
192 { | |
193 boost::mutex::scoped_lock lock(stateMutex_); | |
194 | |
195 if (state_ != State_Setup) | |
196 { | |
197 // Can only be invoked before calling "Start()" | |
198 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
199 } | |
200 | |
201 assert(registry_.get() != NULL); | |
202 const size_t maxCompletedJobs = registry_->GetMaxCompletedJobs(); | |
203 registry_.reset(new JobsRegistry(unserializer, serialized, maxCompletedJobs)); | |
204 } | |
205 | |
206 | |
207 void JobsEngine::LoadRegistryFromString(IJobUnserializer& unserializer, | |
208 const std::string& serialized) | |
209 { | |
210 Json::Value value; | |
211 Json::Reader reader; | |
212 if (reader.parse(serialized, value)) | |
213 { | |
214 LoadRegistryFromJson(unserializer, value); | |
215 } | |
216 else | |
217 { | |
218 throw OrthancException(ErrorCode_BadFileFormat); | |
219 } | |
220 } | |
221 | |
222 | |
223 void JobsEngine::SetWorkersCount(size_t count) | |
224 { | |
225 boost::mutex::scoped_lock lock(stateMutex_); | |
226 | |
227 if (state_ != State_Setup) | |
228 { | |
229 // Can only be invoked before calling "Start()" | |
230 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
231 } | |
232 | |
233 workers_.resize(count); | |
234 } | |
235 | |
236 | |
237 void JobsEngine::SetThreadSleep(unsigned int sleep) | |
238 { | |
239 boost::mutex::scoped_lock lock(stateMutex_); | |
240 | |
241 if (state_ != State_Setup) | |
242 { | |
243 // Can only be invoked before calling "Start()" | |
244 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
245 } | |
246 | |
247 threadSleep_ = sleep; | |
248 } | |
249 | |
250 | |
251 void JobsEngine::Start() | |
252 { | |
253 boost::mutex::scoped_lock lock(stateMutex_); | |
254 | |
255 if (state_ != State_Setup) | |
256 { | |
257 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
258 } | |
259 | |
260 retryHandler_ = boost::thread(RetryHandler, this); | |
261 | |
262 if (workers_.size() == 0) | |
263 { | |
264 // Use all the available CPUs | |
265 size_t n = boost::thread::hardware_concurrency(); | |
266 | |
267 if (n == 0) | |
268 { | |
269 n = 1; | |
270 } | |
271 | |
272 workers_.resize(n); | |
273 } | |
274 | |
275 for (size_t i = 0; i < workers_.size(); i++) | |
276 { | |
277 assert(workers_[i] == NULL); | |
278 workers_[i] = new boost::thread(Worker, this, i); | |
279 } | |
280 | |
281 state_ = State_Running; | |
282 | |
283 LOG(WARNING) << "The jobs engine has started with " << workers_.size() << " threads"; | |
284 } | |
285 | |
286 | |
287 void JobsEngine::Stop() | |
288 { | |
289 { | |
290 boost::mutex::scoped_lock lock(stateMutex_); | |
291 | |
292 if (state_ != State_Running) | |
293 { | |
294 return; | |
295 } | |
296 | |
297 state_ = State_Stopping; | |
298 } | |
299 | |
300 LOG(INFO) << "Stopping the jobs engine"; | |
301 | |
302 if (retryHandler_.joinable()) | |
303 { | |
304 retryHandler_.join(); | |
305 } | |
306 | |
307 for (size_t i = 0; i < workers_.size(); i++) | |
308 { | |
309 assert(workers_[i] != NULL); | |
310 | |
311 if (workers_[i]->joinable()) | |
312 { | |
313 workers_[i]->join(); | |
314 } | |
315 | |
316 delete workers_[i]; | |
317 } | |
318 | |
319 { | |
320 boost::mutex::scoped_lock lock(stateMutex_); | |
321 state_ = State_Done; | |
322 } | |
323 | |
324 LOG(WARNING) << "The jobs engine has stopped"; | |
325 } | |
326 } |