Mercurial > hg > orthanc
annotate OrthancServer/Scheduler/ServerScheduler.cpp @ 1666:d7039be97eeb
better command-line options handling
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 30 Sep 2015 14:04:25 +0200 |
parents | 0a2ad4a6858f |
children | b1291df2f780 |
rev | line source |
---|---|
781 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1009
diff
changeset
|
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics |
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 { |
195 finish_ = false; | |
196 worker_ = boost::thread(Worker, this); | |
197 } | |
198 | |
199 | |
200 ServerScheduler::~ServerScheduler() | |
201 { | |
1453
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
202 if (!finish_) |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
203 { |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
204 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
|
205 Stop(); |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
206 } |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
207 } |
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 |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
210 void ServerScheduler::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 if (!finish_) |
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 finish_ = true; |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
215 |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
216 if (worker_.joinable()) |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
217 { |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
218 worker_.join(); |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
219 } |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
220 } |
781 | 221 } |
222 | |
223 | |
224 void ServerScheduler::Submit(ServerJob& job) | |
225 { | |
226 if (job.filters_.empty()) | |
227 { | |
228 return; | |
229 } | |
230 | |
231 SubmitInternal(job, false); | |
232 } | |
233 | |
234 | |
235 bool ServerScheduler::SubmitAndWait(ListOfStrings& outputs, | |
236 ServerJob& job) | |
237 { | |
238 std::string jobId = job.GetId(); | |
239 | |
240 outputs.clear(); | |
241 | |
242 if (job.filters_.empty()) | |
243 { | |
244 return true; | |
245 } | |
246 | |
247 // Add a sink filter to collect all the results of the filters | |
248 // that have no next filter. | |
1000
13e230bbd882
rename filter to command
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
999
diff
changeset
|
249 ServerCommandInstance& sink = job.AddCommand(new Sink(outputs)); |
781 | 250 |
1000
13e230bbd882
rename filter to command
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
999
diff
changeset
|
251 for (std::list<ServerCommandInstance*>::iterator |
1304 | 252 it = job.filters_.begin(); it != job.filters_.end(); ++it) |
781 | 253 { |
254 if ((*it) != &sink && | |
1009
26642cecd36d
clearer job interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1000
diff
changeset
|
255 (*it)->IsConnectedToSink()) |
781 | 256 { |
1009
26642cecd36d
clearer job interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1000
diff
changeset
|
257 (*it)->ConnectOutput(sink); |
781 | 258 } |
259 } | |
260 | |
261 // Submit the job | |
262 SubmitInternal(job, true); | |
263 | |
264 // Wait for the job to complete (either success or failure) | |
265 JobStatus status; | |
266 | |
267 { | |
268 boost::mutex::scoped_lock lock(mutex_); | |
269 | |
270 assert(watchedJobStatus_.find(jobId) != watchedJobStatus_.end()); | |
271 | |
272 while (watchedJobStatus_[jobId] == JobStatus_Running) | |
273 { | |
995
8c67382f44a7
limit number of jobs in the scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
783
diff
changeset
|
274 watchedJobFinished_.wait(lock); |
781 | 275 } |
276 | |
277 status = watchedJobStatus_[jobId]; | |
278 watchedJobStatus_.erase(jobId); | |
279 } | |
280 | |
281 return (status == JobStatus_Success); | |
282 } | |
283 | |
284 | |
999 | 285 bool ServerScheduler::SubmitAndWait(ServerJob& job) |
286 { | |
287 ListOfStrings ignoredSink; | |
288 return SubmitAndWait(ignoredSink, job); | |
289 } | |
290 | |
291 | |
781 | 292 bool ServerScheduler::IsRunning(const std::string& jobId) |
293 { | |
294 boost::mutex::scoped_lock lock(mutex_); | |
295 return jobs_.find(jobId) != jobs_.end(); | |
296 } | |
297 | |
298 | |
299 void ServerScheduler::Cancel(const std::string& jobId) | |
300 { | |
301 boost::mutex::scoped_lock lock(mutex_); | |
302 | |
303 Jobs::iterator job = jobs_.find(jobId); | |
304 | |
305 if (job != jobs_.end()) | |
306 { | |
307 job->second.cancel_ = true; | |
308 LOG(WARNING) << "Canceling a job (" << job->second.description_ << ")"; | |
309 } | |
310 } | |
311 | |
312 | |
313 float ServerScheduler::GetProgress(const std::string& jobId) | |
314 { | |
315 boost::mutex::scoped_lock lock(mutex_); | |
316 | |
317 Jobs::iterator job = jobs_.find(jobId); | |
318 | |
319 if (job == jobs_.end() || | |
320 job->second.size_ == 0 /* should never happen */) | |
321 { | |
322 // This job is not running | |
323 return 1; | |
324 } | |
325 | |
326 if (job->second.failures_ != 0) | |
327 { | |
328 return 1; | |
329 } | |
330 | |
331 if (job->second.size_ == 1) | |
332 { | |
1545 | 333 return static_cast<float>(job->second.success_); |
781 | 334 } |
335 | |
336 return (static_cast<float>(job->second.success_) / | |
337 static_cast<float>(job->second.size_ - 1)); | |
338 } | |
339 | |
340 | |
341 void ServerScheduler::GetListOfJobs(ListOfStrings& jobs) | |
342 { | |
343 boost::mutex::scoped_lock lock(mutex_); | |
344 | |
345 jobs.clear(); | |
346 | |
347 for (Jobs::const_iterator | |
1304 | 348 it = jobs_.begin(); it != jobs_.end(); ++it) |
781 | 349 { |
350 jobs.push_back(it->first); | |
351 } | |
352 } | |
353 } |