Mercurial > hg > orthanc
annotate Core/JobsEngine/JobsEngine.cpp @ 2620:1232922c8793 jobs
speeding up shutdown if Lua script is in trailing phase
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 22 May 2018 14:08:57 +0200 |
parents | 34dc57f4a7d2 |
children | 389d050a2e66 |
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 "../Logging.h" | |
38 #include "../OrthancException.h" | |
39 | |
40 namespace Orthanc | |
41 { | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
42 bool JobsEngine::IsRunning() |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
43 { |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
44 boost::mutex::scoped_lock lock(stateMutex_); |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
45 return (state_ == State_Running); |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
46 } |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
47 |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
48 |
2569 | 49 bool JobsEngine::ExecuteStep(JobsRegistry::RunningJob& running, |
50 size_t workerIndex) | |
51 { | |
52 assert(running.IsValid()); | |
53 | |
54 if (running.IsPauseScheduled()) | |
55 { | |
56 running.GetJob().ReleaseResources(); | |
57 running.MarkPause(); | |
58 return false; | |
59 } | |
60 | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
61 if (running.IsCancelScheduled()) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
62 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
63 running.GetJob().ReleaseResources(); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
64 running.MarkCanceled(); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
65 return false; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
66 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
67 |
2598
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
68 JobStepResult result; |
2569 | 69 |
2598
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
70 try |
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
71 { |
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
72 result = running.GetJob().ExecuteStep(); |
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
73 } |
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
74 catch (OrthancException& e) |
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
75 { |
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
76 result = JobStepResult::Failure(e.GetErrorCode()); |
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
77 } |
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
78 catch (boost::bad_lexical_cast&) |
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
79 { |
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
80 result = JobStepResult::Failure(ErrorCode_BadFileFormat); |
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
81 } |
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
82 catch (...) |
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
83 { |
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
84 result = JobStepResult::Failure(ErrorCode_InternalError); |
2569 | 85 } |
86 | |
2598
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
87 switch (result.GetCode()) |
2569 | 88 { |
89 case JobStepCode_Success: | |
2598
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
90 running.UpdateStatus(ErrorCode_Success); |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
91 running.GetJob().ReleaseResources(); |
2569 | 92 running.MarkSuccess(); |
93 return false; | |
94 | |
95 case JobStepCode_Failure: | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
96 running.GetJob().ReleaseResources(); |
2598
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
97 running.UpdateStatus(result.GetFailureCode()); |
2569 | 98 running.MarkFailure(); |
99 return false; | |
100 | |
101 case JobStepCode_Retry: | |
2570
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
102 running.GetJob().ReleaseResources(); |
2598
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
103 running.UpdateStatus(ErrorCode_Success); |
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
104 running.MarkRetry(result.GetRetryTimeout()); |
2569 | 105 return false; |
106 | |
107 case JobStepCode_Continue: | |
2598
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
108 running.UpdateStatus(ErrorCode_Success); |
2569 | 109 return true; |
110 | |
111 default: | |
112 throw OrthancException(ErrorCode_InternalError); | |
113 } | |
114 } | |
115 | |
116 | |
117 void JobsEngine::RetryHandler(JobsEngine* engine) | |
118 { | |
119 assert(engine != NULL); | |
120 | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
121 while (engine->IsRunning()) |
2569 | 122 { |
123 boost::this_thread::sleep(boost::posix_time::milliseconds(200)); | |
124 engine->GetRegistry().ScheduleRetries(); | |
125 } | |
126 } | |
127 | |
128 | |
129 void JobsEngine::Worker(JobsEngine* engine, | |
130 size_t workerIndex) | |
131 { | |
132 assert(engine != NULL); | |
133 | |
134 LOG(INFO) << "Worker thread " << workerIndex << " has started"; | |
135 | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
136 while (engine->IsRunning()) |
2569 | 137 { |
138 JobsRegistry::RunningJob running(engine->GetRegistry(), 100); | |
139 | |
140 if (running.IsValid()) | |
141 { | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
142 LOG(INFO) << "Executing job with priority " << running.GetPriority() |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
143 << " in worker thread " << workerIndex << ": " << running.GetId(); |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
144 |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
145 while (engine->IsRunning()) |
2569 | 146 { |
147 if (!engine->ExecuteStep(running, workerIndex)) | |
148 { | |
149 break; | |
150 } | |
151 } | |
152 } | |
153 } | |
154 } | |
155 | |
156 | |
157 JobsEngine::JobsEngine() : | |
158 state_(State_Setup), | |
159 workers_(1) | |
160 { | |
161 } | |
162 | |
163 | |
164 JobsEngine::~JobsEngine() | |
165 { | |
166 if (state_ != State_Setup && | |
167 state_ != State_Done) | |
168 { | |
169 LOG(ERROR) << "INTERNAL ERROR: JobsEngine::Stop() should be invoked manually to avoid mess in the destruction order!"; | |
170 Stop(); | |
171 } | |
172 } | |
173 | |
174 | |
175 void JobsEngine::SetWorkersCount(size_t count) | |
176 { | |
177 boost::mutex::scoped_lock lock(stateMutex_); | |
178 | |
179 if (state_ != State_Setup) | |
180 { | |
181 // Can only be invoked before calling "Start()" | |
182 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
183 } | |
184 | |
185 workers_.resize(count); | |
186 } | |
187 | |
188 | |
189 void JobsEngine::Start() | |
190 { | |
191 boost::mutex::scoped_lock lock(stateMutex_); | |
192 | |
193 if (state_ != State_Setup) | |
194 { | |
195 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
196 } | |
197 | |
198 retryHandler_ = boost::thread(RetryHandler, this); | |
199 | |
2570
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
200 if (workers_.size() == 0) |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
201 { |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
202 // Use all the available CPUs |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
203 size_t n = boost::thread::hardware_concurrency(); |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
204 |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
205 if (n == 0) |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
206 { |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
207 n = 1; |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
208 } |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
209 |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
210 workers_.resize(n); |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
211 } |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
212 |
2569 | 213 for (size_t i = 0; i < workers_.size(); i++) |
214 { | |
2591
441f23af9d89
fix for older releases of boost::thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2581
diff
changeset
|
215 assert(workers_[i] == NULL); |
441f23af9d89
fix for older releases of boost::thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2581
diff
changeset
|
216 workers_[i] = new boost::thread(Worker, this, i); |
2569 | 217 } |
218 | |
219 state_ = State_Running; | |
220 | |
2570
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
221 LOG(WARNING) << "The jobs engine has started with " << workers_.size() << " threads"; |
2569 | 222 } |
223 | |
224 | |
225 void JobsEngine::Stop() | |
226 { | |
227 { | |
228 boost::mutex::scoped_lock lock(stateMutex_); | |
229 | |
230 if (state_ != State_Running) | |
231 { | |
232 return; | |
233 } | |
234 | |
235 state_ = State_Stopping; | |
236 } | |
237 | |
238 LOG(INFO) << "Stopping the jobs engine"; | |
239 | |
240 if (retryHandler_.joinable()) | |
241 { | |
242 retryHandler_.join(); | |
243 } | |
244 | |
245 for (size_t i = 0; i < workers_.size(); i++) | |
246 { | |
2591
441f23af9d89
fix for older releases of boost::thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2581
diff
changeset
|
247 assert(workers_[i] != NULL); |
441f23af9d89
fix for older releases of boost::thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2581
diff
changeset
|
248 |
441f23af9d89
fix for older releases of boost::thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2581
diff
changeset
|
249 if (workers_[i]->joinable()) |
2569 | 250 { |
2591
441f23af9d89
fix for older releases of boost::thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2581
diff
changeset
|
251 workers_[i]->join(); |
2569 | 252 } |
2591
441f23af9d89
fix for older releases of boost::thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2581
diff
changeset
|
253 |
441f23af9d89
fix for older releases of boost::thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2581
diff
changeset
|
254 delete workers_[i]; |
2569 | 255 } |
256 | |
257 { | |
258 boost::mutex::scoped_lock lock(stateMutex_); | |
259 state_ = State_Done; | |
260 } | |
261 | |
262 LOG(WARNING) << "The jobs engine has stopped"; | |
263 } | |
264 } |