Mercurial > hg > orthanc
annotate Core/JobsEngine/SetOfInstancesJob.cpp @ 2811:7cfc8d266f41
reason for releasing resources in jobs
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 06 Sep 2018 12:32:02 +0200 |
parents | 33de4a82c466 |
children | ea7aea6f6a95 |
rev | line source |
---|---|
2585 | 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 "SetOfInstancesJob.h" | |
36 | |
37 #include "../OrthancException.h" | |
2656
a6d3e45eeff5
SerializationToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2655
diff
changeset
|
38 #include "../SerializationToolbox.h" |
2585 | 39 |
40 namespace Orthanc | |
41 { | |
42 SetOfInstancesJob::SetOfInstancesJob() : | |
43 started_(false), | |
44 permissive_(false), | |
45 position_(0) | |
46 { | |
47 } | |
48 | |
49 | |
50 void SetOfInstancesJob::Reserve(size_t size) | |
51 { | |
52 if (started_) | |
53 { | |
54 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
55 } | |
56 else | |
57 { | |
58 instances_.reserve(size); | |
59 } | |
60 } | |
61 | |
62 | |
63 void SetOfInstancesJob::AddInstance(const std::string& instance) | |
64 { | |
65 if (started_) | |
66 { | |
67 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
68 } | |
69 else | |
70 { | |
71 instances_.push_back(instance); | |
72 } | |
73 } | |
74 | |
75 | |
76 void SetOfInstancesJob::SetPermissive(bool permissive) | |
77 { | |
78 if (IsStarted()) | |
79 { | |
80 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
81 } | |
82 else | |
83 { | |
84 permissive_ = permissive; | |
85 } | |
86 } | |
87 | |
88 | |
89 void SetOfInstancesJob::SignalResubmit() | |
90 { | |
91 if (started_) | |
92 { | |
93 position_ = 0; | |
94 failedInstances_.clear(); | |
95 } | |
96 else | |
97 { | |
98 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
99 } | |
100 } | |
101 | |
102 | |
103 float SetOfInstancesJob::GetProgress() | |
104 { | |
105 if (instances_.size() == 0) | |
106 { | |
107 return 0; | |
108 } | |
109 else | |
110 { | |
111 return (static_cast<float>(position_) / | |
112 static_cast<float>(instances_.size())); | |
113 } | |
114 } | |
115 | |
116 | |
2657
5eea2f11e8df
JobsSerialization.GenericJobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
117 const std::string& SetOfInstancesJob::GetInstance(size_t index) const |
5eea2f11e8df
JobsSerialization.GenericJobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
118 { |
5eea2f11e8df
JobsSerialization.GenericJobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
119 if (index > instances_.size()) |
5eea2f11e8df
JobsSerialization.GenericJobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
120 { |
5eea2f11e8df
JobsSerialization.GenericJobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
121 throw OrthancException(ErrorCode_ParameterOutOfRange); |
5eea2f11e8df
JobsSerialization.GenericJobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
122 } |
5eea2f11e8df
JobsSerialization.GenericJobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
123 else |
5eea2f11e8df
JobsSerialization.GenericJobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
124 { |
5eea2f11e8df
JobsSerialization.GenericJobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
125 return instances_[index]; |
5eea2f11e8df
JobsSerialization.GenericJobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
126 } |
5eea2f11e8df
JobsSerialization.GenericJobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
127 } |
5eea2f11e8df
JobsSerialization.GenericJobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
128 |
5eea2f11e8df
JobsSerialization.GenericJobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
129 |
2598
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2586
diff
changeset
|
130 JobStepResult SetOfInstancesJob::ExecuteStep() |
2585 | 131 { |
2586 | 132 if (!started_) |
133 { | |
134 throw OrthancException(ErrorCode_InternalError); | |
135 } | |
136 | |
137 if (instances_.empty() && | |
138 position_ == 0) | |
2585 | 139 { |
2586 | 140 // No instance to handle, we're done |
141 position_ = 1; | |
2598
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2586
diff
changeset
|
142 return JobStepResult::Success(); |
2586 | 143 } |
144 | |
145 if (position_ >= instances_.size()) | |
146 { | |
147 // Already done | |
2598
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2586
diff
changeset
|
148 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
2585 | 149 } |
150 | |
2586 | 151 const std::string currentInstance = instances_[position_]; |
152 | |
2585 | 153 bool ok; |
154 | |
155 try | |
156 { | |
2586 | 157 ok = HandleInstance(currentInstance); |
2585 | 158 |
159 if (!ok && !permissive_) | |
160 { | |
2598
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2586
diff
changeset
|
161 return JobStepResult::Failure(ErrorCode_InternalError); |
2585 | 162 } |
163 } | |
2701 | 164 catch (OrthancException&) |
2585 | 165 { |
166 if (permissive_) | |
167 { | |
168 ok = false; | |
169 } | |
170 else | |
171 { | |
172 throw; | |
173 } | |
174 } | |
175 | |
176 if (!ok) | |
177 { | |
2586 | 178 failedInstances_.insert(currentInstance); |
2585 | 179 } |
180 | |
2586 | 181 position_ += 1; |
2585 | 182 |
2586 | 183 if (position_ == instances_.size()) |
2585 | 184 { |
2586 | 185 // We're done |
2598
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2586
diff
changeset
|
186 return JobStepResult::Success(); |
2585 | 187 } |
188 else | |
189 { | |
2598
34dc57f4a7d2
simplification of JobStepResult
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2586
diff
changeset
|
190 return JobStepResult::Continue(); |
2585 | 191 } |
192 } | |
193 | |
194 | |
2640
c691fcf66071
ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2624
diff
changeset
|
195 void SetOfInstancesJob::GetPublicContent(Json::Value& value) |
c691fcf66071
ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2624
diff
changeset
|
196 { |
c691fcf66071
ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2624
diff
changeset
|
197 value["Description"] = GetDescription(); |
2657
5eea2f11e8df
JobsSerialization.GenericJobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
198 value["InstancesCount"] = static_cast<uint32_t>(instances_.size()); |
5eea2f11e8df
JobsSerialization.GenericJobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2656
diff
changeset
|
199 value["FailedInstancesCount"] = static_cast<uint32_t>(failedInstances_.size()); |
2640
c691fcf66071
ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2624
diff
changeset
|
200 } |
c691fcf66071
ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2624
diff
changeset
|
201 |
c691fcf66071
ResourceModificationJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2624
diff
changeset
|
202 |
2663
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
203 bool SetOfInstancesJob::Serialize(Json::Value& value) |
2585 | 204 { |
2655 | 205 value = Json::objectValue; |
206 | |
2652 | 207 std::string type; |
208 GetJobType(type); | |
209 value["Type"] = type; | |
210 | |
211 value["Permissive"] = permissive_; | |
212 value["Position"] = static_cast<unsigned int>(position_); | |
213 value["Description"] = description_; | |
2585 | 214 |
2656
a6d3e45eeff5
SerializationToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2655
diff
changeset
|
215 SerializationToolbox::WriteArrayOfStrings(value, instances_, "Instances"); |
a6d3e45eeff5
SerializationToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2655
diff
changeset
|
216 SerializationToolbox::WriteSetOfStrings(value, failedInstances_, "FailedInstances"); |
2663
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
217 |
228e2783ce83
some jobs might not be serializable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2657
diff
changeset
|
218 return true; |
2585 | 219 } |
2652 | 220 |
221 | |
222 SetOfInstancesJob::SetOfInstancesJob(const Json::Value& value) : | |
223 started_(false), | |
2656
a6d3e45eeff5
SerializationToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2655
diff
changeset
|
224 permissive_(SerializationToolbox::ReadBoolean(value, "Permissive")), |
a6d3e45eeff5
SerializationToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2655
diff
changeset
|
225 position_(SerializationToolbox::ReadUnsignedInteger(value, "Position")), |
a6d3e45eeff5
SerializationToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2655
diff
changeset
|
226 description_(SerializationToolbox::ReadString(value, "Description")) |
2652 | 227 { |
2656
a6d3e45eeff5
SerializationToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2655
diff
changeset
|
228 SerializationToolbox::ReadArrayOfStrings(instances_, value, "Instances"); |
a6d3e45eeff5
SerializationToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2655
diff
changeset
|
229 SerializationToolbox::ReadSetOfStrings(failedInstances_, value, "FailedInstances"); |
2652 | 230 |
231 if (position_ > instances_.size()) | |
232 { | |
233 throw OrthancException(ErrorCode_BadFileFormat); | |
234 } | |
235 } | |
2585 | 236 } |