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