comparison OrthancServer/Sources/ServerJobs/LuaJobManager.cpp @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents OrthancServer/ServerJobs/LuaJobManager.cpp@8f7ad4989fec
children 05b8fd21089c
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
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-2020 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 "../PrecompiledHeadersServer.h"
35 #include "LuaJobManager.h"
36
37 #include "../OrthancConfiguration.h"
38 #include "../../Core/Logging.h"
39
40 #include "../../Core/JobsEngine/Operations/LogJobOperation.h"
41 #include "Operations/DeleteResourceOperation.h"
42 #include "Operations/ModifyInstanceOperation.h"
43 #include "Operations/StorePeerOperation.h"
44 #include "Operations/StoreScuOperation.h"
45 #include "Operations/SystemCallOperation.h"
46
47 #include "../../Core/JobsEngine/Operations/NullOperationValue.h"
48 #include "../../Core/JobsEngine/Operations/StringOperationValue.h"
49 #include "Operations/DicomInstanceOperationValue.h"
50
51 namespace Orthanc
52 {
53 void LuaJobManager::SignalDone(const SequenceOfOperationsJob& job)
54 {
55 boost::mutex::scoped_lock lock(mutex_);
56
57 if (&job == currentJob_)
58 {
59 currentId_.clear();
60 currentJob_ = NULL;
61 }
62 }
63
64
65 LuaJobManager::LuaJobManager() :
66 currentJob_(NULL),
67 maxOperations_(1000),
68 priority_(0),
69 trailingTimeout_(5000)
70 {
71 unsigned int dicomTimeout;
72
73 {
74 OrthancConfiguration::ReaderLock lock;
75 dicomTimeout = lock.GetConfiguration().GetUnsignedIntegerParameter("DicomAssociationCloseDelay", 5);
76 }
77
78 connectionManager_.SetInactivityTimeout(dicomTimeout * 1000); // Milliseconds expected
79 LOG(INFO) << "Lua: DICOM associations will be closed after "
80 << dicomTimeout << " seconds of inactivity";
81 }
82
83
84 void LuaJobManager::SetMaxOperationsPerJob(size_t count)
85 {
86 boost::mutex::scoped_lock lock(mutex_);
87 maxOperations_ = count;
88 }
89
90
91 void LuaJobManager::SetPriority(int priority)
92 {
93 boost::mutex::scoped_lock lock(mutex_);
94 priority_ = priority;
95 }
96
97
98 void LuaJobManager::SetTrailingOperationTimeout(unsigned int timeout)
99 {
100 boost::mutex::scoped_lock lock(mutex_);
101 trailingTimeout_ = timeout;
102 }
103
104
105 void LuaJobManager::AwakeTrailingSleep()
106 {
107 boost::mutex::scoped_lock lock(mutex_);
108
109 LOG(INFO) << "Awaking trailing sleep";
110
111 if (currentJob_ != NULL)
112 {
113 currentJob_->AwakeTrailingSleep();
114 }
115 }
116
117
118 LuaJobManager::Lock::Lock(LuaJobManager& that,
119 JobsEngine& engine) :
120 that_(that),
121 lock_(that.mutex_),
122 engine_(engine)
123 {
124 if (that_.currentJob_ == NULL)
125 {
126 isNewJob_ = true;
127 }
128 else
129 {
130 jobLock_.reset(new SequenceOfOperationsJob::Lock(*that_.currentJob_));
131
132 if (jobLock_->IsDone() ||
133 jobLock_->GetOperationsCount() >= that_.maxOperations_)
134 {
135 jobLock_.reset(NULL);
136 isNewJob_ = true;
137 }
138 else
139 {
140 isNewJob_ = false;
141 }
142 }
143
144 if (isNewJob_)
145 {
146 // Need to create a new job, as the previous one is either
147 // finished, or is getting too long
148 that_.currentJob_ = new SequenceOfOperationsJob;
149 that_.currentJob_->Register(that_);
150 that_.currentJob_->SetDescription("Lua");
151
152 {
153 jobLock_.reset(new SequenceOfOperationsJob::Lock(*that_.currentJob_));
154 jobLock_->SetTrailingOperationTimeout(that_.trailingTimeout_);
155 }
156 }
157
158 assert(jobLock_.get() != NULL);
159 }
160
161
162 LuaJobManager::Lock::~Lock()
163 {
164 bool isEmpty;
165
166 assert(jobLock_.get() != NULL);
167 isEmpty = (isNewJob_ &&
168 jobLock_->GetOperationsCount() == 0);
169
170 jobLock_.reset(NULL);
171
172 if (isNewJob_)
173 {
174 if (isEmpty)
175 {
176 // No operation was added, discard the newly created job
177 isNewJob_ = false;
178 delete that_.currentJob_;
179 that_.currentJob_ = NULL;
180 }
181 else
182 {
183 engine_.GetRegistry().Submit(that_.currentId_, that_.currentJob_, that_.priority_);
184 }
185 }
186 }
187
188
189 size_t LuaJobManager::Lock::AddDeleteResourceOperation(ServerContext& context)
190 {
191 assert(jobLock_.get() != NULL);
192 return jobLock_->AddOperation(new DeleteResourceOperation(context));
193 }
194
195
196 size_t LuaJobManager::Lock::AddLogOperation()
197 {
198 assert(jobLock_.get() != NULL);
199 return jobLock_->AddOperation(new LogJobOperation);
200 }
201
202
203 size_t LuaJobManager::Lock::AddStoreScuOperation(ServerContext& context,
204 const std::string& localAet,
205 const RemoteModalityParameters& modality)
206 {
207 assert(jobLock_.get() != NULL);
208 return jobLock_->AddOperation(new StoreScuOperation(
209 context, that_.connectionManager_, localAet, modality));
210 }
211
212
213 size_t LuaJobManager::Lock::AddStorePeerOperation(const WebServiceParameters& peer)
214 {
215 assert(jobLock_.get() != NULL);
216 return jobLock_->AddOperation(new StorePeerOperation(peer));
217 }
218
219
220 size_t LuaJobManager::Lock::AddSystemCallOperation(const std::string& command)
221 {
222 assert(jobLock_.get() != NULL);
223 return jobLock_->AddOperation(new SystemCallOperation(command));
224 }
225
226
227 size_t LuaJobManager::Lock::AddSystemCallOperation
228 (const std::string& command,
229 const std::vector<std::string>& preArguments,
230 const std::vector<std::string>& postArguments)
231 {
232 assert(jobLock_.get() != NULL);
233 return jobLock_->AddOperation
234 (new SystemCallOperation(command, preArguments, postArguments));
235 }
236
237
238 size_t LuaJobManager::Lock::AddModifyInstanceOperation(ServerContext& context,
239 DicomModification* modification)
240 {
241 assert(jobLock_.get() != NULL);
242 return jobLock_->AddOperation
243 (new ModifyInstanceOperation(context, RequestOrigin_Lua, modification));
244 }
245
246
247 void LuaJobManager::Lock::AddNullInput(size_t operation)
248 {
249 assert(jobLock_.get() != NULL);
250 NullOperationValue null;
251 jobLock_->AddInput(operation, null);
252 }
253
254
255 void LuaJobManager::Lock::AddStringInput(size_t operation,
256 const std::string& content)
257 {
258 assert(jobLock_.get() != NULL);
259 StringOperationValue value(content);
260 jobLock_->AddInput(operation, value);
261 }
262
263
264 void LuaJobManager::Lock::AddDicomInstanceInput(size_t operation,
265 ServerContext& context,
266 const std::string& instanceId)
267 {
268 assert(jobLock_.get() != NULL);
269 DicomInstanceOperationValue value(context, instanceId);
270 jobLock_->AddInput(operation, value);
271 }
272
273
274 void LuaJobManager::Lock::Connect(size_t operation1,
275 size_t operation2)
276 {
277 assert(jobLock_.get() != NULL);
278 jobLock_->Connect(operation1, operation2);
279 }
280 }