Mercurial > hg > orthanc
annotate Plugins/Engine/PluginsJob.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 | 1e8c4ecd02f4 |
children | ea7aea6f6a95 |
rev | line source |
---|---|
2810 | 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 "../../OrthancServer/PrecompiledHeadersServer.h" | |
35 #include "PluginsJob.h" | |
36 | |
37 #if ORTHANC_ENABLE_PLUGINS != 1 | |
38 #error The plugin support is disabled | |
39 #endif | |
40 | |
41 | |
42 #include "../../Core/OrthancException.h" | |
43 | |
44 #include <json/reader.h> | |
45 #include <cassert> | |
46 | |
47 namespace Orthanc | |
48 { | |
49 PluginsJob::PluginsJob(const _OrthancPluginSubmitJob& parameters) : | |
50 job_(parameters.job_), | |
51 free_(parameters.free_), | |
52 getProgress_(parameters.getProgress_), | |
53 step_(parameters.step_), | |
54 releaseResources_(parameters.releaseResources_), | |
55 reset_(parameters.reset_) | |
56 { | |
57 if (job_ == NULL || | |
58 parameters.type_ == NULL || | |
59 free_ == NULL || | |
60 getProgress_ == NULL || | |
61 step_ == NULL || | |
62 releaseResources_ == NULL || | |
63 reset_ == NULL) | |
64 { | |
65 throw OrthancException(ErrorCode_NullPointer); | |
66 } | |
67 | |
68 type_.assign(parameters.type_); | |
69 | |
70 if (parameters.content_ == NULL) | |
71 { | |
72 publicContent_ = Json::objectValue; | |
73 } | |
74 else | |
75 { | |
76 Json::Reader reader; | |
77 if (!reader.parse(parameters.content_, publicContent_) || | |
78 publicContent_.type() != Json::objectValue) | |
79 { | |
80 free_(job_); | |
81 throw OrthancException(ErrorCode_BadFileFormat); | |
82 } | |
83 } | |
84 | |
85 if (parameters.serialized_ == NULL) | |
86 { | |
87 hasSerialized_ = false; | |
88 } | |
89 else | |
90 { | |
91 hasSerialized_ = true; | |
92 | |
93 Json::Reader reader; | |
94 if (!reader.parse(parameters.serialized_, serialized_) || | |
95 serialized_.type() != Json::objectValue || | |
96 !serialized_.isMember("Type") || | |
97 serialized_["Type"].type() != Json::stringValue) | |
98 { | |
99 free_(job_); | |
100 throw OrthancException(ErrorCode_BadFileFormat); | |
101 } | |
102 } | |
103 } | |
104 | |
105 PluginsJob::~PluginsJob() | |
106 { | |
107 assert(job_ != NULL); | |
108 free_(job_); | |
109 } | |
110 | |
111 JobStepResult PluginsJob::ExecuteStep() | |
112 { | |
2811
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
113 OrthancPluginJobStepStatus status = step_(job_); |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
114 |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
115 switch (status) |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
116 { |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
117 case OrthancPluginJobStepStatus_Success: |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
118 return JobStepResult::Success(); |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
119 |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
120 case OrthancPluginJobStepStatus_Failure: |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
121 return JobStepResult::Failure(ErrorCode_Plugin); |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
122 |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
123 case OrthancPluginJobStepStatus_Continue: |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
124 return JobStepResult::Continue(); |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
125 |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
126 default: |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
127 throw OrthancException(ErrorCode_ParameterOutOfRange); |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
128 } |
2810 | 129 } |
130 | |
131 void PluginsJob::SignalResubmit() | |
132 { | |
133 reset_(job_); | |
134 } | |
135 | |
2811
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
136 void PluginsJob::ReleaseResources(JobReleaseReason reason) |
2810 | 137 { |
2811
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
138 switch (reason) |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
139 { |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
140 case JobReleaseReason_Success: |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
141 releaseResources_(job_, OrthancPluginJobReleaseReason_Success); |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
142 break; |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
143 |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
144 case JobReleaseReason_Failure: |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
145 releaseResources_(job_, OrthancPluginJobReleaseReason_Failure); |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
146 break; |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
147 |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
148 case JobReleaseReason_Canceled: |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
149 releaseResources_(job_, OrthancPluginJobReleaseReason_Canceled); |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
150 break; |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
151 |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
152 case JobReleaseReason_Paused: |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
153 releaseResources_(job_, OrthancPluginJobReleaseReason_Paused); |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
154 break; |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
155 |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
156 default: |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
157 throw OrthancException(ErrorCode_ParameterOutOfRange); |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
158 } |
2810 | 159 } |
160 | |
161 float PluginsJob::GetProgress() | |
162 { | |
163 return getProgress_(job_); | |
164 } | |
165 | |
166 bool PluginsJob::Serialize(Json::Value& value) | |
167 { | |
168 if (hasSerialized_) | |
169 { | |
170 value = serialized_; | |
171 return true; | |
172 } | |
173 else | |
174 { | |
175 return false; | |
176 } | |
177 } | |
178 } |