Mercurial > hg > orthanc
annotate OrthancFramework/Sources/JobsEngine/JobsEngine.cpp @ 4667:08ee98bf103e
"/jobs/..." has new field "ErrorDetails" to help identify the cause of an error
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 27 May 2021 15:59:03 +0200 |
parents | d9473bd5ed43 |
children | 7053502fbf97 |
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 | |
4437
d9473bd5ed43
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4392
diff
changeset
|
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium |
2569 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
8 * modify it under the terms of the GNU Lesser General Public License |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
9 * as published by the Free Software Foundation, either version 3 of |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
10 * the License, or (at your option) any later version. |
2569 | 11 * |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
15 * Lesser General Public License for more details. |
2569 | 16 * |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
17 * You should have received a copy of the GNU Lesser General Public |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
18 * License along with this program. If not, see |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
19 * <http://www.gnu.org/licenses/>. |
2569 | 20 **/ |
21 | |
22 | |
23 #include "../PrecompiledHeaders.h" | |
24 #include "JobsEngine.h" | |
25 | |
26 #include "../Logging.h" | |
27 #include "../OrthancException.h" | |
4392
3af1d763763a
confining Json::Reader and Json::*Writer into Toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4295
diff
changeset
|
28 #include "../Toolbox.h" |
2569 | 29 |
2668
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
30 |
2569 | 31 namespace Orthanc |
32 { | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
33 bool JobsEngine::IsRunning() |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
34 { |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
35 boost::mutex::scoped_lock lock(stateMutex_); |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
36 return (state_ == State_Running); |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
37 } |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
38 |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
39 |
2569 | 40 bool JobsEngine::ExecuteStep(JobsRegistry::RunningJob& running, |
41 size_t workerIndex) | |
42 { | |
43 assert(running.IsValid()); | |
44 | |
45 if (running.IsPauseScheduled()) | |
46 { | |
2812
ea7aea6f6a95
improved naming of methods in IJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2811
diff
changeset
|
47 running.GetJob().Stop(JobStopReason_Paused); |
2569 | 48 running.MarkPause(); |
49 return false; | |
50 } | |
51 | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
52 if (running.IsCancelScheduled()) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
53 { |
2812
ea7aea6f6a95
improved naming of methods in IJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2811
diff
changeset
|
54 running.GetJob().Stop(JobStopReason_Canceled); |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
55 running.MarkCanceled(); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
56 return false; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
57 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
58 |
2598
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
59 JobStepResult result; |
2569 | 60 |
2598
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
61 try |
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
62 { |
3658
2d90dd30858c
providing job ID to the IJob::Step() methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
63 result = running.GetJob().Step(running.GetId()); |
2598
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
64 } |
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
65 catch (OrthancException& e) |
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
66 { |
3240
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
67 result = JobStepResult::Failure(e); |
2598
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
68 } |
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
69 catch (boost::bad_lexical_cast&) |
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
70 { |
3240
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
71 result = JobStepResult::Failure(ErrorCode_BadFileFormat, NULL); |
2598
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
72 } |
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
73 catch (...) |
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
74 { |
3240
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
75 result = JobStepResult::Failure(ErrorCode_InternalError, NULL); |
2569 | 76 } |
77 | |
2598
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
78 switch (result.GetCode()) |
2569 | 79 { |
80 case JobStepCode_Success: | |
2812
ea7aea6f6a95
improved naming of methods in IJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2811
diff
changeset
|
81 running.GetJob().Stop(JobStopReason_Success); |
3240
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
82 running.UpdateStatus(ErrorCode_Success, ""); |
2569 | 83 running.MarkSuccess(); |
84 return false; | |
85 | |
86 case JobStepCode_Failure: | |
2812
ea7aea6f6a95
improved naming of methods in IJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2811
diff
changeset
|
87 running.GetJob().Stop(JobStopReason_Failure); |
3240
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
88 running.UpdateStatus(result.GetFailureCode(), result.GetFailureDetails()); |
2569 | 89 running.MarkFailure(); |
90 return false; | |
91 | |
92 case JobStepCode_Retry: | |
2812
ea7aea6f6a95
improved naming of methods in IJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2811
diff
changeset
|
93 running.GetJob().Stop(JobStopReason_Retry); |
3240
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
94 running.UpdateStatus(ErrorCode_Success, ""); |
2598
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2591
diff
changeset
|
95 running.MarkRetry(result.GetRetryTimeout()); |
2569 | 96 return false; |
97 | |
98 case JobStepCode_Continue: | |
3240
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
99 running.UpdateStatus(ErrorCode_Success, ""); |
2569 | 100 return true; |
101 | |
102 default: | |
103 throw OrthancException(ErrorCode_InternalError); | |
104 } | |
105 } | |
106 | |
107 | |
108 void JobsEngine::RetryHandler(JobsEngine* engine) | |
109 { | |
110 assert(engine != NULL); | |
111 | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
112 while (engine->IsRunning()) |
2569 | 113 { |
2665
389d050a2e66
fix deadlock, speed up unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2598
diff
changeset
|
114 boost::this_thread::sleep(boost::posix_time::milliseconds(engine->threadSleep_)); |
2569 | 115 engine->GetRegistry().ScheduleRetries(); |
116 } | |
117 } | |
118 | |
119 | |
120 void JobsEngine::Worker(JobsEngine* engine, | |
121 size_t workerIndex) | |
122 { | |
123 assert(engine != NULL); | |
124 | |
4295 | 125 CLOG(INFO, JOBS) << "Worker thread " << workerIndex << " has started"; |
2569 | 126 |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
127 while (engine->IsRunning()) |
2569 | 128 { |
2665
389d050a2e66
fix deadlock, speed up unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2598
diff
changeset
|
129 JobsRegistry::RunningJob running(engine->GetRegistry(), engine->threadSleep_); |
2569 | 130 |
131 if (running.IsValid()) | |
132 { | |
4295 | 133 CLOG(INFO, JOBS) << "Executing job with priority " << running.GetPriority() |
134 << " in worker thread " << workerIndex << ": " << running.GetId(); | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
135 |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
136 while (engine->IsRunning()) |
2569 | 137 { |
138 if (!engine->ExecuteStep(running, workerIndex)) | |
139 { | |
140 break; | |
141 } | |
142 } | |
143 } | |
144 } | |
145 } | |
146 | |
147 | |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
148 JobsEngine::JobsEngine(size_t maxCompletedJobs) : |
2569 | 149 state_(State_Setup), |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
150 registry_(new JobsRegistry(maxCompletedJobs)), |
2665
389d050a2e66
fix deadlock, speed up unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2598
diff
changeset
|
151 threadSleep_(200), |
2569 | 152 workers_(1) |
153 { | |
154 } | |
155 | |
156 | |
157 JobsEngine::~JobsEngine() | |
158 { | |
159 if (state_ != State_Setup && | |
160 state_ != State_Done) | |
161 { | |
4295 | 162 CLOG(ERROR, JOBS) << "INTERNAL ERROR: JobsEngine::Stop() should be invoked manually to avoid mess in the destruction order!"; |
2569 | 163 Stop(); |
164 } | |
165 } | |
166 | |
2668
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
167 |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
168 JobsRegistry& JobsEngine::GetRegistry() |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
169 { |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
170 if (registry_.get() == NULL) |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
171 { |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
172 throw OrthancException(ErrorCode_InternalError); |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
173 } |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
174 |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
175 return *registry_; |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
176 } |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
177 |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
178 |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
179 void JobsEngine::LoadRegistryFromJson(IJobUnserializer& unserializer, |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
180 const Json::Value& serialized) |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
181 { |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
182 boost::mutex::scoped_lock lock(stateMutex_); |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
183 |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
184 if (state_ != State_Setup) |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
185 { |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
186 // Can only be invoked before calling "Start()" |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
187 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
188 } |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
189 |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
190 assert(registry_.get() != NULL); |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
191 const size_t maxCompletedJobs = registry_->GetMaxCompletedJobs(); |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
192 registry_.reset(new JobsRegistry(unserializer, serialized, maxCompletedJobs)); |
2668
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
193 } |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
194 |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
195 |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
196 void JobsEngine::LoadRegistryFromString(IJobUnserializer& unserializer, |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
197 const std::string& serialized) |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
198 { |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
199 Json::Value value; |
4392
3af1d763763a
confining Json::Reader and Json::*Writer into Toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4295
diff
changeset
|
200 if (Toolbox::ReadJson(value, serialized)) |
2668
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
201 { |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
202 LoadRegistryFromJson(unserializer, value); |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
203 } |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
204 else |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
205 { |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
206 throw OrthancException(ErrorCode_BadFileFormat); |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
207 } |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
208 } |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
209 |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2665
diff
changeset
|
210 |
2569 | 211 void JobsEngine::SetWorkersCount(size_t count) |
212 { | |
213 boost::mutex::scoped_lock lock(stateMutex_); | |
214 | |
215 if (state_ != State_Setup) | |
216 { | |
217 // Can only be invoked before calling "Start()" | |
218 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
219 } | |
220 | |
221 workers_.resize(count); | |
222 } | |
2665
389d050a2e66
fix deadlock, speed up unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2598
diff
changeset
|
223 |
389d050a2e66
fix deadlock, speed up unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2598
diff
changeset
|
224 |
389d050a2e66
fix deadlock, speed up unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2598
diff
changeset
|
225 void JobsEngine::SetThreadSleep(unsigned int sleep) |
389d050a2e66
fix deadlock, speed up unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2598
diff
changeset
|
226 { |
389d050a2e66
fix deadlock, speed up unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2598
diff
changeset
|
227 boost::mutex::scoped_lock lock(stateMutex_); |
389d050a2e66
fix deadlock, speed up unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2598
diff
changeset
|
228 |
389d050a2e66
fix deadlock, speed up unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2598
diff
changeset
|
229 if (state_ != State_Setup) |
389d050a2e66
fix deadlock, speed up unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2598
diff
changeset
|
230 { |
389d050a2e66
fix deadlock, speed up unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2598
diff
changeset
|
231 // Can only be invoked before calling "Start()" |
389d050a2e66
fix deadlock, speed up unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2598
diff
changeset
|
232 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
389d050a2e66
fix deadlock, speed up unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2598
diff
changeset
|
233 } |
389d050a2e66
fix deadlock, speed up unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2598
diff
changeset
|
234 |
389d050a2e66
fix deadlock, speed up unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2598
diff
changeset
|
235 threadSleep_ = sleep; |
389d050a2e66
fix deadlock, speed up unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2598
diff
changeset
|
236 } |
389d050a2e66
fix deadlock, speed up unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2598
diff
changeset
|
237 |
2569 | 238 |
239 void JobsEngine::Start() | |
240 { | |
241 boost::mutex::scoped_lock lock(stateMutex_); | |
242 | |
243 if (state_ != State_Setup) | |
244 { | |
245 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
246 } | |
247 | |
248 retryHandler_ = boost::thread(RetryHandler, this); | |
249 | |
2570
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
250 if (workers_.size() == 0) |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
251 { |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
252 // Use all the available CPUs |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
253 size_t n = boost::thread::hardware_concurrency(); |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
254 |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
255 if (n == 0) |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
256 { |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
257 n = 1; |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
258 } |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
259 |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
260 workers_.resize(n); |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
261 } |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
262 |
2569 | 263 for (size_t i = 0; i < workers_.size(); i++) |
264 { | |
2591
441f23af9d89
fix for older releases of boost::thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2581
diff
changeset
|
265 assert(workers_[i] == NULL); |
441f23af9d89
fix for older releases of boost::thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2581
diff
changeset
|
266 workers_[i] = new boost::thread(Worker, this, i); |
2569 | 267 } |
268 | |
269 state_ = State_Running; | |
270 | |
4295 | 271 CLOG(WARNING, JOBS) << "The jobs engine has started with " << workers_.size() << " threads"; |
2569 | 272 } |
273 | |
274 | |
275 void JobsEngine::Stop() | |
276 { | |
277 { | |
278 boost::mutex::scoped_lock lock(stateMutex_); | |
279 | |
280 if (state_ != State_Running) | |
281 { | |
282 return; | |
283 } | |
284 | |
285 state_ = State_Stopping; | |
286 } | |
287 | |
4295 | 288 CLOG(INFO, JOBS) << "Stopping the jobs engine"; |
2569 | 289 |
290 if (retryHandler_.joinable()) | |
291 { | |
292 retryHandler_.join(); | |
293 } | |
294 | |
295 for (size_t i = 0; i < workers_.size(); i++) | |
296 { | |
2591
441f23af9d89
fix for older releases of boost::thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2581
diff
changeset
|
297 assert(workers_[i] != NULL); |
441f23af9d89
fix for older releases of boost::thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2581
diff
changeset
|
298 |
441f23af9d89
fix for older releases of boost::thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2581
diff
changeset
|
299 if (workers_[i]->joinable()) |
2569 | 300 { |
2591
441f23af9d89
fix for older releases of boost::thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2581
diff
changeset
|
301 workers_[i]->join(); |
2569 | 302 } |
2591
441f23af9d89
fix for older releases of boost::thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2581
diff
changeset
|
303 |
441f23af9d89
fix for older releases of boost::thread
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2581
diff
changeset
|
304 delete workers_[i]; |
2569 | 305 } |
306 | |
307 { | |
308 boost::mutex::scoped_lock lock(stateMutex_); | |
309 state_ = State_Done; | |
310 } | |
311 | |
4295 | 312 CLOG(WARNING, JOBS) << "The jobs engine has stopped"; |
2569 | 313 } |
314 } |