Mercurial > hg > orthanc
annotate OrthancFramework/Sources/JobsEngine/JobsRegistry.cpp @ 4427:30efda56500d
trying to enable TLS support in DCMTK
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 31 Dec 2020 15:16:18 +0100 |
parents | 44b53a2c0a13 |
children | d9473bd5ed43 |
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 | |
3640
94f4a18a79cc
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3371
diff
changeset
|
5 * Copyright (C) 2017-2020 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. |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
11 * |
2569 | 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 "JobsRegistry.h" | |
25 | |
26 #include "../Logging.h" | |
27 #include "../OrthancException.h" | |
28 #include "../Toolbox.h" | |
2656
a6d3e45eeff5
SerializationToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2655
diff
changeset
|
29 #include "../SerializationToolbox.h" |
2569 | 30 |
31 namespace Orthanc | |
32 { | |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
33 static const char* STATE = "State"; |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
34 static const char* TYPE = "Type"; |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
35 static const char* PRIORITY = "Priority"; |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
36 static const char* JOB = "Job"; |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
37 static const char* JOBS = "Jobs"; |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
38 static const char* JOBS_REGISTRY = "JobsRegistry"; |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
39 static const char* CREATION_TIME = "CreationTime"; |
2669
eaf10085ffa1
no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2668
diff
changeset
|
40 static const char* LAST_CHANGE_TIME = "LastChangeTime"; |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
41 static const char* RUNTIME = "Runtime"; |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
42 |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
43 |
2569 | 44 class JobsRegistry::JobHandler : public boost::noncopyable |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
45 { |
2569 | 46 private: |
47 std::string id_; | |
48 JobState state_; | |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
49 std::string jobType_; |
3712
2a170a8f1faf
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
50 std::unique_ptr<IJob> job_; |
2569 | 51 int priority_; // "+inf()" means highest priority |
52 boost::posix_time::ptime creationTime_; | |
53 boost::posix_time::ptime lastStateChangeTime_; | |
54 boost::posix_time::time_duration runtime_; | |
55 boost::posix_time::ptime retryTime_; | |
56 bool pauseScheduled_; | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
57 bool cancelScheduled_; |
2569 | 58 JobStatus lastStatus_; |
59 | |
60 void Touch() | |
61 { | |
62 const boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time(); | |
63 | |
64 if (state_ == JobState_Running) | |
65 { | |
66 runtime_ += (now - lastStateChangeTime_); | |
67 } | |
68 | |
69 lastStateChangeTime_ = now; | |
70 } | |
71 | |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
72 void SetStateInternal(JobState state) |
2569 | 73 { |
74 state_ = state; | |
75 pauseScheduled_ = false; | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
76 cancelScheduled_ = false; |
2569 | 77 Touch(); |
78 } | |
79 | |
80 public: | |
81 JobHandler(IJob* job, | |
82 int priority) : | |
83 id_(Toolbox::GenerateUuid()), | |
84 state_(JobState_Pending), | |
85 job_(job), | |
86 priority_(priority), | |
87 creationTime_(boost::posix_time::microsec_clock::universal_time()), | |
88 lastStateChangeTime_(creationTime_), | |
89 runtime_(boost::posix_time::milliseconds(0)), | |
90 retryTime_(creationTime_), | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
91 pauseScheduled_(false), |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
92 cancelScheduled_(false) |
2569 | 93 { |
94 if (job == NULL) | |
95 { | |
96 throw OrthancException(ErrorCode_NullPointer); | |
97 } | |
98 | |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
99 job->GetJobType(jobType_); |
2668
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2667
diff
changeset
|
100 job->Start(); |
2630
00327e989458
creating archives/medias is now a job
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2620
diff
changeset
|
101 |
3240
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3191
diff
changeset
|
102 lastStatus_ = JobStatus(ErrorCode_Success, "", *job_); |
2569 | 103 } |
104 | |
105 const std::string& GetId() const | |
106 { | |
107 return id_; | |
108 } | |
109 | |
110 IJob& GetJob() const | |
111 { | |
112 assert(job_.get() != NULL); | |
113 return *job_; | |
114 } | |
115 | |
116 void SetPriority(int priority) | |
117 { | |
118 priority_ = priority; | |
119 } | |
120 | |
121 int GetPriority() const | |
122 { | |
123 return priority_; | |
124 } | |
125 | |
126 JobState GetState() const | |
127 { | |
128 return state_; | |
129 } | |
130 | |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
131 void SetState(JobState state) |
2569 | 132 { |
133 if (state == JobState_Retry) | |
134 { | |
135 // Use "SetRetryState()" | |
136 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
137 } | |
138 else | |
139 { | |
140 SetStateInternal(state); | |
141 } | |
142 } | |
143 | |
144 void SetRetryState(unsigned int timeout) | |
145 { | |
146 if (state_ == JobState_Running) | |
147 { | |
148 SetStateInternal(JobState_Retry); | |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
149 retryTime_ = (boost::posix_time::microsec_clock::universal_time() + |
2569 | 150 boost::posix_time::milliseconds(timeout)); |
151 } | |
152 else | |
153 { | |
154 // Only valid for running jobs | |
155 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
156 } | |
157 } | |
158 | |
159 void SchedulePause() | |
160 { | |
161 if (state_ == JobState_Running) | |
162 { | |
163 pauseScheduled_ = true; | |
164 } | |
165 else | |
166 { | |
167 // Only valid for running jobs | |
168 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
169 } | |
170 } | |
171 | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
172 void ScheduleCancel() |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
173 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
174 if (state_ == JobState_Running) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
175 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
176 cancelScheduled_ = true; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
177 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
178 else |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
179 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
180 // Only valid for running jobs |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
181 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
182 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
183 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
184 |
2569 | 185 bool IsPauseScheduled() |
186 { | |
187 return pauseScheduled_; | |
188 } | |
189 | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
190 bool IsCancelScheduled() |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
191 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
192 return cancelScheduled_; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
193 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
194 |
2569 | 195 bool IsRetryReady(const boost::posix_time::ptime& now) const |
196 { | |
197 if (state_ != JobState_Retry) | |
198 { | |
199 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
200 } | |
201 else | |
202 { | |
203 return retryTime_ <= now; | |
204 } | |
205 } | |
206 | |
207 const boost::posix_time::ptime& GetCreationTime() const | |
208 { | |
209 return creationTime_; | |
210 } | |
211 | |
212 const boost::posix_time::ptime& GetLastStateChangeTime() const | |
213 { | |
214 return lastStateChangeTime_; | |
215 } | |
216 | |
2669
eaf10085ffa1
no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2668
diff
changeset
|
217 void SetLastStateChangeTime(const boost::posix_time::ptime& time) |
eaf10085ffa1
no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2668
diff
changeset
|
218 { |
eaf10085ffa1
no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2668
diff
changeset
|
219 lastStateChangeTime_ = time; |
eaf10085ffa1
no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2668
diff
changeset
|
220 } |
eaf10085ffa1
no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2668
diff
changeset
|
221 |
2569 | 222 const boost::posix_time::time_duration& GetRuntime() const |
223 { | |
224 return runtime_; | |
225 } | |
226 | |
227 const JobStatus& GetLastStatus() const | |
228 { | |
229 return lastStatus_; | |
230 } | |
231 | |
232 void SetLastStatus(const JobStatus& status) | |
233 { | |
234 lastStatus_ = status; | |
235 Touch(); | |
236 } | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
237 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
238 void SetLastErrorCode(ErrorCode code) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
239 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
240 lastStatus_.SetErrorCode(code); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
241 } |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
242 |
2663
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
243 bool Serialize(Json::Value& target) const |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
244 { |
2655 | 245 target = Json::objectValue; |
2663
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
246 |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
247 bool ok; |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
248 |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
249 if (state_ == JobState_Running) |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
250 { |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
251 // WARNING: Cannot directly access the "job_" member, as long |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
252 // as a "RunningJob" instance is running. We do not use a |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
253 // mutex at the "JobHandler" level, as serialization would be |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
254 // blocked while a step in the job is running. Instead, we |
2976
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
255 // save a snapshot of the serialized job. (*) |
2663
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
256 |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
257 if (lastStatus_.HasSerialized()) |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
258 { |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
259 target[JOB] = lastStatus_.GetSerialized(); |
2663
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
260 ok = true; |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
261 } |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
262 else |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
263 { |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
264 ok = false; |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
265 } |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
266 } |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
267 else |
2663
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
268 { |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
269 ok = job_->Serialize(target[JOB]); |
2663
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
270 } |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
271 |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
272 if (ok) |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
273 { |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
274 target[STATE] = EnumerationToString(state_); |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
275 target[PRIORITY] = priority_; |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
276 target[CREATION_TIME] = boost::posix_time::to_iso_string(creationTime_); |
2669
eaf10085ffa1
no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2668
diff
changeset
|
277 target[LAST_CHANGE_TIME] = boost::posix_time::to_iso_string(lastStateChangeTime_); |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
278 target[RUNTIME] = static_cast<unsigned int>(runtime_.total_milliseconds()); |
2663
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
279 return true; |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
280 } |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
281 else |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
282 { |
4268
0ae2ca210077
new macro TLOG() to replace VLOG() for trace logs with a category
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4203
diff
changeset
|
283 LOG(TRACE) << "Job backup is not supported for job of type: " << jobType_; |
2663
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
284 return false; |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
285 } |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
286 } |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
287 |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
288 JobHandler(IJobUnserializer& unserializer, |
2670
c5646f766b3e
serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
289 const Json::Value& serialized, |
c5646f766b3e
serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
290 const std::string& id) : |
c5646f766b3e
serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
291 id_(id), |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
292 pauseScheduled_(false), |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
293 cancelScheduled_(false) |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
294 { |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
295 state_ = StringToJobState(SerializationToolbox::ReadString(serialized, STATE)); |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
296 priority_ = SerializationToolbox::ReadInteger(serialized, PRIORITY); |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
297 creationTime_ = boost::posix_time::from_iso_string |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
298 (SerializationToolbox::ReadString(serialized, CREATION_TIME)); |
2669
eaf10085ffa1
no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2668
diff
changeset
|
299 lastStateChangeTime_ = boost::posix_time::from_iso_string |
eaf10085ffa1
no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2668
diff
changeset
|
300 (SerializationToolbox::ReadString(serialized, LAST_CHANGE_TIME)); |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
301 runtime_ = boost::posix_time::milliseconds |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
302 (SerializationToolbox::ReadInteger(serialized, RUNTIME)); |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
303 |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
304 retryTime_ = creationTime_; |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
305 |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
306 job_.reset(unserializer.UnserializeJob(serialized[JOB])); |
2652 | 307 job_->GetJobType(jobType_); |
308 job_->Start(); | |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
309 |
3240
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3191
diff
changeset
|
310 lastStatus_ = JobStatus(ErrorCode_Success, "", *job_); |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
311 } |
2569 | 312 }; |
313 | |
314 | |
4203
4d42408da117
improving const-correctness in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4121
diff
changeset
|
315 bool JobsRegistry::PriorityComparator::operator() (JobHandler* const& a, |
4d42408da117
improving const-correctness in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4121
diff
changeset
|
316 JobHandler* const& b) const |
2569 | 317 { |
318 return a->GetPriority() < b->GetPriority(); | |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
319 } |
2569 | 320 |
321 | |
322 #if defined(NDEBUG) | |
323 void JobsRegistry::CheckInvariants() const | |
324 { | |
325 } | |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
326 |
2569 | 327 #else |
328 bool JobsRegistry::IsPendingJob(const JobHandler& job) const | |
329 { | |
330 PendingJobs copy = pendingJobs_; | |
331 while (!copy.empty()) | |
332 { | |
333 if (copy.top() == &job) | |
334 { | |
335 return true; | |
336 } | |
337 | |
338 copy.pop(); | |
339 } | |
340 | |
341 return false; | |
342 } | |
343 | |
344 bool JobsRegistry::IsCompletedJob(JobHandler& job) const | |
345 { | |
346 for (CompletedJobs::const_iterator it = completedJobs_.begin(); | |
347 it != completedJobs_.end(); ++it) | |
348 { | |
349 if (*it == &job) | |
350 { | |
351 return true; | |
352 } | |
353 } | |
354 | |
355 return false; | |
356 } | |
357 | |
358 bool JobsRegistry::IsRetryJob(JobHandler& job) const | |
359 { | |
360 return retryJobs_.find(&job) != retryJobs_.end(); | |
361 } | |
362 | |
363 void JobsRegistry::CheckInvariants() const | |
364 { | |
365 { | |
366 PendingJobs copy = pendingJobs_; | |
367 while (!copy.empty()) | |
368 { | |
369 assert(copy.top()->GetState() == JobState_Pending); | |
370 copy.pop(); | |
371 } | |
372 } | |
373 | |
374 assert(completedJobs_.size() <= maxCompletedJobs_); | |
375 | |
376 for (CompletedJobs::const_iterator it = completedJobs_.begin(); | |
377 it != completedJobs_.end(); ++it) | |
378 { | |
379 assert((*it)->GetState() == JobState_Success || | |
380 (*it)->GetState() == JobState_Failure); | |
381 } | |
382 | |
383 for (RetryJobs::const_iterator it = retryJobs_.begin(); | |
384 it != retryJobs_.end(); ++it) | |
385 { | |
386 assert((*it)->GetState() == JobState_Retry); | |
387 } | |
388 | |
389 for (JobsIndex::const_iterator it = jobsIndex_.begin(); | |
390 it != jobsIndex_.end(); ++it) | |
391 { | |
392 JobHandler& job = *it->second; | |
393 | |
394 assert(job.GetId() == it->first); | |
395 | |
396 switch (job.GetState()) | |
397 { | |
398 case JobState_Pending: | |
399 assert(!IsRetryJob(job) && IsPendingJob(job) && !IsCompletedJob(job)); | |
400 break; | |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
401 |
2569 | 402 case JobState_Success: |
403 case JobState_Failure: | |
404 assert(!IsRetryJob(job) && !IsPendingJob(job) && IsCompletedJob(job)); | |
405 break; | |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
406 |
2569 | 407 case JobState_Retry: |
408 assert(IsRetryJob(job) && !IsPendingJob(job) && !IsCompletedJob(job)); | |
409 break; | |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
410 |
2569 | 411 case JobState_Running: |
412 case JobState_Paused: | |
413 assert(!IsRetryJob(job) && !IsPendingJob(job) && !IsCompletedJob(job)); | |
414 break; | |
415 | |
416 default: | |
417 throw OrthancException(ErrorCode_InternalError); | |
418 } | |
419 } | |
420 } | |
421 #endif | |
422 | |
423 | |
424 void JobsRegistry::ForgetOldCompletedJobs() | |
425 { | |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
426 while (completedJobs_.size() > maxCompletedJobs_) |
2569 | 427 { |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
428 assert(completedJobs_.front() != NULL); |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
429 |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
430 std::string id = completedJobs_.front()->GetId(); |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
431 assert(jobsIndex_.find(id) != jobsIndex_.end()); |
2569 | 432 |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
433 jobsIndex_.erase(id); |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
434 delete(completedJobs_.front()); |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
435 completedJobs_.pop_front(); |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
436 } |
2569 | 437 |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
438 CheckInvariants(); |
2569 | 439 } |
440 | |
441 | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
442 void JobsRegistry::SetCompletedJob(JobHandler& job, |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
443 bool success) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
444 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
445 job.SetState(success ? JobState_Success : JobState_Failure); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
446 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
447 completedJobs_.push_back(&job); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
448 someJobComplete_.notify_all(); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
449 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
450 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
451 |
2569 | 452 void JobsRegistry::MarkRunningAsCompleted(JobHandler& job, |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
453 CompletedReason reason) |
2569 | 454 { |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
455 const char* tmp; |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
456 |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
457 switch (reason) |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
458 { |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
459 case CompletedReason_Success: |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
460 tmp = "success"; |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
461 break; |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
462 |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
463 case CompletedReason_Failure: |
3371 | 464 tmp = "failure"; |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
465 break; |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
466 |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
467 case CompletedReason_Canceled: |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
468 tmp = "cancel"; |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
469 break; |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
470 |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
471 default: |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
472 throw OrthancException(ErrorCode_InternalError); |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
473 } |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
474 |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
475 LOG(INFO) << "Job has completed with " << tmp << ": " << job.GetId(); |
2569 | 476 |
477 CheckInvariants(); | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
478 |
2569 | 479 assert(job.GetState() == JobState_Running); |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
480 SetCompletedJob(job, reason == CompletedReason_Success); |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
481 |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
482 if (reason == CompletedReason_Canceled) |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
483 { |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
484 job.SetLastErrorCode(ErrorCode_CanceledJob); |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
485 } |
2570
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
486 |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
487 if (observer_ != NULL) |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
488 { |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
489 if (reason == CompletedReason_Success) |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
490 { |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
491 observer_->SignalJobSuccess(job.GetId()); |
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
492 } |
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
493 else |
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
494 { |
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
495 observer_->SignalJobFailure(job.GetId()); |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
496 } |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
497 } |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
498 |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
499 // WARNING: The following call might make "job" invalid if the job |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
500 // history size is empty |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
501 ForgetOldCompletedJobs(); |
2569 | 502 } |
503 | |
504 | |
505 void JobsRegistry::MarkRunningAsRetry(JobHandler& job, | |
506 unsigned int timeout) | |
507 { | |
508 LOG(INFO) << "Job scheduled for retry in " << timeout << "ms: " << job.GetId(); | |
509 | |
510 CheckInvariants(); | |
511 | |
512 assert(job.GetState() == JobState_Running && | |
513 retryJobs_.find(&job) == retryJobs_.end()); | |
514 | |
515 retryJobs_.insert(&job); | |
516 job.SetRetryState(timeout); | |
517 | |
518 CheckInvariants(); | |
519 } | |
520 | |
521 | |
522 void JobsRegistry::MarkRunningAsPaused(JobHandler& job) | |
523 { | |
524 LOG(INFO) << "Job paused: " << job.GetId(); | |
525 | |
526 CheckInvariants(); | |
527 assert(job.GetState() == JobState_Running); | |
528 | |
529 job.SetState(JobState_Paused); | |
530 | |
531 CheckInvariants(); | |
532 } | |
533 | |
534 | |
2570
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
535 bool JobsRegistry::GetStateInternal(JobState& state, |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
536 const std::string& id) |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
537 { |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
538 CheckInvariants(); |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
539 |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
540 JobsIndex::const_iterator it = jobsIndex_.find(id); |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
541 if (it == jobsIndex_.end()) |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
542 { |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
543 return false; |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
544 } |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
545 else |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
546 { |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
547 state = it->second->GetState(); |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
548 return true; |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
549 } |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
550 } |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
551 |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
552 |
2569 | 553 JobsRegistry::~JobsRegistry() |
554 { | |
555 for (JobsIndex::iterator it = jobsIndex_.begin(); it != jobsIndex_.end(); ++it) | |
556 { | |
557 assert(it->second != NULL); | |
558 delete it->second; | |
559 } | |
560 } | |
561 | |
562 | |
2620
1232922c8793
speeding up shutdown if Lua script is in trailing phase
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2600
diff
changeset
|
563 void JobsRegistry::SetMaxCompletedJobs(size_t n) |
2569 | 564 { |
565 boost::mutex::scoped_lock lock(mutex_); | |
566 CheckInvariants(); | |
567 | |
2620
1232922c8793
speeding up shutdown if Lua script is in trailing phase
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2600
diff
changeset
|
568 LOG(INFO) << "The size of the history of the jobs engine is set to: " << n << " job(s)"; |
1232922c8793
speeding up shutdown if Lua script is in trailing phase
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2600
diff
changeset
|
569 |
1232922c8793
speeding up shutdown if Lua script is in trailing phase
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2600
diff
changeset
|
570 maxCompletedJobs_ = n; |
2569 | 571 ForgetOldCompletedJobs(); |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
572 } |
2569 | 573 |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
574 |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
575 size_t JobsRegistry::GetMaxCompletedJobs() |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
576 { |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
577 boost::mutex::scoped_lock lock(mutex_); |
2569 | 578 CheckInvariants(); |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
579 return maxCompletedJobs_; |
2569 | 580 } |
581 | |
582 | |
583 void JobsRegistry::ListJobs(std::set<std::string>& target) | |
584 { | |
585 boost::mutex::scoped_lock lock(mutex_); | |
586 CheckInvariants(); | |
587 | |
588 for (JobsIndex::const_iterator it = jobsIndex_.begin(); | |
589 it != jobsIndex_.end(); ++it) | |
590 { | |
591 target.insert(it->first); | |
592 } | |
593 } | |
594 | |
595 | |
596 bool JobsRegistry::GetJobInfo(JobInfo& target, | |
597 const std::string& id) | |
598 { | |
599 boost::mutex::scoped_lock lock(mutex_); | |
600 CheckInvariants(); | |
601 | |
602 JobsIndex::const_iterator found = jobsIndex_.find(id); | |
603 | |
604 if (found == jobsIndex_.end()) | |
605 { | |
606 return false; | |
607 } | |
608 else | |
609 { | |
610 const JobHandler& handler = *found->second; | |
611 target = JobInfo(handler.GetId(), | |
612 handler.GetPriority(), | |
613 handler.GetState(), | |
614 handler.GetLastStatus(), | |
615 handler.GetCreationTime(), | |
616 handler.GetLastStateChangeTime(), | |
617 handler.GetRuntime()); | |
618 return true; | |
619 } | |
620 } | |
621 | |
622 | |
2976
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
623 bool JobsRegistry::GetJobOutput(std::string& output, |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
624 MimeType& mime, |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
625 const std::string& job, |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
626 const std::string& key) |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
627 { |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
628 boost::mutex::scoped_lock lock(mutex_); |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
629 CheckInvariants(); |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
630 |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
631 JobsIndex::const_iterator found = jobsIndex_.find(job); |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
632 |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
633 if (found == jobsIndex_.end()) |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
634 { |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
635 return false; |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
636 } |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
637 else |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
638 { |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
639 const JobHandler& handler = *found->second; |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
640 |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
641 if (handler.GetState() == JobState_Success) |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
642 { |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
643 return handler.GetJob().GetOutput(output, mime, key); |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
644 } |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
645 else |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
646 { |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
647 return false; |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
648 } |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
649 } |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
650 } |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
651 |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2954
diff
changeset
|
652 |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
653 void JobsRegistry::SubmitInternal(std::string& id, |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
654 JobHandler* handler) |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
655 { |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
656 if (handler == NULL) |
2668
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2667
diff
changeset
|
657 { |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2667
diff
changeset
|
658 throw OrthancException(ErrorCode_NullPointer); |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2667
diff
changeset
|
659 } |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
660 |
3712
2a170a8f1faf
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
661 std::unique_ptr<JobHandler> protection(handler); |
2669
eaf10085ffa1
no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2668
diff
changeset
|
662 |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
663 { |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
664 boost::mutex::scoped_lock lock(mutex_); |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
665 CheckInvariants(); |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
666 |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
667 id = handler->GetId(); |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
668 int priority = handler->GetPriority(); |
2569 | 669 |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
670 jobsIndex_.insert(std::make_pair(id, protection.release())); |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
671 |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
672 switch (handler->GetState()) |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
673 { |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
674 case JobState_Pending: |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
675 case JobState_Retry: |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
676 case JobState_Running: |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
677 handler->SetState(JobState_Pending); |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
678 pendingJobs_.push(handler); |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
679 pendingJobAvailable_.notify_one(); |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
680 break; |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
681 |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
682 case JobState_Success: |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
683 SetCompletedJob(*handler, true); |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
684 break; |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
685 |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
686 case JobState_Failure: |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
687 SetCompletedJob(*handler, false); |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
688 break; |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
689 |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
690 case JobState_Paused: |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
691 break; |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
692 |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
693 default: |
2954
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2950
diff
changeset
|
694 { |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2950
diff
changeset
|
695 std::string details = ("A job should not be loaded from state: " + |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2950
diff
changeset
|
696 std::string(EnumerationToString(handler->GetState()))); |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2950
diff
changeset
|
697 throw OrthancException(ErrorCode_InternalError, details); |
d924f9bb61cc
taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2950
diff
changeset
|
698 } |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
699 } |
2668
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2667
diff
changeset
|
700 |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
701 LOG(INFO) << "New job submitted with priority " << priority << ": " << id; |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
702 |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
703 if (observer_ != NULL) |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
704 { |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
705 observer_->SignalJobSubmitted(id); |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
706 } |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
707 |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
708 // WARNING: The following call might make "handler" invalid if |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
709 // the job history size is empty |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
710 ForgetOldCompletedJobs(); |
2668
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2667
diff
changeset
|
711 } |
2569 | 712 } |
713 | |
4300 | 714 JobsRegistry::JobsRegistry(size_t maxCompletedJobs) : |
715 maxCompletedJobs_(maxCompletedJobs), | |
716 observer_(NULL) | |
717 { | |
718 } | |
719 | |
2569 | 720 |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
721 void JobsRegistry::Submit(std::string& id, |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
722 IJob* job, // Takes ownership |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
723 int priority) |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
724 { |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
725 SubmitInternal(id, new JobHandler(job, priority)); |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
726 } |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
727 |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
728 |
2569 | 729 void JobsRegistry::Submit(IJob* job, // Takes ownership |
730 int priority) | |
731 { | |
732 std::string id; | |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
733 SubmitInternal(id, new JobHandler(job, priority)); |
2569 | 734 } |
735 | |
736 | |
3240
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3191
diff
changeset
|
737 void JobsRegistry::SubmitAndWait(Json::Value& successContent, |
2867 | 738 IJob* job, // Takes ownership |
2570
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
739 int priority) |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
740 { |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
741 std::string id; |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
742 Submit(id, job, priority); |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
743 |
2867 | 744 JobState state = JobState_Pending; // Dummy initialization |
2570
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
745 |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
746 { |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
747 boost::mutex::scoped_lock lock(mutex_); |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
748 |
2867 | 749 for (;;) |
2570
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
750 { |
2867 | 751 if (!GetStateInternal(state, id)) |
752 { | |
3162
94a4f75cc746
Fix issue #119 (/patients/.../archive returns a 500 when JobsHistorySize is 0)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
753 // Job has finished and has been lost (typically happens if |
94a4f75cc746
Fix issue #119 (/patients/.../archive returns a 500 when JobsHistorySize is 0)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
754 // "JobsHistorySize" is 0) |
94a4f75cc746
Fix issue #119 (/patients/.../archive returns a 500 when JobsHistorySize is 0)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
755 throw OrthancException(ErrorCode_InexistentItem, |
94a4f75cc746
Fix issue #119 (/patients/.../archive returns a 500 when JobsHistorySize is 0)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
756 "Cannot retrieve the status of the job, " |
94a4f75cc746
Fix issue #119 (/patients/.../archive returns a 500 when JobsHistorySize is 0)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
757 "make sure that \"JobsHistorySize\" is not 0"); |
2867 | 758 } |
759 else if (state == JobState_Failure) | |
760 { | |
761 // Failure | |
3240
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3191
diff
changeset
|
762 JobsIndex::const_iterator it = jobsIndex_.find(id); |
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3191
diff
changeset
|
763 if (it != jobsIndex_.end()) // Should always be true, already tested in GetStateInternal() |
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3191
diff
changeset
|
764 { |
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3191
diff
changeset
|
765 ErrorCode code = it->second->GetLastStatus().GetErrorCode(); |
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3191
diff
changeset
|
766 const std::string& details = it->second->GetLastStatus().GetDetails(); |
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3191
diff
changeset
|
767 |
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3191
diff
changeset
|
768 if (details.empty()) |
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3191
diff
changeset
|
769 { |
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3191
diff
changeset
|
770 throw OrthancException(code); |
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3191
diff
changeset
|
771 } |
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3191
diff
changeset
|
772 else |
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3191
diff
changeset
|
773 { |
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3191
diff
changeset
|
774 throw OrthancException(code, details); |
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3191
diff
changeset
|
775 } |
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3191
diff
changeset
|
776 } |
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3191
diff
changeset
|
777 else |
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3191
diff
changeset
|
778 { |
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3191
diff
changeset
|
779 throw OrthancException(ErrorCode_InternalError); |
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3191
diff
changeset
|
780 } |
2867 | 781 } |
782 else if (state == JobState_Success) | |
783 { | |
784 // Success, try and retrieve the status of the job | |
785 JobsIndex::const_iterator it = jobsIndex_.find(id); | |
786 if (it == jobsIndex_.end()) | |
787 { | |
788 // Should not happen | |
789 state = JobState_Failure; | |
790 } | |
791 else | |
792 { | |
793 const JobStatus& status = it->second->GetLastStatus(); | |
794 successContent = status.GetPublicContent(); | |
795 } | |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
796 |
3240
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3191
diff
changeset
|
797 return; |
2867 | 798 } |
799 else | |
800 { | |
801 // This job has not finished yet, wait for new completion | |
802 someJobComplete_.wait(lock); | |
803 } | |
2570
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
804 } |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
805 } |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
806 } |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
807 |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
808 |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
809 bool JobsRegistry::SetPriority(const std::string& id, |
2569 | 810 int priority) |
811 { | |
812 LOG(INFO) << "Changing priority to " << priority << " for job: " << id; | |
813 | |
814 boost::mutex::scoped_lock lock(mutex_); | |
815 CheckInvariants(); | |
816 | |
817 JobsIndex::iterator found = jobsIndex_.find(id); | |
818 | |
819 if (found == jobsIndex_.end()) | |
820 { | |
821 LOG(WARNING) << "Unknown job: " << id; | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
822 return false; |
2569 | 823 } |
824 else | |
825 { | |
826 found->second->SetPriority(priority); | |
827 | |
828 if (found->second->GetState() == JobState_Pending) | |
829 { | |
830 // If the job is pending, we need to reconstruct the | |
831 // priority queue, as the heap condition has changed | |
832 | |
833 PendingJobs copy; | |
834 std::swap(copy, pendingJobs_); | |
835 | |
836 assert(pendingJobs_.empty()); | |
837 while (!copy.empty()) | |
838 { | |
839 pendingJobs_.push(copy.top()); | |
840 copy.pop(); | |
841 } | |
842 } | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
843 |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
844 CheckInvariants(); |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
845 return true; |
2569 | 846 } |
847 } | |
848 | |
849 | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
850 void JobsRegistry::RemovePendingJob(const std::string& id) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
851 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
852 // If the job is pending, we need to reconstruct the priority |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
853 // queue to remove it |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
854 PendingJobs copy; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
855 std::swap(copy, pendingJobs_); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
856 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
857 assert(pendingJobs_.empty()); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
858 while (!copy.empty()) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
859 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
860 if (copy.top()->GetId() != id) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
861 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
862 pendingJobs_.push(copy.top()); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
863 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
864 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
865 copy.pop(); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
866 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
867 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
868 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
869 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
870 void JobsRegistry::RemoveRetryJob(JobHandler* handler) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
871 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
872 RetryJobs::iterator item = retryJobs_.find(handler); |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
873 assert(item != retryJobs_.end()); |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
874 retryJobs_.erase(item); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
875 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
876 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
877 |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
878 bool JobsRegistry::Pause(const std::string& id) |
2569 | 879 { |
880 LOG(INFO) << "Pausing job: " << id; | |
881 | |
882 boost::mutex::scoped_lock lock(mutex_); | |
883 CheckInvariants(); | |
884 | |
885 JobsIndex::iterator found = jobsIndex_.find(id); | |
886 | |
887 if (found == jobsIndex_.end()) | |
888 { | |
889 LOG(WARNING) << "Unknown job: " << id; | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
890 return false; |
2569 | 891 } |
892 else | |
893 { | |
894 switch (found->second->GetState()) | |
895 { | |
896 case JobState_Pending: | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
897 RemovePendingJob(id); |
2569 | 898 found->second->SetState(JobState_Paused); |
899 break; | |
900 | |
901 case JobState_Retry: | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
902 RemoveRetryJob(found->second); |
2569 | 903 found->second->SetState(JobState_Paused); |
904 break; | |
905 | |
906 case JobState_Paused: | |
907 case JobState_Success: | |
908 case JobState_Failure: | |
909 // Nothing to be done | |
910 break; | |
911 | |
912 case JobState_Running: | |
913 found->second->SchedulePause(); | |
914 break; | |
915 | |
916 default: | |
917 throw OrthancException(ErrorCode_InternalError); | |
918 } | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
919 |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
920 CheckInvariants(); |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
921 return true; |
2569 | 922 } |
923 } | |
924 | |
925 | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
926 bool JobsRegistry::Cancel(const std::string& id) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
927 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
928 LOG(INFO) << "Canceling job: " << id; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
929 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
930 boost::mutex::scoped_lock lock(mutex_); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
931 CheckInvariants(); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
932 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
933 JobsIndex::iterator found = jobsIndex_.find(id); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
934 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
935 if (found == jobsIndex_.end()) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
936 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
937 LOG(WARNING) << "Unknown job: " << id; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
938 return false; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
939 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
940 else |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
941 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
942 switch (found->second->GetState()) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
943 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
944 case JobState_Pending: |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
945 RemovePendingJob(id); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
946 SetCompletedJob(*found->second, false); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
947 found->second->SetLastErrorCode(ErrorCode_CanceledJob); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
948 break; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
949 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
950 case JobState_Retry: |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
951 RemoveRetryJob(found->second); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
952 SetCompletedJob(*found->second, false); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
953 found->second->SetLastErrorCode(ErrorCode_CanceledJob); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
954 break; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
955 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
956 case JobState_Paused: |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
957 SetCompletedJob(*found->second, false); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
958 found->second->SetLastErrorCode(ErrorCode_CanceledJob); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
959 break; |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
960 |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
961 case JobState_Success: |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
962 case JobState_Failure: |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
963 // Nothing to be done |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
964 break; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
965 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
966 case JobState_Running: |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
967 found->second->ScheduleCancel(); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
968 break; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
969 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
970 default: |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
971 throw OrthancException(ErrorCode_InternalError); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
972 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
973 |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
974 // WARNING: The following call might make "handler" invalid if |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
975 // the job history size is empty |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
976 ForgetOldCompletedJobs(); |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
977 |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
978 return true; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
979 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
980 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
981 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
982 |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
983 bool JobsRegistry::Resume(const std::string& id) |
2569 | 984 { |
985 LOG(INFO) << "Resuming job: " << id; | |
986 | |
987 boost::mutex::scoped_lock lock(mutex_); | |
988 CheckInvariants(); | |
989 | |
990 JobsIndex::iterator found = jobsIndex_.find(id); | |
991 | |
992 if (found == jobsIndex_.end()) | |
993 { | |
994 LOG(WARNING) << "Unknown job: " << id; | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
995 return false; |
2569 | 996 } |
997 else if (found->second->GetState() != JobState_Paused) | |
998 { | |
999 LOG(WARNING) << "Cannot resume a job that is not paused: " << id; | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
1000 return false; |
2569 | 1001 } |
1002 else | |
1003 { | |
1004 found->second->SetState(JobState_Pending); | |
1005 pendingJobs_.push(found->second); | |
1006 pendingJobAvailable_.notify_one(); | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
1007 CheckInvariants(); |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1008 return true; |
2569 | 1009 } |
1010 } | |
1011 | |
1012 | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
1013 bool JobsRegistry::Resubmit(const std::string& id) |
2569 | 1014 { |
1015 LOG(INFO) << "Resubmitting failed job: " << id; | |
1016 | |
1017 boost::mutex::scoped_lock lock(mutex_); | |
1018 CheckInvariants(); | |
1019 | |
1020 JobsIndex::iterator found = jobsIndex_.find(id); | |
1021 | |
1022 if (found == jobsIndex_.end()) | |
1023 { | |
1024 LOG(WARNING) << "Unknown job: " << id; | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
1025 return false; |
2569 | 1026 } |
1027 else if (found->second->GetState() != JobState_Failure) | |
1028 { | |
1029 LOG(WARNING) << "Cannot resubmit a job that has not failed: " << id; | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
1030 return false; |
2569 | 1031 } |
1032 else | |
1033 { | |
2812
ea7aea6f6a95
improved naming of methods in IJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2673
diff
changeset
|
1034 found->second->GetJob().Reset(); |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1035 |
2569 | 1036 bool ok = false; |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1037 for (CompletedJobs::iterator it = completedJobs_.begin(); |
2569 | 1038 it != completedJobs_.end(); ++it) |
1039 { | |
1040 if (*it == found->second) | |
1041 { | |
1042 ok = true; | |
1043 completedJobs_.erase(it); | |
1044 break; | |
1045 } | |
1046 } | |
1047 | |
4121 | 1048 (void) ok; // Remove warning about unused variable in release builds |
2569 | 1049 assert(ok); |
1050 | |
1051 found->second->SetState(JobState_Pending); | |
1052 pendingJobs_.push(found->second); | |
1053 pendingJobAvailable_.notify_one(); | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
1054 |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
1055 CheckInvariants(); |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
1056 return true; |
2569 | 1057 } |
1058 } | |
1059 | |
1060 | |
1061 void JobsRegistry::ScheduleRetries() | |
1062 { | |
1063 boost::mutex::scoped_lock lock(mutex_); | |
1064 CheckInvariants(); | |
1065 | |
1066 RetryJobs copy; | |
1067 std::swap(copy, retryJobs_); | |
1068 | |
1069 const boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time(); | |
1070 | |
1071 assert(retryJobs_.empty()); | |
1072 for (RetryJobs::iterator it = copy.begin(); it != copy.end(); ++it) | |
1073 { | |
1074 if ((*it)->IsRetryReady(now)) | |
1075 { | |
1076 LOG(INFO) << "Retrying job: " << (*it)->GetId(); | |
1077 (*it)->SetState(JobState_Pending); | |
1078 pendingJobs_.push(*it); | |
1079 pendingJobAvailable_.notify_one(); | |
1080 } | |
1081 else | |
1082 { | |
1083 retryJobs_.insert(*it); | |
1084 } | |
1085 } | |
1086 | |
1087 CheckInvariants(); | |
1088 } | |
1089 | |
1090 | |
1091 bool JobsRegistry::GetState(JobState& state, | |
1092 const std::string& id) | |
1093 { | |
1094 boost::mutex::scoped_lock lock(mutex_); | |
2570
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
1095 return GetStateInternal(state, id); |
2569 | 1096 } |
1097 | |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
1098 |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1099 void JobsRegistry::SetObserver(JobsRegistry::IObserver& observer) |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
1100 { |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1101 boost::mutex::scoped_lock lock(mutex_); |
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1102 observer_ = &observer; |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
1103 } |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
1104 |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1105 |
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1106 void JobsRegistry::ResetObserver() |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
1107 { |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1108 boost::mutex::scoped_lock lock(mutex_); |
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1109 observer_ = NULL; |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
1110 } |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
1111 |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1112 |
2569 | 1113 JobsRegistry::RunningJob::RunningJob(JobsRegistry& registry, |
1114 unsigned int timeout) : | |
1115 registry_(registry), | |
1116 handler_(NULL), | |
1117 targetState_(JobState_Failure), | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1118 targetRetryTimeout_(0), |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1119 canceled_(false) |
2569 | 1120 { |
1121 { | |
1122 boost::mutex::scoped_lock lock(registry_.mutex_); | |
1123 | |
1124 while (registry_.pendingJobs_.empty()) | |
1125 { | |
1126 if (timeout == 0) | |
1127 { | |
1128 registry_.pendingJobAvailable_.wait(lock); | |
1129 } | |
1130 else | |
1131 { | |
1132 bool success = registry_.pendingJobAvailable_.timed_wait | |
1133 (lock, boost::posix_time::milliseconds(timeout)); | |
1134 if (!success) | |
1135 { | |
1136 // No pending job | |
1137 return; | |
1138 } | |
1139 } | |
1140 } | |
1141 | |
1142 handler_ = registry_.pendingJobs_.top(); | |
1143 registry_.pendingJobs_.pop(); | |
1144 | |
1145 assert(handler_->GetState() == JobState_Pending); | |
1146 handler_->SetState(JobState_Running); | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1147 handler_->SetLastErrorCode(ErrorCode_Success); |
2569 | 1148 |
1149 job_ = &handler_->GetJob(); | |
1150 id_ = handler_->GetId(); | |
1151 priority_ = handler_->GetPriority(); | |
1152 } | |
1153 } | |
1154 | |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1155 |
2569 | 1156 JobsRegistry::RunningJob::~RunningJob() |
1157 { | |
1158 if (IsValid()) | |
1159 { | |
1160 boost::mutex::scoped_lock lock(registry_.mutex_); | |
1161 | |
1162 switch (targetState_) | |
1163 { | |
1164 case JobState_Failure: | |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
1165 registry_.MarkRunningAsCompleted |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
1166 (*handler_, canceled_ ? CompletedReason_Canceled : CompletedReason_Failure); |
2569 | 1167 break; |
1168 | |
1169 case JobState_Success: | |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
1170 registry_.MarkRunningAsCompleted(*handler_, CompletedReason_Success); |
2569 | 1171 break; |
1172 | |
1173 case JobState_Paused: | |
1174 registry_.MarkRunningAsPaused(*handler_); | |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1175 break; |
2569 | 1176 |
1177 case JobState_Retry: | |
1178 registry_.MarkRunningAsRetry(*handler_, targetRetryTimeout_); | |
1179 break; | |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1180 |
2569 | 1181 default: |
1182 assert(0); | |
1183 } | |
1184 } | |
1185 } | |
1186 | |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1187 |
2569 | 1188 bool JobsRegistry::RunningJob::IsValid() const |
1189 { | |
1190 return (handler_ != NULL && | |
1191 job_ != NULL); | |
1192 } | |
1193 | |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1194 |
2569 | 1195 const std::string& JobsRegistry::RunningJob::GetId() const |
1196 { | |
1197 if (!IsValid()) | |
1198 { | |
1199 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
1200 } | |
1201 else | |
1202 { | |
1203 return id_; | |
1204 } | |
1205 } | |
1206 | |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1207 |
2569 | 1208 int JobsRegistry::RunningJob::GetPriority() const |
1209 { | |
1210 if (!IsValid()) | |
1211 { | |
1212 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
1213 } | |
1214 else | |
1215 { | |
1216 return priority_; | |
1217 } | |
1218 } | |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1219 |
2569 | 1220 |
1221 IJob& JobsRegistry::RunningJob::GetJob() | |
1222 { | |
1223 if (!IsValid()) | |
1224 { | |
1225 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
1226 } | |
1227 else | |
1228 { | |
1229 return *job_; | |
1230 } | |
1231 } | |
1232 | |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1233 |
2569 | 1234 bool JobsRegistry::RunningJob::IsPauseScheduled() |
1235 { | |
1236 if (!IsValid()) | |
1237 { | |
1238 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
1239 } | |
1240 else | |
1241 { | |
1242 boost::mutex::scoped_lock lock(registry_.mutex_); | |
1243 registry_.CheckInvariants(); | |
1244 assert(handler_->GetState() == JobState_Running); | |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1245 |
2569 | 1246 return handler_->IsPauseScheduled(); |
1247 } | |
1248 } | |
1249 | |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1250 |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1251 bool JobsRegistry::RunningJob::IsCancelScheduled() |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1252 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1253 if (!IsValid()) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1254 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1255 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1256 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1257 else |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1258 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1259 boost::mutex::scoped_lock lock(registry_.mutex_); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1260 registry_.CheckInvariants(); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1261 assert(handler_->GetState() == JobState_Running); |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1262 |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1263 return handler_->IsCancelScheduled(); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1264 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1265 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1266 |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1267 |
2569 | 1268 void JobsRegistry::RunningJob::MarkSuccess() |
1269 { | |
1270 if (!IsValid()) | |
1271 { | |
1272 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
1273 } | |
1274 else | |
1275 { | |
1276 targetState_ = JobState_Success; | |
1277 } | |
1278 } | |
1279 | |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1280 |
2569 | 1281 void JobsRegistry::RunningJob::MarkFailure() |
1282 { | |
1283 if (!IsValid()) | |
1284 { | |
1285 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
1286 } | |
1287 else | |
1288 { | |
1289 targetState_ = JobState_Failure; | |
1290 } | |
1291 } | |
1292 | |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1293 |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1294 void JobsRegistry::RunningJob::MarkCanceled() |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1295 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1296 if (!IsValid()) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1297 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1298 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1299 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1300 else |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1301 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1302 targetState_ = JobState_Failure; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1303 canceled_ = true; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1304 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1305 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1306 |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1307 |
2569 | 1308 void JobsRegistry::RunningJob::MarkPause() |
1309 { | |
1310 if (!IsValid()) | |
1311 { | |
1312 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
1313 } | |
1314 else | |
1315 { | |
1316 targetState_ = JobState_Paused; | |
1317 } | |
1318 } | |
1319 | |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1320 |
2569 | 1321 void JobsRegistry::RunningJob::MarkRetry(unsigned int timeout) |
1322 { | |
1323 if (!IsValid()) | |
1324 { | |
1325 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
1326 } | |
1327 else | |
1328 { | |
1329 targetState_ = JobState_Retry; | |
1330 targetRetryTimeout_ = timeout; | |
1331 } | |
1332 } | |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1333 |
2569 | 1334 |
3240
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3191
diff
changeset
|
1335 void JobsRegistry::RunningJob::UpdateStatus(ErrorCode code, |
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3191
diff
changeset
|
1336 const std::string& details) |
2569 | 1337 { |
1338 if (!IsValid()) | |
1339 { | |
1340 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
1341 } | |
1342 else | |
1343 { | |
3240
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3191
diff
changeset
|
1344 JobStatus status(code, details, *job_); |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1345 |
2569 | 1346 boost::mutex::scoped_lock lock(registry_.mutex_); |
1347 registry_.CheckInvariants(); | |
1348 assert(handler_->GetState() == JobState_Running); | |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1349 |
2569 | 1350 handler_->SetLastStatus(status); |
1351 } | |
1352 } | |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1353 |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1354 |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1355 |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1356 void JobsRegistry::Serialize(Json::Value& target) |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1357 { |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1358 boost::mutex::scoped_lock lock(mutex_); |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1359 CheckInvariants(); |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1360 |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1361 target = Json::objectValue; |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1362 target[TYPE] = JOBS_REGISTRY; |
2670
c5646f766b3e
serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
1363 target[JOBS] = Json::objectValue; |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1364 |
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1365 for (JobsIndex::const_iterator it = jobsIndex_.begin(); |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1366 it != jobsIndex_.end(); ++it) |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1367 { |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1368 Json::Value v; |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1369 if (it->second->Serialize(v)) |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1370 { |
2670
c5646f766b3e
serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
1371 target[JOBS][it->first] = v; |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1372 } |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1373 } |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1374 } |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1375 |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1376 |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1377 JobsRegistry::JobsRegistry(IJobUnserializer& unserializer, |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
1378 const Json::Value& s, |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
1379 size_t maxCompletedJobs) : |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1380 maxCompletedJobs_(maxCompletedJobs), |
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1381 observer_(NULL) |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1382 { |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1383 if (SerializationToolbox::ReadString(s, TYPE) != JOBS_REGISTRY || |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1384 !s.isMember(JOBS) || |
2670
c5646f766b3e
serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
1385 s[JOBS].type() != Json::objectValue) |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1386 { |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1387 throw OrthancException(ErrorCode_BadFileFormat); |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1388 } |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1389 |
2670
c5646f766b3e
serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
1390 Json::Value::Members members = s[JOBS].getMemberNames(); |
c5646f766b3e
serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
1391 |
c5646f766b3e
serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
1392 for (Json::Value::Members::const_iterator it = members.begin(); |
c5646f766b3e
serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
1393 it != members.end(); ++it) |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1394 { |
3712
2a170a8f1faf
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
1395 std::unique_ptr<JobHandler> job; |
3191
20826867141f
ignore jobs that cannot be unserialized
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3179
diff
changeset
|
1396 |
20826867141f
ignore jobs that cannot be unserialized
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3179
diff
changeset
|
1397 try |
20826867141f
ignore jobs that cannot be unserialized
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3179
diff
changeset
|
1398 { |
20826867141f
ignore jobs that cannot be unserialized
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3179
diff
changeset
|
1399 job.reset(new JobHandler(unserializer, s[JOBS][*it], *it)); |
20826867141f
ignore jobs that cannot be unserialized
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3179
diff
changeset
|
1400 } |
20826867141f
ignore jobs that cannot be unserialized
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3179
diff
changeset
|
1401 catch (OrthancException& e) |
20826867141f
ignore jobs that cannot be unserialized
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3179
diff
changeset
|
1402 { |
20826867141f
ignore jobs that cannot be unserialized
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3179
diff
changeset
|
1403 LOG(WARNING) << "Cannot unserialize one job from previous execution, " |
20826867141f
ignore jobs that cannot be unserialized
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3179
diff
changeset
|
1404 << "skipping it: " << e.What(); |
20826867141f
ignore jobs that cannot be unserialized
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3179
diff
changeset
|
1405 continue; |
20826867141f
ignore jobs that cannot be unserialized
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3179
diff
changeset
|
1406 } |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
1407 |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
1408 const boost::posix_time::ptime lastChangeTime = job->GetLastStateChangeTime(); |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
1409 |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1410 std::string id; |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
1411 SubmitInternal(id, job.release()); |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
1412 |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
1413 // Check whether the job has not been removed (which could be |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
1414 // the case if the "maxCompletedJobs_" value gets smaller) |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
1415 JobsIndex::iterator found = jobsIndex_.find(id); |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
1416 if (found != jobsIndex_.end()) |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
1417 { |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
1418 // The job still lies in the history: Update the time of its |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
1419 // last change to the time that was serialized |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
1420 assert(found->second != NULL); |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
1421 found->second->SetLastStateChangeTime(lastChangeTime); |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2939
diff
changeset
|
1422 } |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1423 } |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1424 } |
3177
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1425 |
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1426 |
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1427 void JobsRegistry::GetStatistics(unsigned int& pending, |
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1428 unsigned int& running, |
3179
fca730c267d7
New primitives to set and refresh metrics
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3177
diff
changeset
|
1429 unsigned int& success, |
fca730c267d7
New primitives to set and refresh metrics
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3177
diff
changeset
|
1430 unsigned int& failed) |
3177
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1431 { |
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1432 boost::mutex::scoped_lock lock(mutex_); |
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1433 CheckInvariants(); |
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1434 |
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1435 pending = 0; |
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1436 running = 0; |
3179
fca730c267d7
New primitives to set and refresh metrics
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3177
diff
changeset
|
1437 success = 0; |
fca730c267d7
New primitives to set and refresh metrics
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3177
diff
changeset
|
1438 failed = 0; |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1439 |
3177
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1440 for (JobsIndex::const_iterator it = jobsIndex_.begin(); |
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1441 it != jobsIndex_.end(); ++it) |
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1442 { |
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1443 JobHandler& job = *it->second; |
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1444 |
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1445 switch (job.GetState()) |
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1446 { |
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1447 case JobState_Retry: |
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1448 case JobState_Pending: |
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1449 pending ++; |
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1450 break; |
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1451 |
3179
fca730c267d7
New primitives to set and refresh metrics
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3177
diff
changeset
|
1452 case JobState_Paused: |
3177
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1453 case JobState_Running: |
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1454 running ++; |
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1455 break; |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1456 |
3177
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1457 case JobState_Success: |
3179
fca730c267d7
New primitives to set and refresh metrics
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3177
diff
changeset
|
1458 success ++; |
fca730c267d7
New primitives to set and refresh metrics
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3177
diff
changeset
|
1459 break; |
fca730c267d7
New primitives to set and refresh metrics
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3177
diff
changeset
|
1460 |
3177
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1461 case JobState_Failure: |
3179
fca730c267d7
New primitives to set and refresh metrics
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3177
diff
changeset
|
1462 failed ++; |
3177
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1463 break; |
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1464 |
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1465 default: |
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1466 throw OrthancException(ErrorCode_InternalError); |
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1467 } |
4024
1d2b31fc782f
new 'changes': JobSubmitted, JobSuccess, JobFailure
Alain Mazy <alain@mazy.be>
parents:
4022
diff
changeset
|
1468 } |
3177
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3162
diff
changeset
|
1469 } |
2569 | 1470 } |