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