Mercurial > hg > orthanc
annotate OrthancServer/Plugins/Engine/PluginsJob.cpp @ 4570:648defffc8cc db-changes
new enum: TransactionType
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 08 Mar 2021 16:04:56 +0100 |
parents | d9473bd5ed43 |
children | f0038043fb97 7053502fbf97 |
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 | |
4437
d9473bd5ed43
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4392
diff
changeset
|
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium |
2810 | 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 | |
4045 | 34 #include "../../Sources/PrecompiledHeadersServer.h" |
2810 | 35 #include "PluginsJob.h" |
36 | |
37 #if ORTHANC_ENABLE_PLUGINS != 1 | |
38 #error The plugin support is disabled | |
39 #endif | |
40 | |
41 | |
4045 | 42 #include "../../../OrthancFramework/Sources/Logging.h" |
43 #include "../../../OrthancFramework/Sources/OrthancException.h" | |
4392
3af1d763763a
confining Json::Reader and Json::*Writer into Toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4045
diff
changeset
|
44 #include "../../../OrthancFramework/Sources/Toolbox.h" |
2810 | 45 |
46 #include <cassert> | |
47 | |
48 namespace Orthanc | |
49 { | |
2815
925d8dc03a23
unserialization of jobs from plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2814
diff
changeset
|
50 PluginsJob::PluginsJob(const _OrthancPluginCreateJob& parameters) : |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
51 parameters_(parameters) |
2810 | 52 { |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
53 if (parameters_.job == NULL) |
2810 | 54 { |
55 throw OrthancException(ErrorCode_NullPointer); | |
56 } | |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
57 |
2815
925d8dc03a23
unserialization of jobs from plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2814
diff
changeset
|
58 if (parameters_.target == NULL || |
925d8dc03a23
unserialization of jobs from plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2814
diff
changeset
|
59 parameters_.finalize == NULL || |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
60 parameters_.type == NULL || |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
61 parameters_.getProgress == NULL || |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
62 parameters_.getContent == NULL || |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
63 parameters_.getSerialized == NULL || |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
64 parameters_.step == NULL || |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
65 parameters_.stop == NULL || |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
66 parameters_.reset == NULL) |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
67 { |
2815
925d8dc03a23
unserialization of jobs from plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2814
diff
changeset
|
68 parameters_.finalize(parameters.job); |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
69 throw OrthancException(ErrorCode_NullPointer); |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
70 } |
2810 | 71 |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
72 type_.assign(parameters.type); |
2810 | 73 } |
74 | |
75 PluginsJob::~PluginsJob() | |
76 { | |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
77 assert(parameters_.job != NULL); |
2815
925d8dc03a23
unserialization of jobs from plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2814
diff
changeset
|
78 parameters_.finalize(parameters_.job); |
2810 | 79 } |
80 | |
3658
2d90dd30858c
providing job ID to the IJob::Step() methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
81 JobStepResult PluginsJob::Step(const std::string& jobId) |
2810 | 82 { |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
83 OrthancPluginJobStepStatus status = parameters_.step(parameters_.job); |
2811
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
84 |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
85 switch (status) |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
86 { |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
87 case OrthancPluginJobStepStatus_Success: |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
88 return JobStepResult::Success(); |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
89 |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
90 case OrthancPluginJobStepStatus_Failure: |
3240
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
91 return JobStepResult::Failure(ErrorCode_Plugin, NULL); |
2811
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
92 |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
93 case OrthancPluginJobStepStatus_Continue: |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
94 return JobStepResult::Continue(); |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
95 |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
96 default: |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
97 throw OrthancException(ErrorCode_ParameterOutOfRange); |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
98 } |
2810 | 99 } |
100 | |
2812
ea7aea6f6a95
improved naming of methods in IJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2811
diff
changeset
|
101 void PluginsJob::Reset() |
2810 | 102 { |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
103 parameters_.reset(parameters_.job); |
2810 | 104 } |
105 | |
2812
ea7aea6f6a95
improved naming of methods in IJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2811
diff
changeset
|
106 void PluginsJob::Stop(JobStopReason reason) |
2810 | 107 { |
2811
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
108 switch (reason) |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
109 { |
2812
ea7aea6f6a95
improved naming of methods in IJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2811
diff
changeset
|
110 case JobStopReason_Success: |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
111 parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Success); |
2811
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
112 break; |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
113 |
2812
ea7aea6f6a95
improved naming of methods in IJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2811
diff
changeset
|
114 case JobStopReason_Failure: |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
115 parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Failure); |
2811
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
116 break; |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
117 |
2812
ea7aea6f6a95
improved naming of methods in IJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2811
diff
changeset
|
118 case JobStopReason_Canceled: |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
119 parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Canceled); |
2811
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
120 break; |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
121 |
2812
ea7aea6f6a95
improved naming of methods in IJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2811
diff
changeset
|
122 case JobStopReason_Paused: |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
123 parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Paused); |
2811
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
124 break; |
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 float PluginsJob::GetProgress() | |
132 { | |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
133 return parameters_.getProgress(parameters_.job); |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
134 } |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
135 |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
136 void PluginsJob::GetPublicContent(Json::Value& value) |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
137 { |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
138 const char* content = parameters_.getContent(parameters_.job); |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
139 |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
140 if (content == NULL) |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
141 { |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
142 value = Json::objectValue; |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
143 } |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
144 else |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
145 { |
4392
3af1d763763a
confining Json::Reader and Json::*Writer into Toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4045
diff
changeset
|
146 if (!Toolbox::ReadJson(value, content) || |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
147 value.type() != Json::objectValue) |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
148 { |
2956 | 149 throw OrthancException(ErrorCode_Plugin, |
150 "A job plugin must provide a JSON object as its public content"); | |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
151 } |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
152 } |
2810 | 153 } |
154 | |
155 bool PluginsJob::Serialize(Json::Value& value) | |
156 { | |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
157 const char* serialized = parameters_.getSerialized(parameters_.job); |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
158 |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
159 if (serialized == NULL) |
2810 | 160 { |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
161 return false; |
2810 | 162 } |
163 else | |
164 { | |
4392
3af1d763763a
confining Json::Reader and Json::*Writer into Toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4045
diff
changeset
|
165 if (!Toolbox::ReadJson(value, serialized) || |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
166 value.type() != Json::objectValue) |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
167 { |
2956 | 168 throw OrthancException(ErrorCode_Plugin, |
169 "A job plugin must provide a JSON object as its serialized content"); | |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
170 } |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
171 |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
172 |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
173 static const char* KEY_TYPE = "Type"; |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
174 |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
175 if (value.isMember(KEY_TYPE)) |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
176 { |
2956 | 177 throw OrthancException(ErrorCode_Plugin, |
178 "The \"Type\" field is for reserved use for serialized job"); | |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
179 } |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
180 |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
181 value[KEY_TYPE] = type_; |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
182 return true; |
2810 | 183 } |
184 } | |
185 } |