Mercurial > hg > orthanc
annotate Core/JobsEngine/JobsRegistry.cpp @ 2676:d2f70c8f8bfd jobs
primitives to compile OpenSSL within MariaDB
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 12 Jun 2018 16:22:07 +0200 |
parents | 8e0bc055d18c |
children | ea7aea6f6a95 |
rev | line source |
---|---|
2569 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
11 * | |
12 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
18 * modify file(s) with this exception, you may extend this exception to | |
19 * your version of the file(s), but you are not obligated to do so. If | |
20 * you do not wish to do so, delete this exception statement from your | |
21 * version. If you delete this exception statement from all source files | |
22 * in the program, then also delete it here. | |
23 * | |
24 * This program is distributed in the hope that it will be useful, but | |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
33 | |
34 #include "../PrecompiledHeaders.h" | |
35 #include "JobsRegistry.h" | |
36 | |
37 #include "../Logging.h" | |
38 #include "../OrthancException.h" | |
39 #include "../Toolbox.h" | |
2656
a6d3e45eeff5
SerializationToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2655
diff
changeset
|
40 #include "../SerializationToolbox.h" |
2569 | 41 |
42 namespace Orthanc | |
43 { | |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
44 static const char* STATE = "State"; |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
45 static const char* TYPE = "Type"; |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
46 static const char* PRIORITY = "Priority"; |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
47 static const char* JOB = "Job"; |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
48 static const char* JOBS = "Jobs"; |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
49 static const char* JOBS_REGISTRY = "JobsRegistry"; |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
50 static const char* MAX_COMPLETED_JOBS = "MaxCompletedJobs"; |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
51 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
|
52 static const char* LAST_CHANGE_TIME = "LastChangeTime"; |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
53 static const char* RUNTIME = "Runtime"; |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
54 |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
55 |
2569 | 56 class JobsRegistry::JobHandler : public boost::noncopyable |
57 { | |
58 private: | |
59 std::string id_; | |
60 JobState state_; | |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
61 std::string jobType_; |
2569 | 62 std::auto_ptr<IJob> job_; |
63 int priority_; // "+inf()" means highest priority | |
64 boost::posix_time::ptime creationTime_; | |
65 boost::posix_time::ptime lastStateChangeTime_; | |
66 boost::posix_time::time_duration runtime_; | |
67 boost::posix_time::ptime retryTime_; | |
68 bool pauseScheduled_; | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
69 bool cancelScheduled_; |
2569 | 70 JobStatus lastStatus_; |
71 | |
72 void Touch() | |
73 { | |
74 const boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time(); | |
75 | |
76 if (state_ == JobState_Running) | |
77 { | |
78 runtime_ += (now - lastStateChangeTime_); | |
79 } | |
80 | |
81 lastStateChangeTime_ = now; | |
82 } | |
83 | |
84 void SetStateInternal(JobState state) | |
85 { | |
86 state_ = state; | |
87 pauseScheduled_ = false; | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
88 cancelScheduled_ = false; |
2569 | 89 Touch(); |
90 } | |
91 | |
92 public: | |
93 JobHandler(IJob* job, | |
94 int priority) : | |
95 id_(Toolbox::GenerateUuid()), | |
96 state_(JobState_Pending), | |
97 job_(job), | |
98 priority_(priority), | |
99 creationTime_(boost::posix_time::microsec_clock::universal_time()), | |
100 lastStateChangeTime_(creationTime_), | |
101 runtime_(boost::posix_time::milliseconds(0)), | |
102 retryTime_(creationTime_), | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
103 pauseScheduled_(false), |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
104 cancelScheduled_(false) |
2569 | 105 { |
106 if (job == NULL) | |
107 { | |
108 throw OrthancException(ErrorCode_NullPointer); | |
109 } | |
110 | |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
111 job->GetJobType(jobType_); |
2668
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2667
diff
changeset
|
112 job->Start(); |
2630
00327e989458
creating archives/medias is now a job
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2620
diff
changeset
|
113 |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
114 lastStatus_ = JobStatus(ErrorCode_Success, *job_); |
2569 | 115 } |
116 | |
117 const std::string& GetId() const | |
118 { | |
119 return id_; | |
120 } | |
121 | |
122 IJob& GetJob() const | |
123 { | |
124 assert(job_.get() != NULL); | |
125 return *job_; | |
126 } | |
127 | |
128 void SetPriority(int priority) | |
129 { | |
130 priority_ = priority; | |
131 } | |
132 | |
133 int GetPriority() const | |
134 { | |
135 return priority_; | |
136 } | |
137 | |
138 JobState GetState() const | |
139 { | |
140 return state_; | |
141 } | |
142 | |
143 void SetState(JobState state) | |
144 { | |
145 if (state == JobState_Retry) | |
146 { | |
147 // Use "SetRetryState()" | |
148 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
149 } | |
150 else | |
151 { | |
152 SetStateInternal(state); | |
153 } | |
154 } | |
155 | |
156 void SetRetryState(unsigned int timeout) | |
157 { | |
158 if (state_ == JobState_Running) | |
159 { | |
160 SetStateInternal(JobState_Retry); | |
161 retryTime_ = (boost::posix_time::microsec_clock::universal_time() + | |
162 boost::posix_time::milliseconds(timeout)); | |
163 } | |
164 else | |
165 { | |
166 // Only valid for running jobs | |
167 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
168 } | |
169 } | |
170 | |
171 void SchedulePause() | |
172 { | |
173 if (state_ == JobState_Running) | |
174 { | |
175 pauseScheduled_ = true; | |
176 } | |
177 else | |
178 { | |
179 // Only valid for running jobs | |
180 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
181 } | |
182 } | |
183 | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
184 void ScheduleCancel() |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
185 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
186 if (state_ == JobState_Running) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
187 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
188 cancelScheduled_ = true; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
189 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
190 else |
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 // Only valid for running jobs |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
193 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
194 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
195 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
196 |
2569 | 197 bool IsPauseScheduled() |
198 { | |
199 return pauseScheduled_; | |
200 } | |
201 | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
202 bool IsCancelScheduled() |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
203 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
204 return cancelScheduled_; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
205 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
206 |
2569 | 207 bool IsRetryReady(const boost::posix_time::ptime& now) const |
208 { | |
209 if (state_ != JobState_Retry) | |
210 { | |
211 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
212 } | |
213 else | |
214 { | |
215 return retryTime_ <= now; | |
216 } | |
217 } | |
218 | |
219 const boost::posix_time::ptime& GetCreationTime() const | |
220 { | |
221 return creationTime_; | |
222 } | |
223 | |
224 const boost::posix_time::ptime& GetLastStateChangeTime() const | |
225 { | |
226 return lastStateChangeTime_; | |
227 } | |
228 | |
2669
eaf10085ffa1
no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2668
diff
changeset
|
229 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
|
230 { |
eaf10085ffa1
no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2668
diff
changeset
|
231 lastStateChangeTime_ = time; |
eaf10085ffa1
no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2668
diff
changeset
|
232 } |
eaf10085ffa1
no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2668
diff
changeset
|
233 |
2569 | 234 const boost::posix_time::time_duration& GetRuntime() const |
235 { | |
236 return runtime_; | |
237 } | |
238 | |
239 const JobStatus& GetLastStatus() const | |
240 { | |
241 return lastStatus_; | |
242 } | |
243 | |
244 void SetLastStatus(const JobStatus& status) | |
245 { | |
246 lastStatus_ = status; | |
247 Touch(); | |
248 } | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
249 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
250 void SetLastErrorCode(ErrorCode code) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
251 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
252 lastStatus_.SetErrorCode(code); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
253 } |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
254 |
2663
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
255 bool Serialize(Json::Value& target) const |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
256 { |
2655 | 257 target = Json::objectValue; |
2663
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
258 |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
259 bool ok; |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
260 |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
261 if (state_ == JobState_Running) |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
262 { |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
263 // WARNING: Cannot directly access the "job_" member, as long |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
264 // 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
|
265 // mutex at the "JobHandler" level, as serialization would be |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
266 // blocked while a step in the job is running. Instead, we |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
267 // 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
|
268 |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
269 if (lastStatus_.HasSerialized()) |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
270 { |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
271 target[JOB] = lastStatus_.GetSerialized(); |
2663
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
272 ok = true; |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
273 } |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
274 else |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
275 { |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
276 ok = false; |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
277 } |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
278 } |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
279 else |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
280 { |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
281 ok = job_->Serialize(target[JOB]); |
2663
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
282 } |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
283 |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
284 if (ok) |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
285 { |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
286 target[STATE] = EnumerationToString(state_); |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
287 target[PRIORITY] = priority_; |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
288 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
|
289 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
|
290 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
|
291 return true; |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
292 } |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
293 else |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
294 { |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
295 LOG(INFO) << "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
|
296 return false; |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
297 } |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
298 } |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
299 |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
300 JobHandler(IJobUnserializer& unserializer, |
2670
c5646f766b3e
serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
301 const Json::Value& serialized, |
c5646f766b3e
serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
302 const std::string& id) : |
c5646f766b3e
serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
303 id_(id), |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
304 pauseScheduled_(false), |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
305 cancelScheduled_(false) |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
306 { |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
307 state_ = StringToJobState(SerializationToolbox::ReadString(serialized, STATE)); |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
308 priority_ = SerializationToolbox::ReadInteger(serialized, PRIORITY); |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
309 creationTime_ = boost::posix_time::from_iso_string |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
310 (SerializationToolbox::ReadString(serialized, CREATION_TIME)); |
2669
eaf10085ffa1
no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2668
diff
changeset
|
311 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
|
312 (SerializationToolbox::ReadString(serialized, LAST_CHANGE_TIME)); |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
313 runtime_ = boost::posix_time::milliseconds |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
314 (SerializationToolbox::ReadInteger(serialized, RUNTIME)); |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
315 |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
316 retryTime_ = creationTime_; |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
317 |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
318 job_.reset(unserializer.UnserializeJob(serialized[JOB])); |
2652 | 319 job_->GetJobType(jobType_); |
320 job_->Start(); | |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
321 |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
322 lastStatus_ = JobStatus(ErrorCode_Success, *job_); |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
323 } |
2569 | 324 }; |
325 | |
326 | |
327 bool JobsRegistry::PriorityComparator::operator() (JobHandler*& a, | |
328 JobHandler*& b) const | |
329 { | |
330 return a->GetPriority() < b->GetPriority(); | |
331 } | |
332 | |
333 | |
334 #if defined(NDEBUG) | |
335 void JobsRegistry::CheckInvariants() const | |
336 { | |
337 } | |
338 | |
339 #else | |
340 bool JobsRegistry::IsPendingJob(const JobHandler& job) const | |
341 { | |
342 PendingJobs copy = pendingJobs_; | |
343 while (!copy.empty()) | |
344 { | |
345 if (copy.top() == &job) | |
346 { | |
347 return true; | |
348 } | |
349 | |
350 copy.pop(); | |
351 } | |
352 | |
353 return false; | |
354 } | |
355 | |
356 bool JobsRegistry::IsCompletedJob(JobHandler& job) const | |
357 { | |
358 for (CompletedJobs::const_iterator it = completedJobs_.begin(); | |
359 it != completedJobs_.end(); ++it) | |
360 { | |
361 if (*it == &job) | |
362 { | |
363 return true; | |
364 } | |
365 } | |
366 | |
367 return false; | |
368 } | |
369 | |
370 bool JobsRegistry::IsRetryJob(JobHandler& job) const | |
371 { | |
372 return retryJobs_.find(&job) != retryJobs_.end(); | |
373 } | |
374 | |
375 void JobsRegistry::CheckInvariants() const | |
376 { | |
377 { | |
378 PendingJobs copy = pendingJobs_; | |
379 while (!copy.empty()) | |
380 { | |
381 assert(copy.top()->GetState() == JobState_Pending); | |
382 copy.pop(); | |
383 } | |
384 } | |
385 | |
386 assert(completedJobs_.size() <= maxCompletedJobs_); | |
387 | |
388 for (CompletedJobs::const_iterator it = completedJobs_.begin(); | |
389 it != completedJobs_.end(); ++it) | |
390 { | |
391 assert((*it)->GetState() == JobState_Success || | |
392 (*it)->GetState() == JobState_Failure); | |
393 } | |
394 | |
395 for (RetryJobs::const_iterator it = retryJobs_.begin(); | |
396 it != retryJobs_.end(); ++it) | |
397 { | |
398 assert((*it)->GetState() == JobState_Retry); | |
399 } | |
400 | |
401 for (JobsIndex::const_iterator it = jobsIndex_.begin(); | |
402 it != jobsIndex_.end(); ++it) | |
403 { | |
404 JobHandler& job = *it->second; | |
405 | |
406 assert(job.GetId() == it->first); | |
407 | |
408 switch (job.GetState()) | |
409 { | |
410 case JobState_Pending: | |
411 assert(!IsRetryJob(job) && IsPendingJob(job) && !IsCompletedJob(job)); | |
412 break; | |
413 | |
414 case JobState_Success: | |
415 case JobState_Failure: | |
416 assert(!IsRetryJob(job) && !IsPendingJob(job) && IsCompletedJob(job)); | |
417 break; | |
418 | |
419 case JobState_Retry: | |
420 assert(IsRetryJob(job) && !IsPendingJob(job) && !IsCompletedJob(job)); | |
421 break; | |
422 | |
423 case JobState_Running: | |
424 case JobState_Paused: | |
425 assert(!IsRetryJob(job) && !IsPendingJob(job) && !IsCompletedJob(job)); | |
426 break; | |
427 | |
428 default: | |
429 throw OrthancException(ErrorCode_InternalError); | |
430 } | |
431 } | |
432 } | |
433 #endif | |
434 | |
435 | |
436 void JobsRegistry::ForgetOldCompletedJobs() | |
437 { | |
438 if (maxCompletedJobs_ != 0) | |
439 { | |
440 while (completedJobs_.size() > maxCompletedJobs_) | |
441 { | |
442 assert(completedJobs_.front() != NULL); | |
443 | |
444 std::string id = completedJobs_.front()->GetId(); | |
445 assert(jobsIndex_.find(id) != jobsIndex_.end()); | |
446 | |
447 jobsIndex_.erase(id); | |
448 delete(completedJobs_.front()); | |
449 completedJobs_.pop_front(); | |
450 } | |
451 } | |
452 } | |
453 | |
454 | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
455 void JobsRegistry::SetCompletedJob(JobHandler& job, |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
456 bool success) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
457 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
458 job.SetState(success ? JobState_Success : JobState_Failure); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
459 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
460 completedJobs_.push_back(&job); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
461 ForgetOldCompletedJobs(); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
462 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
463 someJobComplete_.notify_all(); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
464 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
465 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
466 |
2569 | 467 void JobsRegistry::MarkRunningAsCompleted(JobHandler& job, |
468 bool success) | |
469 { | |
470 LOG(INFO) << "Job has completed with " << (success ? "success" : "failure") | |
471 << ": " << job.GetId(); | |
472 | |
473 CheckInvariants(); | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
474 |
2569 | 475 assert(job.GetState() == JobState_Running); |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
476 SetCompletedJob(job, success); |
2570
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
477 |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
478 if (observer_ != NULL) |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
479 { |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
480 if (success) |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
481 { |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
482 observer_->SignalJobSuccess(job.GetId()); |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
483 } |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
484 else |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
485 { |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
486 observer_->SignalJobFailure(job.GetId()); |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
487 } |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
488 } |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
489 |
2569 | 490 CheckInvariants(); |
491 } | |
492 | |
493 | |
494 void JobsRegistry::MarkRunningAsRetry(JobHandler& job, | |
495 unsigned int timeout) | |
496 { | |
497 LOG(INFO) << "Job scheduled for retry in " << timeout << "ms: " << job.GetId(); | |
498 | |
499 CheckInvariants(); | |
500 | |
501 assert(job.GetState() == JobState_Running && | |
502 retryJobs_.find(&job) == retryJobs_.end()); | |
503 | |
504 retryJobs_.insert(&job); | |
505 job.SetRetryState(timeout); | |
506 | |
507 CheckInvariants(); | |
508 } | |
509 | |
510 | |
511 void JobsRegistry::MarkRunningAsPaused(JobHandler& job) | |
512 { | |
513 LOG(INFO) << "Job paused: " << job.GetId(); | |
514 | |
515 CheckInvariants(); | |
516 assert(job.GetState() == JobState_Running); | |
517 | |
518 job.SetState(JobState_Paused); | |
519 | |
520 CheckInvariants(); | |
521 } | |
522 | |
523 | |
2570
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
524 bool JobsRegistry::GetStateInternal(JobState& state, |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
525 const std::string& id) |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
526 { |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
527 CheckInvariants(); |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
528 |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
529 JobsIndex::const_iterator it = jobsIndex_.find(id); |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
530 if (it == jobsIndex_.end()) |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
531 { |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
532 return false; |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
533 } |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
534 else |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
535 { |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
536 state = it->second->GetState(); |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
537 return true; |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
538 } |
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 |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
541 |
2569 | 542 JobsRegistry::~JobsRegistry() |
543 { | |
544 for (JobsIndex::iterator it = jobsIndex_.begin(); it != jobsIndex_.end(); ++it) | |
545 { | |
546 assert(it->second != NULL); | |
547 delete it->second; | |
548 } | |
549 } | |
550 | |
551 | |
2620
1232922c8793
speeding up shutdown if Lua script is in trailing phase
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2600
diff
changeset
|
552 void JobsRegistry::SetMaxCompletedJobs(size_t n) |
2569 | 553 { |
554 boost::mutex::scoped_lock lock(mutex_); | |
555 CheckInvariants(); | |
556 | |
2620
1232922c8793
speeding up shutdown if Lua script is in trailing phase
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2600
diff
changeset
|
557 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
|
558 |
1232922c8793
speeding up shutdown if Lua script is in trailing phase
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2600
diff
changeset
|
559 maxCompletedJobs_ = n; |
2569 | 560 ForgetOldCompletedJobs(); |
561 | |
562 CheckInvariants(); | |
563 } | |
564 | |
565 | |
566 void JobsRegistry::ListJobs(std::set<std::string>& target) | |
567 { | |
568 boost::mutex::scoped_lock lock(mutex_); | |
569 CheckInvariants(); | |
570 | |
571 for (JobsIndex::const_iterator it = jobsIndex_.begin(); | |
572 it != jobsIndex_.end(); ++it) | |
573 { | |
574 target.insert(it->first); | |
575 } | |
576 } | |
577 | |
578 | |
579 bool JobsRegistry::GetJobInfo(JobInfo& target, | |
580 const std::string& id) | |
581 { | |
582 boost::mutex::scoped_lock lock(mutex_); | |
583 CheckInvariants(); | |
584 | |
585 JobsIndex::const_iterator found = jobsIndex_.find(id); | |
586 | |
587 if (found == jobsIndex_.end()) | |
588 { | |
589 return false; | |
590 } | |
591 else | |
592 { | |
593 const JobHandler& handler = *found->second; | |
594 target = JobInfo(handler.GetId(), | |
595 handler.GetPriority(), | |
596 handler.GetState(), | |
597 handler.GetLastStatus(), | |
598 handler.GetCreationTime(), | |
599 handler.GetLastStateChangeTime(), | |
600 handler.GetRuntime()); | |
601 return true; | |
602 } | |
603 } | |
604 | |
605 | |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
606 void JobsRegistry::SubmitInternal(std::string& id, |
2669
eaf10085ffa1
no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2668
diff
changeset
|
607 JobHandler* handlerRaw, |
eaf10085ffa1
no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2668
diff
changeset
|
608 bool keepLastChangeTime) |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2630
diff
changeset
|
609 { |
2668
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2667
diff
changeset
|
610 if (handlerRaw == NULL) |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2667
diff
changeset
|
611 { |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2667
diff
changeset
|
612 throw OrthancException(ErrorCode_NullPointer); |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2667
diff
changeset
|
613 } |
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2667
diff
changeset
|
614 |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
615 std::auto_ptr<JobHandler> handler(handlerRaw); |
2569 | 616 |
2669
eaf10085ffa1
no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2668
diff
changeset
|
617 boost::posix_time::ptime lastChangeTime = handler->GetLastStateChangeTime(); |
eaf10085ffa1
no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2668
diff
changeset
|
618 |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
619 { |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
620 boost::mutex::scoped_lock lock(mutex_); |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
621 CheckInvariants(); |
2569 | 622 |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
623 id = handler->GetId(); |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
624 int priority = handler->GetPriority(); |
2569 | 625 |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
626 switch (handler->GetState()) |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
627 { |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
628 case JobState_Pending: |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
629 case JobState_Retry: |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
630 case JobState_Running: |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
631 handler->SetState(JobState_Pending); |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
632 pendingJobs_.push(handler.get()); |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
633 pendingJobAvailable_.notify_one(); |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
634 break; |
2668
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2667
diff
changeset
|
635 |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
636 case JobState_Success: |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
637 SetCompletedJob(*handler, true); |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
638 break; |
2668
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2667
diff
changeset
|
639 |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
640 case JobState_Failure: |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
641 SetCompletedJob(*handler, false); |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
642 break; |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
643 |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
644 case JobState_Paused: |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
645 break; |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
646 |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
647 default: |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
648 LOG(ERROR) << "A job should not be loaded from state: " |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
649 << EnumerationToString(handler->GetState()); |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
650 throw OrthancException(ErrorCode_InternalError); |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
651 } |
2668
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2667
diff
changeset
|
652 |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
653 if (keepLastChangeTime) |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
654 { |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
655 handler->SetLastStateChangeTime(lastChangeTime); |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
656 } |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
657 |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
658 jobsIndex_.insert(std::make_pair(id, handler.release())); |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
659 |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
660 LOG(INFO) << "New job submitted with priority " << priority << ": " << id; |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
661 |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
662 if (observer_ != NULL) |
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 observer_->SignalJobSubmitted(id); |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
665 } |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
666 |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
667 CheckInvariants(); |
2668
d26dd081df97
saving jobs engine on exit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2667
diff
changeset
|
668 } |
2569 | 669 } |
670 | |
671 | |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
672 void JobsRegistry::Submit(std::string& id, |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
673 IJob* job, // Takes ownership |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
674 int priority) |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
675 { |
2669
eaf10085ffa1
no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2668
diff
changeset
|
676 SubmitInternal(id, new JobHandler(job, priority), false); |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
677 } |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
678 |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
679 |
2569 | 680 void JobsRegistry::Submit(IJob* job, // Takes ownership |
681 int priority) | |
682 { | |
683 std::string id; | |
2669
eaf10085ffa1
no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2668
diff
changeset
|
684 SubmitInternal(id, new JobHandler(job, priority), false); |
2569 | 685 } |
686 | |
687 | |
2570
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
688 bool JobsRegistry::SubmitAndWait(IJob* job, // Takes ownership |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
689 int priority) |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
690 { |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
691 std::string id; |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
692 Submit(id, job, priority); |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
693 |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
694 JobState state; |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
695 |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
696 { |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
697 boost::mutex::scoped_lock lock(mutex_); |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
698 |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
699 while (GetStateInternal(state, id) && |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
700 state != JobState_Success && |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
701 state != JobState_Failure) |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
702 { |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
703 someJobComplete_.wait(lock); |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
704 } |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
705 } |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
706 |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
707 return (state == JobState_Success); |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
708 } |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
709 |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
710 |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
711 bool JobsRegistry::SetPriority(const std::string& id, |
2569 | 712 int priority) |
713 { | |
714 LOG(INFO) << "Changing priority to " << priority << " for job: " << id; | |
715 | |
716 boost::mutex::scoped_lock lock(mutex_); | |
717 CheckInvariants(); | |
718 | |
719 JobsIndex::iterator found = jobsIndex_.find(id); | |
720 | |
721 if (found == jobsIndex_.end()) | |
722 { | |
723 LOG(WARNING) << "Unknown job: " << id; | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
724 return false; |
2569 | 725 } |
726 else | |
727 { | |
728 found->second->SetPriority(priority); | |
729 | |
730 if (found->second->GetState() == JobState_Pending) | |
731 { | |
732 // If the job is pending, we need to reconstruct the | |
733 // priority queue, as the heap condition has changed | |
734 | |
735 PendingJobs copy; | |
736 std::swap(copy, pendingJobs_); | |
737 | |
738 assert(pendingJobs_.empty()); | |
739 while (!copy.empty()) | |
740 { | |
741 pendingJobs_.push(copy.top()); | |
742 copy.pop(); | |
743 } | |
744 } | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
745 |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
746 CheckInvariants(); |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
747 return true; |
2569 | 748 } |
749 } | |
750 | |
751 | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
752 void JobsRegistry::RemovePendingJob(const std::string& id) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
753 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
754 // If the job is pending, we need to reconstruct the priority |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
755 // queue to remove it |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
756 PendingJobs copy; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
757 std::swap(copy, pendingJobs_); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
758 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
759 assert(pendingJobs_.empty()); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
760 while (!copy.empty()) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
761 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
762 if (copy.top()->GetId() != id) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
763 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
764 pendingJobs_.push(copy.top()); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
765 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
766 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
767 copy.pop(); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
768 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
769 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
770 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
771 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
772 void JobsRegistry::RemoveRetryJob(JobHandler* handler) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
773 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
774 RetryJobs::iterator item = retryJobs_.find(handler); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
775 assert(item != retryJobs_.end()); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
776 retryJobs_.erase(item); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
777 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
778 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
779 |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
780 bool JobsRegistry::Pause(const std::string& id) |
2569 | 781 { |
782 LOG(INFO) << "Pausing job: " << id; | |
783 | |
784 boost::mutex::scoped_lock lock(mutex_); | |
785 CheckInvariants(); | |
786 | |
787 JobsIndex::iterator found = jobsIndex_.find(id); | |
788 | |
789 if (found == jobsIndex_.end()) | |
790 { | |
791 LOG(WARNING) << "Unknown job: " << id; | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
792 return false; |
2569 | 793 } |
794 else | |
795 { | |
796 switch (found->second->GetState()) | |
797 { | |
798 case JobState_Pending: | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
799 RemovePendingJob(id); |
2569 | 800 found->second->SetState(JobState_Paused); |
801 break; | |
802 | |
803 case JobState_Retry: | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
804 RemoveRetryJob(found->second); |
2569 | 805 found->second->SetState(JobState_Paused); |
806 break; | |
807 | |
808 case JobState_Paused: | |
809 case JobState_Success: | |
810 case JobState_Failure: | |
811 // Nothing to be done | |
812 break; | |
813 | |
814 case JobState_Running: | |
815 found->second->SchedulePause(); | |
816 break; | |
817 | |
818 default: | |
819 throw OrthancException(ErrorCode_InternalError); | |
820 } | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
821 |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
822 CheckInvariants(); |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
823 return true; |
2569 | 824 } |
825 } | |
826 | |
827 | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
828 bool JobsRegistry::Cancel(const std::string& id) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
829 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
830 LOG(INFO) << "Canceling job: " << id; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
831 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
832 boost::mutex::scoped_lock lock(mutex_); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
833 CheckInvariants(); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
834 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
835 JobsIndex::iterator found = jobsIndex_.find(id); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
836 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
837 if (found == jobsIndex_.end()) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
838 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
839 LOG(WARNING) << "Unknown job: " << id; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
840 return false; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
841 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
842 else |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
843 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
844 switch (found->second->GetState()) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
845 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
846 case JobState_Pending: |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
847 RemovePendingJob(id); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
848 SetCompletedJob(*found->second, false); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
849 found->second->SetLastErrorCode(ErrorCode_CanceledJob); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
850 break; |
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 case JobState_Retry: |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
853 RemoveRetryJob(found->second); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
854 SetCompletedJob(*found->second, false); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
855 found->second->SetLastErrorCode(ErrorCode_CanceledJob); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
856 break; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
857 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
858 case JobState_Paused: |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
859 SetCompletedJob(*found->second, false); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
860 found->second->SetLastErrorCode(ErrorCode_CanceledJob); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
861 break; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
862 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
863 case JobState_Success: |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
864 case JobState_Failure: |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
865 // Nothing to be done |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
866 break; |
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 case JobState_Running: |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
869 found->second->ScheduleCancel(); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
870 break; |
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 default: |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
873 throw OrthancException(ErrorCode_InternalError); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
874 } |
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 CheckInvariants(); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
877 return true; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
878 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
879 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
880 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
881 |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
882 bool JobsRegistry::Resume(const std::string& id) |
2569 | 883 { |
884 LOG(INFO) << "Resuming job: " << id; | |
885 | |
886 boost::mutex::scoped_lock lock(mutex_); | |
887 CheckInvariants(); | |
888 | |
889 JobsIndex::iterator found = jobsIndex_.find(id); | |
890 | |
891 if (found == jobsIndex_.end()) | |
892 { | |
893 LOG(WARNING) << "Unknown job: " << id; | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
894 return false; |
2569 | 895 } |
896 else if (found->second->GetState() != JobState_Paused) | |
897 { | |
898 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
|
899 return false; |
2569 | 900 } |
901 else | |
902 { | |
903 found->second->SetState(JobState_Pending); | |
904 pendingJobs_.push(found->second); | |
905 pendingJobAvailable_.notify_one(); | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
906 CheckInvariants(); |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
907 return true; |
2569 | 908 } |
909 } | |
910 | |
911 | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
912 bool JobsRegistry::Resubmit(const std::string& id) |
2569 | 913 { |
914 LOG(INFO) << "Resubmitting failed job: " << id; | |
915 | |
916 boost::mutex::scoped_lock lock(mutex_); | |
917 CheckInvariants(); | |
918 | |
919 JobsIndex::iterator found = jobsIndex_.find(id); | |
920 | |
921 if (found == jobsIndex_.end()) | |
922 { | |
923 LOG(WARNING) << "Unknown job: " << id; | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
924 return false; |
2569 | 925 } |
926 else if (found->second->GetState() != JobState_Failure) | |
927 { | |
928 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
|
929 return false; |
2569 | 930 } |
931 else | |
932 { | |
2583
1b6a6d80b6f2
OrthancPeerStoreJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2581
diff
changeset
|
933 found->second->GetJob().SignalResubmit(); |
1b6a6d80b6f2
OrthancPeerStoreJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2581
diff
changeset
|
934 |
2569 | 935 bool ok = false; |
936 for (CompletedJobs::iterator it = completedJobs_.begin(); | |
937 it != completedJobs_.end(); ++it) | |
938 { | |
939 if (*it == found->second) | |
940 { | |
941 ok = true; | |
942 completedJobs_.erase(it); | |
943 break; | |
944 } | |
945 } | |
946 | |
947 assert(ok); | |
948 | |
949 found->second->SetState(JobState_Pending); | |
950 pendingJobs_.push(found->second); | |
951 pendingJobAvailable_.notify_one(); | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
952 |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
953 CheckInvariants(); |
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
954 return true; |
2569 | 955 } |
956 } | |
957 | |
958 | |
959 void JobsRegistry::ScheduleRetries() | |
960 { | |
961 boost::mutex::scoped_lock lock(mutex_); | |
962 CheckInvariants(); | |
963 | |
964 RetryJobs copy; | |
965 std::swap(copy, retryJobs_); | |
966 | |
967 const boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time(); | |
968 | |
969 assert(retryJobs_.empty()); | |
970 for (RetryJobs::iterator it = copy.begin(); it != copy.end(); ++it) | |
971 { | |
972 if ((*it)->IsRetryReady(now)) | |
973 { | |
974 LOG(INFO) << "Retrying job: " << (*it)->GetId(); | |
975 (*it)->SetState(JobState_Pending); | |
976 pendingJobs_.push(*it); | |
977 pendingJobAvailable_.notify_one(); | |
978 } | |
979 else | |
980 { | |
981 retryJobs_.insert(*it); | |
982 } | |
983 } | |
984 | |
985 CheckInvariants(); | |
986 } | |
987 | |
988 | |
989 bool JobsRegistry::GetState(JobState& state, | |
990 const std::string& id) | |
991 { | |
992 boost::mutex::scoped_lock lock(mutex_); | |
2570
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
993 return GetStateInternal(state, id); |
2569 | 994 } |
995 | |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
996 |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
997 void JobsRegistry::SetObserver(JobsRegistry::IObserver& observer) |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
998 { |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
999 boost::mutex::scoped_lock lock(mutex_); |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
1000 observer_ = &observer; |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
1001 } |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
1002 |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
1003 |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
1004 void JobsRegistry::ResetObserver() |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
1005 { |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
1006 boost::mutex::scoped_lock lock(mutex_); |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
1007 observer_ = NULL; |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
1008 } |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
1009 |
2569 | 1010 |
1011 JobsRegistry::RunningJob::RunningJob(JobsRegistry& registry, | |
1012 unsigned int timeout) : | |
1013 registry_(registry), | |
1014 handler_(NULL), | |
1015 targetState_(JobState_Failure), | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1016 targetRetryTimeout_(0), |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1017 canceled_(false) |
2569 | 1018 { |
1019 { | |
1020 boost::mutex::scoped_lock lock(registry_.mutex_); | |
1021 | |
1022 while (registry_.pendingJobs_.empty()) | |
1023 { | |
1024 if (timeout == 0) | |
1025 { | |
1026 registry_.pendingJobAvailable_.wait(lock); | |
1027 } | |
1028 else | |
1029 { | |
1030 bool success = registry_.pendingJobAvailable_.timed_wait | |
1031 (lock, boost::posix_time::milliseconds(timeout)); | |
1032 if (!success) | |
1033 { | |
1034 // No pending job | |
1035 return; | |
1036 } | |
1037 } | |
1038 } | |
1039 | |
1040 handler_ = registry_.pendingJobs_.top(); | |
1041 registry_.pendingJobs_.pop(); | |
1042 | |
1043 assert(handler_->GetState() == JobState_Pending); | |
1044 handler_->SetState(JobState_Running); | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1045 handler_->SetLastErrorCode(ErrorCode_Success); |
2569 | 1046 |
1047 job_ = &handler_->GetJob(); | |
1048 id_ = handler_->GetId(); | |
1049 priority_ = handler_->GetPriority(); | |
1050 } | |
1051 } | |
1052 | |
1053 | |
1054 JobsRegistry::RunningJob::~RunningJob() | |
1055 { | |
1056 if (IsValid()) | |
1057 { | |
1058 boost::mutex::scoped_lock lock(registry_.mutex_); | |
1059 | |
1060 switch (targetState_) | |
1061 { | |
1062 case JobState_Failure: | |
1063 registry_.MarkRunningAsCompleted(*handler_, false); | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1064 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1065 if (canceled_) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1066 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1067 handler_->SetLastErrorCode(ErrorCode_CanceledJob); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1068 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1069 |
2569 | 1070 break; |
1071 | |
1072 case JobState_Success: | |
1073 registry_.MarkRunningAsCompleted(*handler_, true); | |
1074 break; | |
1075 | |
1076 case JobState_Paused: | |
1077 registry_.MarkRunningAsPaused(*handler_); | |
1078 break; | |
1079 | |
1080 case JobState_Retry: | |
1081 registry_.MarkRunningAsRetry(*handler_, targetRetryTimeout_); | |
1082 break; | |
1083 | |
1084 default: | |
1085 assert(0); | |
1086 } | |
1087 } | |
1088 } | |
1089 | |
1090 | |
1091 bool JobsRegistry::RunningJob::IsValid() const | |
1092 { | |
1093 return (handler_ != NULL && | |
1094 job_ != NULL); | |
1095 } | |
1096 | |
1097 | |
1098 const std::string& JobsRegistry::RunningJob::GetId() const | |
1099 { | |
1100 if (!IsValid()) | |
1101 { | |
1102 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
1103 } | |
1104 else | |
1105 { | |
1106 return id_; | |
1107 } | |
1108 } | |
1109 | |
1110 | |
1111 int JobsRegistry::RunningJob::GetPriority() const | |
1112 { | |
1113 if (!IsValid()) | |
1114 { | |
1115 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
1116 } | |
1117 else | |
1118 { | |
1119 return priority_; | |
1120 } | |
1121 } | |
1122 | |
1123 | |
1124 IJob& JobsRegistry::RunningJob::GetJob() | |
1125 { | |
1126 if (!IsValid()) | |
1127 { | |
1128 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
1129 } | |
1130 else | |
1131 { | |
1132 return *job_; | |
1133 } | |
1134 } | |
1135 | |
1136 | |
1137 bool JobsRegistry::RunningJob::IsPauseScheduled() | |
1138 { | |
1139 if (!IsValid()) | |
1140 { | |
1141 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
1142 } | |
1143 else | |
1144 { | |
1145 boost::mutex::scoped_lock lock(registry_.mutex_); | |
1146 registry_.CheckInvariants(); | |
1147 assert(handler_->GetState() == JobState_Running); | |
1148 | |
1149 return handler_->IsPauseScheduled(); | |
1150 } | |
1151 } | |
1152 | |
1153 | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1154 bool JobsRegistry::RunningJob::IsCancelScheduled() |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1155 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1156 if (!IsValid()) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1157 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1158 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1159 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1160 else |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1161 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1162 boost::mutex::scoped_lock lock(registry_.mutex_); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1163 registry_.CheckInvariants(); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1164 assert(handler_->GetState() == JobState_Running); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1165 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1166 return handler_->IsCancelScheduled(); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1167 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1168 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1169 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1170 |
2569 | 1171 void JobsRegistry::RunningJob::MarkSuccess() |
1172 { | |
1173 if (!IsValid()) | |
1174 { | |
1175 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
1176 } | |
1177 else | |
1178 { | |
1179 targetState_ = JobState_Success; | |
1180 } | |
1181 } | |
1182 | |
1183 | |
1184 void JobsRegistry::RunningJob::MarkFailure() | |
1185 { | |
1186 if (!IsValid()) | |
1187 { | |
1188 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
1189 } | |
1190 else | |
1191 { | |
1192 targetState_ = JobState_Failure; | |
1193 } | |
1194 } | |
1195 | |
1196 | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1197 void JobsRegistry::RunningJob::MarkCanceled() |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1198 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1199 if (!IsValid()) |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1200 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1201 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1202 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1203 else |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1204 { |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1205 targetState_ = JobState_Failure; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1206 canceled_ = true; |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1207 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1208 } |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1209 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
1210 |
2569 | 1211 void JobsRegistry::RunningJob::MarkPause() |
1212 { | |
1213 if (!IsValid()) | |
1214 { | |
1215 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
1216 } | |
1217 else | |
1218 { | |
1219 targetState_ = JobState_Paused; | |
1220 } | |
1221 } | |
1222 | |
1223 | |
1224 void JobsRegistry::RunningJob::MarkRetry(unsigned int timeout) | |
1225 { | |
1226 if (!IsValid()) | |
1227 { | |
1228 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
1229 } | |
1230 else | |
1231 { | |
1232 targetState_ = JobState_Retry; | |
1233 targetRetryTimeout_ = timeout; | |
1234 } | |
1235 } | |
1236 | |
1237 | |
1238 void JobsRegistry::RunningJob::UpdateStatus(ErrorCode code) | |
1239 { | |
1240 if (!IsValid()) | |
1241 { | |
1242 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
1243 } | |
1244 else | |
1245 { | |
1246 JobStatus status(code, *job_); | |
1247 | |
1248 boost::mutex::scoped_lock lock(registry_.mutex_); | |
1249 registry_.CheckInvariants(); | |
1250 assert(handler_->GetState() == JobState_Running); | |
1251 | |
1252 handler_->SetLastStatus(status); | |
1253 } | |
1254 } | |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1255 |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1256 |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1257 |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1258 void JobsRegistry::Serialize(Json::Value& target) |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1259 { |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1260 boost::mutex::scoped_lock lock(mutex_); |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1261 CheckInvariants(); |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1262 |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1263 target = Json::objectValue; |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1264 target[TYPE] = JOBS_REGISTRY; |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1265 target[MAX_COMPLETED_JOBS] = static_cast<unsigned int>(maxCompletedJobs_); |
2670
c5646f766b3e
serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
1266 target[JOBS] = Json::objectValue; |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1267 |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1268 for (JobsIndex::const_iterator it = jobsIndex_.begin(); |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1269 it != jobsIndex_.end(); ++it) |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1270 { |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1271 Json::Value v; |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1272 if (it->second->Serialize(v)) |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1273 { |
2670
c5646f766b3e
serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
1274 target[JOBS][it->first] = v; |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1275 } |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1276 } |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1277 } |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1278 |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1279 |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1280 JobsRegistry::JobsRegistry(IJobUnserializer& unserializer, |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
1281 const Json::Value& s) : |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2670
diff
changeset
|
1282 observer_(NULL) |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1283 { |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1284 if (SerializationToolbox::ReadString(s, TYPE) != JOBS_REGISTRY || |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1285 !s.isMember(JOBS) || |
2670
c5646f766b3e
serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
1286 s[JOBS].type() != Json::objectValue) |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1287 { |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1288 throw OrthancException(ErrorCode_BadFileFormat); |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1289 } |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1290 |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1291 maxCompletedJobs_ = SerializationToolbox::ReadUnsignedInteger(s, MAX_COMPLETED_JOBS); |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1292 |
2670
c5646f766b3e
serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
1293 Json::Value::Members members = s[JOBS].getMemberNames(); |
c5646f766b3e
serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
1294 |
c5646f766b3e
serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
1295 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
|
1296 it != members.end(); ++it) |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1297 { |
2670
c5646f766b3e
serialize jobs as an object
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
1298 std::auto_ptr<JobHandler> job(new JobHandler(unserializer, s[JOBS][*it], *it)); |
2669
eaf10085ffa1
no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2668
diff
changeset
|
1299 |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1300 std::string id; |
2669
eaf10085ffa1
no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2668
diff
changeset
|
1301 SubmitInternal(id, job.release(), true); |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1302 } |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2663
diff
changeset
|
1303 } |
2569 | 1304 } |