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