Mercurial > hg > orthanc
annotate Core/JobsEngine/JobsRegistry.h @ 2694:8340596dc4a9 jobs
merge
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 29 Jun 2018 09:56:04 +0200 |
parents | 8e0bc055d18c |
children | 251614c2edac |
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 | |
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 #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: |
74 class JobHandler; | |
75 | |
76 struct PriorityComparator | |
77 { | |
78 bool operator() (JobHandler*& a, | |
79 JobHandler*& b) const; | |
80 }; | |
81 | |
82 typedef std::map<std::string, JobHandler*> JobsIndex; | |
83 typedef std::list<JobHandler*> CompletedJobs; | |
84 typedef std::set<JobHandler*> RetryJobs; | |
85 typedef std::priority_queue<JobHandler*, | |
86 std::vector<JobHandler*>, // Could be a "std::deque" | |
87 PriorityComparator> PendingJobs; | |
88 | |
89 boost::mutex mutex_; | |
90 JobsIndex jobsIndex_; | |
91 PendingJobs pendingJobs_; | |
92 CompletedJobs completedJobs_; | |
93 RetryJobs retryJobs_; | |
94 | |
95 boost::condition_variable pendingJobAvailable_; | |
2570
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
96 boost::condition_variable someJobComplete_; |
2569 | 97 size_t maxCompletedJobs_; |
98 | |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
99 IObserver* observer_; |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
100 |
2569 | 101 |
102 #ifndef NDEBUG | |
103 bool IsPendingJob(const JobHandler& job) const; | |
104 | |
105 bool IsCompletedJob(JobHandler& job) const; | |
106 | |
107 bool IsRetryJob(JobHandler& job) const; | |
108 #endif | |
109 | |
110 void CheckInvariants() const; | |
111 | |
112 void ForgetOldCompletedJobs(); | |
113 | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
114 void SetCompletedJob(JobHandler& job, |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
115 bool success); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
116 |
2569 | 117 void MarkRunningAsCompleted(JobHandler& job, |
118 bool success); | |
119 | |
120 void MarkRunningAsRetry(JobHandler& job, | |
121 unsigned int timeout); | |
122 | |
123 void MarkRunningAsPaused(JobHandler& job); | |
2570
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
124 |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
125 bool GetStateInternal(JobState& state, |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
126 const std::string& id); |
2569 | 127 |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
128 void RemovePendingJob(const std::string& id); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
129 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
130 void RemoveRetryJob(JobHandler* handler); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
131 |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2648
diff
changeset
|
132 void SubmitInternal(std::string& id, |
2669
eaf10085ffa1
no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2667
diff
changeset
|
133 JobHandler* handler, |
eaf10085ffa1
no passwords in public content of jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2667
diff
changeset
|
134 bool keepLastChangeTime); |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2648
diff
changeset
|
135 |
2569 | 136 public: |
137 JobsRegistry() : | |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
138 maxCompletedJobs_(10), |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
139 observer_(NULL) |
2569 | 140 { |
141 } | |
142 | |
2667
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2648
diff
changeset
|
143 JobsRegistry(IJobUnserializer& unserializer, |
5fa2f2ce74f0
serialization of JobsRegistry
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2648
diff
changeset
|
144 const Json::Value& s); |
2569 | 145 |
146 ~JobsRegistry(); | |
147 | |
148 void SetMaxCompletedJobs(size_t i); | |
149 | |
150 void ListJobs(std::set<std::string>& target); | |
151 | |
152 bool GetJobInfo(JobInfo& target, | |
153 const std::string& id); | |
2648
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2581
diff
changeset
|
154 |
e1893d31652a
serialization of JobHandler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2581
diff
changeset
|
155 void Serialize(Json::Value& target); |
2569 | 156 |
157 void Submit(std::string& id, | |
158 IJob* job, // Takes ownership | |
159 int priority); | |
160 | |
161 void Submit(IJob* job, // Takes ownership | |
162 int priority); | |
2570
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
163 |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
164 bool SubmitAndWait(IJob* job, // Takes ownership |
2e879c796ec7
JobsRegistry::SubmitAndWait(), StoreScuJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2569
diff
changeset
|
165 int priority); |
2569 | 166 |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
167 bool SetPriority(const std::string& id, |
2569 | 168 int priority); |
169 | |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
170 bool Pause(const std::string& id); |
2569 | 171 |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
172 bool Resume(const std::string& id); |
2569 | 173 |
2573
3372c5255333
StoreScuJob, Orthanc Explorer for jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2570
diff
changeset
|
174 bool Resubmit(const std::string& id); |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
175 |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
176 bool Cancel(const std::string& id); |
2569 | 177 |
178 void ScheduleRetries(); | |
179 | |
180 bool GetState(JobState& state, | |
181 const std::string& id); | |
182 | |
2673
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
183 void SetObserver(IObserver& observer); |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
184 |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
185 void ResetObserver(); |
8e0bc055d18c
JobsRegistry::IObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2669
diff
changeset
|
186 |
2569 | 187 class RunningJob : public boost::noncopyable |
188 { | |
189 private: | |
190 JobsRegistry& registry_; | |
191 JobHandler* handler_; // Can only be accessed if the | |
192 // registry mutex is locked! | |
193 IJob* job_; // Will by design be in mutual exclusion, | |
194 // because only one RunningJob can be | |
195 // executed at a time on a JobHandler | |
196 | |
197 std::string id_; | |
198 int priority_; | |
199 JobState targetState_; | |
200 unsigned int targetRetryTimeout_; | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
201 bool canceled_; |
2569 | 202 |
203 public: | |
204 RunningJob(JobsRegistry& registry, | |
205 unsigned int timeout); | |
206 | |
207 ~RunningJob(); | |
208 | |
209 bool IsValid() const; | |
210 | |
211 const std::string& GetId() const; | |
212 | |
213 int GetPriority() const; | |
214 | |
215 IJob& GetJob(); | |
216 | |
217 bool IsPauseScheduled(); | |
218 | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
219 bool IsCancelScheduled(); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
220 |
2569 | 221 void MarkSuccess(); |
222 | |
223 void MarkFailure(); | |
224 | |
225 void MarkPause(); | |
226 | |
2581
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
227 void MarkCanceled(); |
8da2cffc2378
JobsRegistry::Cancel()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2573
diff
changeset
|
228 |
2569 | 229 void MarkRetry(unsigned int timeout); |
230 | |
231 void UpdateStatus(ErrorCode code); | |
232 }; | |
233 }; | |
234 } |