Mercurial > hg > orthanc
annotate Core/JobsEngine/JobsRegistry.h @ 3860:9ccbbd55bc23
allow Keeping the SOPInstanceUID in /instances/../modify
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Fri, 24 Apr 2020 17:30:41 +0200 |
parents | 94f4a18a79cc |
children | a2e4edc7b9aa |
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 | |
3640
94f4a18a79cc
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3240
diff
changeset
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium |
2569 | 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 #pragma once | |
35 | |
36 #if !defined(ORTHANC_SANDBOXED) | |
37 # error The macro ORTHANC_SANDBOXED must be defined | |
38 #endif | |
39 | |
40 #if ORTHANC_SANDBOXED == 1 | |
41 # error The job engine cannot be used in sandboxed environments | |
42 #endif | |
43 | |
44 #include "JobInfo.h" | |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2581
diff
changeset
|
45 #include "IJobUnserializer.h" |
2569 | 46 |
47 #include <list> | |
48 #include <set> | |
49 #include <queue> | |
50 #include <boost/thread/mutex.hpp> | |
51 #include <boost/thread/condition_variable.hpp> | |
52 | |
53 namespace Orthanc | |
54 { | |
55 // This class handles the state machine of the jobs engine | |
56 class JobsRegistry : public boost::noncopyable | |
57 { | |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
58 public: |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
59 class IObserver : public boost::noncopyable |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
60 { |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
61 public: |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
62 virtual ~IObserver() |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
63 { |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
64 } |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
65 |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
66 virtual void SignalJobSubmitted(const std::string& jobId) = 0; |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
67 |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
68 virtual void SignalJobSuccess(const std::string& jobId) = 0; |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
69 |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
70 virtual void SignalJobFailure(const std::string& jobId) = 0; |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
71 }; |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
72 |
2569 | 73 private: |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2867
diff
changeset
|
74 enum CompletedReason |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2867
diff
changeset
|
75 { |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2867
diff
changeset
|
76 CompletedReason_Success, |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2867
diff
changeset
|
77 CompletedReason_Failure, |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2867
diff
changeset
|
78 CompletedReason_Canceled |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2867
diff
changeset
|
79 }; |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2867
diff
changeset
|
80 |
2569 | 81 class JobHandler; |
82 | |
83 struct PriorityComparator | |
84 { | |
85 bool operator() (JobHandler*& a, | |
86 JobHandler*& b) const; | |
87 }; | |
88 | |
89 typedef std::map<std::string, JobHandler*> JobsIndex; | |
90 typedef std::list<JobHandler*> CompletedJobs; | |
91 typedef std::set<JobHandler*> RetryJobs; | |
92 typedef std::priority_queue<JobHandler*, | |
93 std::vector<JobHandler*>, // Could be a "std::deque" | |
94 PriorityComparator> PendingJobs; | |
95 | |
96 boost::mutex mutex_; | |
97 JobsIndex jobsIndex_; | |
98 PendingJobs pendingJobs_; | |
99 CompletedJobs completedJobs_; | |
100 RetryJobs retryJobs_; | |
101 | |
102 boost::condition_variable pendingJobAvailable_; | |
2570
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
103 boost::condition_variable someJobComplete_; |
2569 | 104 size_t maxCompletedJobs_; |
105 | |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
106 IObserver* observer_; |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
107 |
2569 | 108 |
109 #ifndef NDEBUG | |
110 bool IsPendingJob(const JobHandler& job) const; | |
111 | |
112 bool IsCompletedJob(JobHandler& job) const; | |
113 | |
114 bool IsRetryJob(JobHandler& job) const; | |
115 #endif | |
116 | |
117 void CheckInvariants() const; | |
118 | |
119 void ForgetOldCompletedJobs(); | |
120 | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
121 void SetCompletedJob(JobHandler& job, |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
122 bool success); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
123 |
2569 | 124 void MarkRunningAsCompleted(JobHandler& job, |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2867
diff
changeset
|
125 CompletedReason reason); |
2569 | 126 |
127 void MarkRunningAsRetry(JobHandler& job, | |
128 unsigned int timeout); | |
129 | |
130 void MarkRunningAsPaused(JobHandler& job); | |
2570
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
131 |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
132 bool GetStateInternal(JobState& state, |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
133 const std::string& id); |
2569 | 134 |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
135 void RemovePendingJob(const std::string& id); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
136 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
137 void RemoveRetryJob(JobHandler* handler); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
138 |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2648
diff
changeset
|
139 void SubmitInternal(std::string& id, |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2867
diff
changeset
|
140 JobHandler* handler); |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2648
diff
changeset
|
141 |
2569 | 142 public: |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2867
diff
changeset
|
143 JobsRegistry(size_t maxCompletedJobs) : |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2867
diff
changeset
|
144 maxCompletedJobs_(maxCompletedJobs), |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
145 observer_(NULL) |
2569 | 146 { |
147 } | |
148 | |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2648
diff
changeset
|
149 JobsRegistry(IJobUnserializer& unserializer, |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2867
diff
changeset
|
150 const Json::Value& s, |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2867
diff
changeset
|
151 size_t maxCompletedJobs); |
2569 | 152 |
153 ~JobsRegistry(); | |
154 | |
155 void SetMaxCompletedJobs(size_t i); | |
156 | |
2950
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2867
diff
changeset
|
157 size_t GetMaxCompletedJobs(); |
dc18d5804746
support of JobsHistorySize set to zero
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2867
diff
changeset
|
158 |
2569 | 159 void ListJobs(std::set<std::string>& target); |
160 | |
161 bool GetJobInfo(JobInfo& target, | |
162 const std::string& id); | |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2581
diff
changeset
|
163 |
2976
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2950
diff
changeset
|
164 bool GetJobOutput(std::string& output, |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2950
diff
changeset
|
165 MimeType& mime, |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2950
diff
changeset
|
166 const std::string& job, |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2950
diff
changeset
|
167 const std::string& key); |
cb5d75143da0
Asynchronous generation of ZIP archives and DICOM medias
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2950
diff
changeset
|
168 |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2581
diff
changeset
|
169 void Serialize(Json::Value& target); |
2569 | 170 |
171 void Submit(std::string& id, | |
172 IJob* job, // Takes ownership | |
173 int priority); | |
174 | |
175 void Submit(IJob* job, // Takes ownership | |
176 int priority); | |
2570
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
177 |
3240
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3179
diff
changeset
|
178 void SubmitAndWait(Json::Value& successContent, |
2867 | 179 IJob* job, // Takes ownership |
2570
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
180 int priority); |
2569 | 181 |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
182 bool SetPriority(const std::string& id, |
2569 | 183 int priority); |
184 | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
185 bool Pause(const std::string& id); |
2569 | 186 |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
187 bool Resume(const std::string& id); |
2569 | 188 |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
189 bool Resubmit(const std::string& id); |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
190 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
191 bool Cancel(const std::string& id); |
2569 | 192 |
193 void ScheduleRetries(); | |
194 | |
195 bool GetState(JobState& state, | |
196 const std::string& id); | |
197 | |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
198 void SetObserver(IObserver& observer); |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
199 |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
200 void ResetObserver(); |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
201 |
3177
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
202 void GetStatistics(unsigned int& pending, |
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
203 unsigned int& running, |
3179
fca730c267d7
New primitives to set and refresh metrics
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3177
diff
changeset
|
204 unsigned int& success, |
fca730c267d7
New primitives to set and refresh metrics
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3177
diff
changeset
|
205 unsigned int& errors); |
3177
053e72ff9fc5
new metrics: orthanc_jobs_[pending|running|completed]
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
206 |
2569 | 207 class RunningJob : public boost::noncopyable |
208 { | |
209 private: | |
210 JobsRegistry& registry_; | |
211 JobHandler* handler_; // Can only be accessed if the | |
212 // registry mutex is locked! | |
213 IJob* job_; // Will by design be in mutual exclusion, | |
214 // because only one RunningJob can be | |
215 // executed at a time on a JobHandler | |
216 | |
217 std::string id_; | |
218 int priority_; | |
219 JobState targetState_; | |
220 unsigned int targetRetryTimeout_; | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
221 bool canceled_; |
2569 | 222 |
223 public: | |
224 RunningJob(JobsRegistry& registry, | |
225 unsigned int timeout); | |
226 | |
227 ~RunningJob(); | |
228 | |
229 bool IsValid() const; | |
230 | |
231 const std::string& GetId() const; | |
232 | |
233 int GetPriority() const; | |
234 | |
235 IJob& GetJob(); | |
236 | |
237 bool IsPauseScheduled(); | |
238 | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
239 bool IsCancelScheduled(); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
240 |
2569 | 241 void MarkSuccess(); |
242 | |
243 void MarkFailure(); | |
244 | |
245 void MarkPause(); | |
246 | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
247 void MarkCanceled(); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
248 |
2569 | 249 void MarkRetry(unsigned int timeout); |
250 | |
3240
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3179
diff
changeset
|
251 void UpdateStatus(ErrorCode code, |
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3179
diff
changeset
|
252 const std::string& details); |
2569 | 253 }; |
254 }; | |
255 } |