Mercurial > hg > orthanc
annotate OrthancServer/Scheduler/ServerScheduler.cpp @ 2115:a657f7772e69
Handling of private tags/creators in the "Dictionary" configuration option
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 31 Oct 2016 15:23:32 +0100 |
parents | ed383e7a6753 |
children | 29689b30f9ae |
rev | line source |
---|---|
781 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1009
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
781 | 5 * |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
10 * | |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
22 * | |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
1624
0a2ad4a6858f
fix missing precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1545
diff
changeset
|
33 #include "../PrecompiledHeadersServer.h" |
781 | 34 #include "ServerScheduler.h" |
35 | |
36 #include "../../Core/OrthancException.h" | |
1486
f967bdf8534e
refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1453
diff
changeset
|
37 #include "../../Core/Logging.h" |
781 | 38 |
39 namespace Orthanc | |
40 { | |
41 namespace | |
42 { | |
43 // Anonymous namespace to avoid clashes between compilation modules | |
1000
13e230bbd882
rename filter to command
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
999
diff
changeset
|
44 class Sink : public IServerCommand |
781 | 45 { |
46 private: | |
47 ListOfStrings& target_; | |
48 | |
49 public: | |
50 Sink(ListOfStrings& target) : target_(target) | |
51 { | |
52 } | |
53 | |
54 virtual bool Apply(ListOfStrings& outputs, | |
55 const ListOfStrings& inputs) | |
56 { | |
57 for (ListOfStrings::const_iterator | |
1304 | 58 it = inputs.begin(); it != inputs.end(); ++it) |
781 | 59 { |
60 target_.push_back(*it); | |
61 } | |
62 | |
63 return true; | |
64 } | |
65 }; | |
66 } | |
67 | |
68 | |
69 ServerScheduler::JobInfo& ServerScheduler::GetJobInfo(const std::string& jobId) | |
70 { | |
71 Jobs::iterator info = jobs_.find(jobId); | |
72 | |
73 if (info == jobs_.end()) | |
74 { | |
75 throw OrthancException(ErrorCode_InternalError); | |
76 } | |
77 | |
78 return info->second; | |
79 } | |
80 | |
81 | |
82 void ServerScheduler::SignalSuccess(const std::string& jobId) | |
83 { | |
84 boost::mutex::scoped_lock lock(mutex_); | |
85 | |
86 JobInfo& info = GetJobInfo(jobId); | |
87 info.success_++; | |
88 | |
89 assert(info.failures_ == 0); | |
90 | |
91 if (info.success_ >= info.size_) | |
92 { | |
93 if (info.watched_) | |
94 { | |
95 watchedJobStatus_[jobId] = JobStatus_Success; | |
995
8c67382f44a7
limit number of jobs in the scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
783
diff
changeset
|
96 watchedJobFinished_.notify_all(); |
781 | 97 } |
98 | |
99 LOG(INFO) << "Job successfully finished (" << info.description_ << ")"; | |
100 jobs_.erase(jobId); | |
995
8c67382f44a7
limit number of jobs in the scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
783
diff
changeset
|
101 |
8c67382f44a7
limit number of jobs in the scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
783
diff
changeset
|
102 availableJob_.Release(); |
781 | 103 } |
104 } | |
105 | |
106 | |
107 void ServerScheduler::SignalFailure(const std::string& jobId) | |
108 { | |
109 boost::mutex::scoped_lock lock(mutex_); | |
110 | |
111 JobInfo& info = GetJobInfo(jobId); | |
112 info.failures_++; | |
113 | |
114 if (info.success_ + info.failures_ >= info.size_) | |
115 { | |
116 if (info.watched_) | |
117 { | |
118 watchedJobStatus_[jobId] = JobStatus_Failure; | |
995
8c67382f44a7
limit number of jobs in the scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
783
diff
changeset
|
119 watchedJobFinished_.notify_all(); |
781 | 120 } |
121 | |
122 LOG(ERROR) << "Job has failed (" << info.description_ << ")"; | |
123 jobs_.erase(jobId); | |
995
8c67382f44a7
limit number of jobs in the scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
783
diff
changeset
|
124 |
8c67382f44a7
limit number of jobs in the scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
783
diff
changeset
|
125 availableJob_.Release(); |
781 | 126 } |
127 } | |
128 | |
129 | |
130 void ServerScheduler::Worker(ServerScheduler* that) | |
131 { | |
132 static const int32_t TIMEOUT = 100; | |
133 | |
783
c9cdd53a6b31
main scheduler added to the server context
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
781
diff
changeset
|
134 LOG(WARNING) << "The server scheduler has started"; |
c9cdd53a6b31
main scheduler added to the server context
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
781
diff
changeset
|
135 |
781 | 136 while (!that->finish_) |
137 { | |
138 std::auto_ptr<IDynamicObject> object(that->queue_.Dequeue(TIMEOUT)); | |
139 if (object.get() != NULL) | |
140 { | |
1000
13e230bbd882
rename filter to command
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
999
diff
changeset
|
141 ServerCommandInstance& filter = dynamic_cast<ServerCommandInstance&>(*object); |
781 | 142 |
143 // Skip the execution of this filter if its parent job has | |
144 // previously failed. | |
145 bool jobHasFailed; | |
146 { | |
147 boost::mutex::scoped_lock lock(that->mutex_); | |
148 JobInfo& info = that->GetJobInfo(filter.GetJobId()); | |
149 jobHasFailed = (info.failures_ > 0 || info.cancel_); | |
150 } | |
151 | |
152 if (jobHasFailed) | |
153 { | |
154 that->SignalFailure(filter.GetJobId()); | |
155 } | |
156 else | |
157 { | |
158 filter.Execute(*that); | |
159 } | |
160 } | |
161 } | |
162 } | |
163 | |
164 | |
165 void ServerScheduler::SubmitInternal(ServerJob& job, | |
166 bool watched) | |
167 { | |
995
8c67382f44a7
limit number of jobs in the scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
783
diff
changeset
|
168 availableJob_.Acquire(); |
8c67382f44a7
limit number of jobs in the scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
783
diff
changeset
|
169 |
781 | 170 boost::mutex::scoped_lock lock(mutex_); |
171 | |
172 JobInfo info; | |
173 info.size_ = job.Submit(queue_, *this); | |
174 info.cancel_ = false; | |
175 info.success_ = 0; | |
176 info.failures_ = 0; | |
177 info.description_ = job.GetDescription(); | |
178 info.watched_ = watched; | |
179 | |
180 assert(info.size_ > 0); | |
181 | |
182 if (watched) | |
183 { | |
184 watchedJobStatus_[job.GetId()] = JobStatus_Running; | |
185 } | |
186 | |
187 jobs_[job.GetId()] = info; | |
188 | |
189 LOG(INFO) << "New job submitted (" << job.description_ << ")"; | |
190 } | |
191 | |
192 | |
995
8c67382f44a7
limit number of jobs in the scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
783
diff
changeset
|
193 ServerScheduler::ServerScheduler(unsigned int maxJobs) : availableJob_(maxJobs) |
781 | 194 { |
2063 | 195 if (maxJobs == 0) |
196 { | |
197 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
198 } | |
199 | |
781 | 200 finish_ = false; |
201 worker_ = boost::thread(Worker, this); | |
202 } | |
203 | |
204 | |
205 ServerScheduler::~ServerScheduler() | |
206 { | |
1453
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
207 if (!finish_) |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
208 { |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
209 LOG(ERROR) << "INTERNAL ERROR: ServerScheduler::Finalize() should be invoked manually to avoid mess in the destruction order!"; |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
210 Stop(); |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
211 } |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
212 } |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
213 |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
214 |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
215 void ServerScheduler::Stop() |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
216 { |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
217 if (!finish_) |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
218 { |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
219 finish_ = true; |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
220 |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
221 if (worker_.joinable()) |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
222 { |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
223 worker_.join(); |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
224 } |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
225 } |
781 | 226 } |
227 | |
228 | |
229 void ServerScheduler::Submit(ServerJob& job) | |
230 { | |
231 if (job.filters_.empty()) | |
232 { | |
233 return; | |
234 } | |
235 | |
236 SubmitInternal(job, false); | |
237 } | |
238 | |
239 | |
240 bool ServerScheduler::SubmitAndWait(ListOfStrings& outputs, | |
241 ServerJob& job) | |
242 { | |
243 std::string jobId = job.GetId(); | |
244 | |
245 outputs.clear(); | |
246 | |
247 if (job.filters_.empty()) | |
248 { | |
249 return true; | |
250 } | |
251 | |
252 // Add a sink filter to collect all the results of the filters | |
253 // that have no next filter. | |
1000
13e230bbd882
rename filter to command
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
999
diff
changeset
|
254 ServerCommandInstance& sink = job.AddCommand(new Sink(outputs)); |
781 | 255 |
1000
13e230bbd882
rename filter to command
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
999
diff
changeset
|
256 for (std::list<ServerCommandInstance*>::iterator |
1304 | 257 it = job.filters_.begin(); it != job.filters_.end(); ++it) |
781 | 258 { |
259 if ((*it) != &sink && | |
1009
26642cecd36d
clearer job interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1000
diff
changeset
|
260 (*it)->IsConnectedToSink()) |
781 | 261 { |
1009
26642cecd36d
clearer job interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1000
diff
changeset
|
262 (*it)->ConnectOutput(sink); |
781 | 263 } |
264 } | |
265 | |
266 // Submit the job | |
267 SubmitInternal(job, true); | |
268 | |
269 // Wait for the job to complete (either success or failure) | |
270 JobStatus status; | |
271 | |
272 { | |
273 boost::mutex::scoped_lock lock(mutex_); | |
274 | |
275 assert(watchedJobStatus_.find(jobId) != watchedJobStatus_.end()); | |
276 | |
277 while (watchedJobStatus_[jobId] == JobStatus_Running) | |
278 { | |
995
8c67382f44a7
limit number of jobs in the scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
783
diff
changeset
|
279 watchedJobFinished_.wait(lock); |
781 | 280 } |
281 | |
282 status = watchedJobStatus_[jobId]; | |
283 watchedJobStatus_.erase(jobId); | |
284 } | |
285 | |
286 return (status == JobStatus_Success); | |
287 } | |
288 | |
289 | |
999 | 290 bool ServerScheduler::SubmitAndWait(ServerJob& job) |
291 { | |
292 ListOfStrings ignoredSink; | |
293 return SubmitAndWait(ignoredSink, job); | |
294 } | |
295 | |
296 | |
781 | 297 bool ServerScheduler::IsRunning(const std::string& jobId) |
298 { | |
299 boost::mutex::scoped_lock lock(mutex_); | |
300 return jobs_.find(jobId) != jobs_.end(); | |
301 } | |
302 | |
303 | |
304 void ServerScheduler::Cancel(const std::string& jobId) | |
305 { | |
306 boost::mutex::scoped_lock lock(mutex_); | |
307 | |
308 Jobs::iterator job = jobs_.find(jobId); | |
309 | |
310 if (job != jobs_.end()) | |
311 { | |
312 job->second.cancel_ = true; | |
313 LOG(WARNING) << "Canceling a job (" << job->second.description_ << ")"; | |
314 } | |
315 } | |
316 | |
317 | |
318 float ServerScheduler::GetProgress(const std::string& jobId) | |
319 { | |
320 boost::mutex::scoped_lock lock(mutex_); | |
321 | |
322 Jobs::iterator job = jobs_.find(jobId); | |
323 | |
324 if (job == jobs_.end() || | |
325 job->second.size_ == 0 /* should never happen */) | |
326 { | |
327 // This job is not running | |
328 return 1; | |
329 } | |
330 | |
331 if (job->second.failures_ != 0) | |
332 { | |
333 return 1; | |
334 } | |
335 | |
336 if (job->second.size_ == 1) | |
337 { | |
1545 | 338 return static_cast<float>(job->second.success_); |
781 | 339 } |
340 | |
341 return (static_cast<float>(job->second.success_) / | |
342 static_cast<float>(job->second.size_ - 1)); | |
343 } | |
344 | |
345 | |
346 void ServerScheduler::GetListOfJobs(ListOfStrings& jobs) | |
347 { | |
348 boost::mutex::scoped_lock lock(mutex_); | |
349 | |
350 jobs.clear(); | |
351 | |
352 for (Jobs::const_iterator | |
1304 | 353 it = jobs_.begin(); it != jobs_.end(); ++it) |
781 | 354 { |
355 jobs.push_back(it->first); | |
356 } | |
357 } | |
358 } |