Mercurial > hg > orthanc
annotate OrthancServer/Scheduler/ServerScheduler.cpp @ 1565:4b23310eb7e8
add tags per instances in a series
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 21 Aug 2015 17:29:16 +0200 |
parents | 33d34bc4ac15 |
children | 0a2ad4a6858f |
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 | |
33 #include "ServerScheduler.h" | |
34 | |
35 #include "../../Core/OrthancException.h" | |
1486
f967bdf8534e
refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1453
diff
changeset
|
36 #include "../../Core/Logging.h" |
781 | 37 |
38 namespace Orthanc | |
39 { | |
40 namespace | |
41 { | |
42 // Anonymous namespace to avoid clashes between compilation modules | |
1000
13e230bbd882
rename filter to command
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
999
diff
changeset
|
43 class Sink : public IServerCommand |
781 | 44 { |
45 private: | |
46 ListOfStrings& target_; | |
47 | |
48 public: | |
49 Sink(ListOfStrings& target) : target_(target) | |
50 { | |
51 } | |
52 | |
53 virtual bool Apply(ListOfStrings& outputs, | |
54 const ListOfStrings& inputs) | |
55 { | |
56 for (ListOfStrings::const_iterator | |
1304 | 57 it = inputs.begin(); it != inputs.end(); ++it) |
781 | 58 { |
59 target_.push_back(*it); | |
60 } | |
61 | |
62 return true; | |
63 } | |
64 }; | |
65 } | |
66 | |
67 | |
68 ServerScheduler::JobInfo& ServerScheduler::GetJobInfo(const std::string& jobId) | |
69 { | |
70 Jobs::iterator info = jobs_.find(jobId); | |
71 | |
72 if (info == jobs_.end()) | |
73 { | |
74 throw OrthancException(ErrorCode_InternalError); | |
75 } | |
76 | |
77 return info->second; | |
78 } | |
79 | |
80 | |
81 void ServerScheduler::SignalSuccess(const std::string& jobId) | |
82 { | |
83 boost::mutex::scoped_lock lock(mutex_); | |
84 | |
85 JobInfo& info = GetJobInfo(jobId); | |
86 info.success_++; | |
87 | |
88 assert(info.failures_ == 0); | |
89 | |
90 if (info.success_ >= info.size_) | |
91 { | |
92 if (info.watched_) | |
93 { | |
94 watchedJobStatus_[jobId] = JobStatus_Success; | |
995
8c67382f44a7
limit number of jobs in the scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
783
diff
changeset
|
95 watchedJobFinished_.notify_all(); |
781 | 96 } |
97 | |
98 LOG(INFO) << "Job successfully finished (" << info.description_ << ")"; | |
99 jobs_.erase(jobId); | |
995
8c67382f44a7
limit number of jobs in the scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
783
diff
changeset
|
100 |
8c67382f44a7
limit number of jobs in the scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
783
diff
changeset
|
101 availableJob_.Release(); |
781 | 102 } |
103 } | |
104 | |
105 | |
106 void ServerScheduler::SignalFailure(const std::string& jobId) | |
107 { | |
108 boost::mutex::scoped_lock lock(mutex_); | |
109 | |
110 JobInfo& info = GetJobInfo(jobId); | |
111 info.failures_++; | |
112 | |
113 if (info.success_ + info.failures_ >= info.size_) | |
114 { | |
115 if (info.watched_) | |
116 { | |
117 watchedJobStatus_[jobId] = JobStatus_Failure; | |
995
8c67382f44a7
limit number of jobs in the scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
783
diff
changeset
|
118 watchedJobFinished_.notify_all(); |
781 | 119 } |
120 | |
121 LOG(ERROR) << "Job has failed (" << info.description_ << ")"; | |
122 jobs_.erase(jobId); | |
995
8c67382f44a7
limit number of jobs in the scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
783
diff
changeset
|
123 |
8c67382f44a7
limit number of jobs in the scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
783
diff
changeset
|
124 availableJob_.Release(); |
781 | 125 } |
126 } | |
127 | |
128 | |
129 void ServerScheduler::Worker(ServerScheduler* that) | |
130 { | |
131 static const int32_t TIMEOUT = 100; | |
132 | |
783
c9cdd53a6b31
main scheduler added to the server context
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
781
diff
changeset
|
133 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
|
134 |
781 | 135 while (!that->finish_) |
136 { | |
137 std::auto_ptr<IDynamicObject> object(that->queue_.Dequeue(TIMEOUT)); | |
138 if (object.get() != NULL) | |
139 { | |
1000
13e230bbd882
rename filter to command
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
999
diff
changeset
|
140 ServerCommandInstance& filter = dynamic_cast<ServerCommandInstance&>(*object); |
781 | 141 |
142 // Skip the execution of this filter if its parent job has | |
143 // previously failed. | |
144 bool jobHasFailed; | |
145 { | |
146 boost::mutex::scoped_lock lock(that->mutex_); | |
147 JobInfo& info = that->GetJobInfo(filter.GetJobId()); | |
148 jobHasFailed = (info.failures_ > 0 || info.cancel_); | |
149 } | |
150 | |
151 if (jobHasFailed) | |
152 { | |
153 that->SignalFailure(filter.GetJobId()); | |
154 } | |
155 else | |
156 { | |
157 filter.Execute(*that); | |
158 } | |
159 } | |
160 } | |
161 } | |
162 | |
163 | |
164 void ServerScheduler::SubmitInternal(ServerJob& job, | |
165 bool watched) | |
166 { | |
995
8c67382f44a7
limit number of jobs in the scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
783
diff
changeset
|
167 availableJob_.Acquire(); |
8c67382f44a7
limit number of jobs in the scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
783
diff
changeset
|
168 |
781 | 169 boost::mutex::scoped_lock lock(mutex_); |
170 | |
171 JobInfo info; | |
172 info.size_ = job.Submit(queue_, *this); | |
173 info.cancel_ = false; | |
174 info.success_ = 0; | |
175 info.failures_ = 0; | |
176 info.description_ = job.GetDescription(); | |
177 info.watched_ = watched; | |
178 | |
179 assert(info.size_ > 0); | |
180 | |
181 if (watched) | |
182 { | |
183 watchedJobStatus_[job.GetId()] = JobStatus_Running; | |
184 } | |
185 | |
186 jobs_[job.GetId()] = info; | |
187 | |
188 LOG(INFO) << "New job submitted (" << job.description_ << ")"; | |
189 } | |
190 | |
191 | |
995
8c67382f44a7
limit number of jobs in the scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
783
diff
changeset
|
192 ServerScheduler::ServerScheduler(unsigned int maxJobs) : availableJob_(maxJobs) |
781 | 193 { |
194 finish_ = false; | |
195 worker_ = boost::thread(Worker, this); | |
196 } | |
197 | |
198 | |
199 ServerScheduler::~ServerScheduler() | |
200 { | |
1453
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
201 if (!finish_) |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
202 { |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
203 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
|
204 Stop(); |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
205 } |
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 void ServerScheduler::Stop() |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
210 { |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
211 if (!finish_) |
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 finish_ = true; |
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 if (worker_.joinable()) |
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 worker_.join(); |
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 } |
781 | 220 } |
221 | |
222 | |
223 void ServerScheduler::Submit(ServerJob& job) | |
224 { | |
225 if (job.filters_.empty()) | |
226 { | |
227 return; | |
228 } | |
229 | |
230 SubmitInternal(job, false); | |
231 } | |
232 | |
233 | |
234 bool ServerScheduler::SubmitAndWait(ListOfStrings& outputs, | |
235 ServerJob& job) | |
236 { | |
237 std::string jobId = job.GetId(); | |
238 | |
239 outputs.clear(); | |
240 | |
241 if (job.filters_.empty()) | |
242 { | |
243 return true; | |
244 } | |
245 | |
246 // Add a sink filter to collect all the results of the filters | |
247 // that have no next filter. | |
1000
13e230bbd882
rename filter to command
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
999
diff
changeset
|
248 ServerCommandInstance& sink = job.AddCommand(new Sink(outputs)); |
781 | 249 |
1000
13e230bbd882
rename filter to command
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
999
diff
changeset
|
250 for (std::list<ServerCommandInstance*>::iterator |
1304 | 251 it = job.filters_.begin(); it != job.filters_.end(); ++it) |
781 | 252 { |
253 if ((*it) != &sink && | |
1009
26642cecd36d
clearer job interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1000
diff
changeset
|
254 (*it)->IsConnectedToSink()) |
781 | 255 { |
1009
26642cecd36d
clearer job interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1000
diff
changeset
|
256 (*it)->ConnectOutput(sink); |
781 | 257 } |
258 } | |
259 | |
260 // Submit the job | |
261 SubmitInternal(job, true); | |
262 | |
263 // Wait for the job to complete (either success or failure) | |
264 JobStatus status; | |
265 | |
266 { | |
267 boost::mutex::scoped_lock lock(mutex_); | |
268 | |
269 assert(watchedJobStatus_.find(jobId) != watchedJobStatus_.end()); | |
270 | |
271 while (watchedJobStatus_[jobId] == JobStatus_Running) | |
272 { | |
995
8c67382f44a7
limit number of jobs in the scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
783
diff
changeset
|
273 watchedJobFinished_.wait(lock); |
781 | 274 } |
275 | |
276 status = watchedJobStatus_[jobId]; | |
277 watchedJobStatus_.erase(jobId); | |
278 } | |
279 | |
280 return (status == JobStatus_Success); | |
281 } | |
282 | |
283 | |
999 | 284 bool ServerScheduler::SubmitAndWait(ServerJob& job) |
285 { | |
286 ListOfStrings ignoredSink; | |
287 return SubmitAndWait(ignoredSink, job); | |
288 } | |
289 | |
290 | |
781 | 291 bool ServerScheduler::IsRunning(const std::string& jobId) |
292 { | |
293 boost::mutex::scoped_lock lock(mutex_); | |
294 return jobs_.find(jobId) != jobs_.end(); | |
295 } | |
296 | |
297 | |
298 void ServerScheduler::Cancel(const std::string& jobId) | |
299 { | |
300 boost::mutex::scoped_lock lock(mutex_); | |
301 | |
302 Jobs::iterator job = jobs_.find(jobId); | |
303 | |
304 if (job != jobs_.end()) | |
305 { | |
306 job->second.cancel_ = true; | |
307 LOG(WARNING) << "Canceling a job (" << job->second.description_ << ")"; | |
308 } | |
309 } | |
310 | |
311 | |
312 float ServerScheduler::GetProgress(const std::string& jobId) | |
313 { | |
314 boost::mutex::scoped_lock lock(mutex_); | |
315 | |
316 Jobs::iterator job = jobs_.find(jobId); | |
317 | |
318 if (job == jobs_.end() || | |
319 job->second.size_ == 0 /* should never happen */) | |
320 { | |
321 // This job is not running | |
322 return 1; | |
323 } | |
324 | |
325 if (job->second.failures_ != 0) | |
326 { | |
327 return 1; | |
328 } | |
329 | |
330 if (job->second.size_ == 1) | |
331 { | |
1545 | 332 return static_cast<float>(job->second.success_); |
781 | 333 } |
334 | |
335 return (static_cast<float>(job->second.success_) / | |
336 static_cast<float>(job->second.size_ - 1)); | |
337 } | |
338 | |
339 | |
340 void ServerScheduler::GetListOfJobs(ListOfStrings& jobs) | |
341 { | |
342 boost::mutex::scoped_lock lock(mutex_); | |
343 | |
344 jobs.clear(); | |
345 | |
346 for (Jobs::const_iterator | |
1304 | 347 it = jobs_.begin(); it != jobs_.end(); ++it) |
781 | 348 { |
349 jobs.push_back(it->first); | |
350 } | |
351 } | |
352 } |