Mercurial > hg > orthanc
annotate UnitTestsSources/MultiThreadingTests.cpp @ 2564:f8681f251caa jobs
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 04 May 2018 17:49:44 +0200 |
parents | 98dfc1948d00 |
children | 0f06b4d5b3d0 |
rev | line source |
---|---|
827
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
1 /** |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
2 * Orthanc - A Lightweight, RESTful DICOM Store |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1009
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
2447
878b59270859
upgrade to year 2018
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2382
diff
changeset
|
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium |
827
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
6 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
7 * This program is free software: you can redistribute it and/or |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
8 * modify it under the terms of the GNU General Public License as |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
9 * published by the Free Software Foundation, either version 3 of the |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
10 * License, or (at your option) any later version. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
11 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
12 * In addition, as a special exception, the copyright holders of this |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
13 * program give permission to link the code of its release with the |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
15 * that use the same license as the "OpenSSL" library), and distribute |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
16 * the linked executables. You must obey the GNU General Public License |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
17 * in all respects for all of the code used other than "OpenSSL". If you |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
18 * modify file(s) with this exception, you may extend this exception to |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
19 * your version of the file(s), but you are not obligated to do so. If |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
20 * you do not wish to do so, delete this exception statement from your |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
21 * version. If you delete this exception statement from all source files |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
22 * in the program, then also delete it here. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
23 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
24 * This program is distributed in the hope that it will be useful, but |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
25 * WITHOUT ANY WARRANTY; without even the implied warranty of |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
27 * General Public License for more details. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
28 * |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
29 * You should have received a copy of the GNU General Public License |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
31 **/ |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
32 |
3d6f9b7d0add
precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
776
diff
changeset
|
33 |
831
84513f2ee1f3
pch for unit tests and server
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
827
diff
changeset
|
34 #include "PrecompiledHeadersUnitTests.h" |
723 | 35 #include "gtest/gtest.h" |
36 | |
781 | 37 #include "../OrthancServer/Scheduler/ServerScheduler.h" |
723 | 38 #include "../Core/OrthancException.h" |
2143
fd5875662670
creation of namespace SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2140
diff
changeset
|
39 #include "../Core/SystemToolbox.h" |
723 | 40 #include "../Core/Toolbox.h" |
760 | 41 #include "../Core/MultiThreading/Locker.h" |
42 #include "../Core/MultiThreading/Mutex.h" | |
43 #include "../Core/MultiThreading/ReaderWriterLock.h" | |
723 | 44 |
45 using namespace Orthanc; | |
46 | |
47 namespace | |
48 { | |
1396
ac4efabeb80c
Migration of the orthanc-client as a separate project
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1304
diff
changeset
|
49 class DynamicInteger : public IDynamicObject |
723 | 50 { |
51 private: | |
52 int value_; | |
53 std::set<int>& target_; | |
54 | |
55 public: | |
56 DynamicInteger(int value, std::set<int>& target) : | |
57 value_(value), target_(target) | |
58 { | |
59 } | |
60 | |
61 int GetValue() const | |
62 { | |
63 return value_; | |
64 } | |
65 }; | |
66 } | |
67 | |
68 | |
69 TEST(MultiThreading, SharedMessageQueueBasic) | |
70 { | |
71 std::set<int> s; | |
72 | |
73 SharedMessageQueue q; | |
74 ASSERT_TRUE(q.WaitEmpty(0)); | |
75 q.Enqueue(new DynamicInteger(10, s)); | |
76 ASSERT_FALSE(q.WaitEmpty(1)); | |
77 q.Enqueue(new DynamicInteger(20, s)); | |
78 q.Enqueue(new DynamicInteger(30, s)); | |
79 q.Enqueue(new DynamicInteger(40, s)); | |
80 | |
81 std::auto_ptr<DynamicInteger> i; | |
82 i.reset(dynamic_cast<DynamicInteger*>(q.Dequeue(1))); ASSERT_EQ(10, i->GetValue()); | |
83 i.reset(dynamic_cast<DynamicInteger*>(q.Dequeue(1))); ASSERT_EQ(20, i->GetValue()); | |
84 i.reset(dynamic_cast<DynamicInteger*>(q.Dequeue(1))); ASSERT_EQ(30, i->GetValue()); | |
85 ASSERT_FALSE(q.WaitEmpty(1)); | |
86 i.reset(dynamic_cast<DynamicInteger*>(q.Dequeue(1))); ASSERT_EQ(40, i->GetValue()); | |
87 ASSERT_TRUE(q.WaitEmpty(0)); | |
88 ASSERT_EQ(NULL, q.Dequeue(1)); | |
89 } | |
90 | |
91 | |
92 TEST(MultiThreading, SharedMessageQueueClean) | |
93 { | |
94 std::set<int> s; | |
95 | |
96 try | |
97 { | |
98 SharedMessageQueue q; | |
99 q.Enqueue(new DynamicInteger(10, s)); | |
100 q.Enqueue(new DynamicInteger(20, s)); | |
1583
9ea3d082b064
got rid of custom exceptions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1582
diff
changeset
|
101 throw OrthancException(ErrorCode_InternalError); |
723 | 102 } |
103 catch (OrthancException&) | |
104 { | |
105 } | |
106 } | |
107 | |
108 | |
760 | 109 TEST(MultiThreading, Mutex) |
110 { | |
111 Mutex mutex; | |
112 Locker locker(mutex); | |
113 } | |
114 | |
115 | |
116 TEST(MultiThreading, ReaderWriterLock) | |
117 { | |
118 ReaderWriterLock lock; | |
119 | |
120 { | |
121 Locker locker1(lock.ForReader()); | |
122 Locker locker2(lock.ForReader()); | |
123 } | |
124 | |
125 { | |
126 Locker locker3(lock.ForWriter()); | |
127 } | |
128 } | |
769
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
129 |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
130 |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
131 |
2382
7284093111b0
big reorganization to cleanly separate framework vs. server
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
132 #include "../Core/DicomNetworking/ReusableDicomUserConnection.h" |
769
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
133 |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
134 TEST(ReusableDicomUserConnection, DISABLED_Basic) |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
135 { |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
136 ReusableDicomUserConnection c; |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
137 c.SetMillisecondsBeforeClose(200); |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
138 printf("START\n"); fflush(stdout); |
775
d3ba35466225
integration mainline -> lua-scripting
Sebastien Jodogne <s.jodogne@gmail.com>
diff
changeset
|
139 |
769
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
140 { |
1427
d710ea64f0fd
Custom setting of the local AET during C-Store SCU (both in Lua and in the REST API)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1396
diff
changeset
|
141 RemoteModalityParameters remote("STORESCP", "localhost", 2000, ModalityManufacturer_Generic); |
d710ea64f0fd
Custom setting of the local AET during C-Store SCU (both in Lua and in the REST API)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1396
diff
changeset
|
142 ReusableDicomUserConnection::Locker lock(c, "ORTHANC", remote); |
2222
21713ce8717b
Fix handling of Move Originator AET and ID in C-MOVE SCP
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
143 lock.GetConnection().StoreFile("/home/jodogne/DICOM/Cardiac/MR.X.1.2.276.0.7230010.3.1.4.2831157719.2256.1336386844.676281"); |
769
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
144 } |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
145 |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
146 printf("**\n"); fflush(stdout); |
2242
4e8e0ad2001c
move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2222
diff
changeset
|
147 SystemToolbox::USleep(1000000); |
769
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
148 printf("**\n"); fflush(stdout); |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
149 |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
150 { |
1427
d710ea64f0fd
Custom setting of the local AET during C-Store SCU (both in Lua and in the REST API)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1396
diff
changeset
|
151 RemoteModalityParameters remote("STORESCP", "localhost", 2000, ModalityManufacturer_Generic); |
d710ea64f0fd
Custom setting of the local AET during C-Store SCU (both in Lua and in the REST API)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1396
diff
changeset
|
152 ReusableDicomUserConnection::Locker lock(c, "ORTHANC", remote); |
2222
21713ce8717b
Fix handling of Move Originator AET and ID in C-MOVE SCP
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2143
diff
changeset
|
153 lock.GetConnection().StoreFile("/home/jodogne/DICOM/Cardiac/MR.X.1.2.276.0.7230010.3.1.4.2831157719.2256.1336386844.676277"); |
769
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
154 } |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
155 |
2140 | 156 SystemToolbox::ServerBarrier(); |
769
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
157 printf("DONE\n"); fflush(stdout); |
3f946e5c3802
ReusableDicomUserConnection
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
760
diff
changeset
|
158 } |
765 | 159 |
160 | |
161 | |
1000
13e230bbd882
rename filter to command
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
162 class Tutu : public IServerCommand |
765 | 163 { |
164 private: | |
165 int factor_; | |
166 | |
167 public: | |
168 Tutu(int f) : factor_(f) | |
169 { | |
170 } | |
171 | |
172 virtual bool Apply(ListOfStrings& outputs, | |
173 const ListOfStrings& inputs) | |
174 { | |
175 for (ListOfStrings::const_iterator | |
1304 | 176 it = inputs.begin(); it != inputs.end(); ++it) |
765 | 177 { |
178 int a = boost::lexical_cast<int>(*it); | |
179 int b = factor_ * a; | |
180 | |
181 printf("%d * %d = %d\n", a, factor_, b); | |
182 | |
183 //if (a == 84) { printf("BREAK\n"); return false; } | |
184 | |
185 outputs.push_back(boost::lexical_cast<std::string>(b)); | |
186 } | |
187 | |
2242
4e8e0ad2001c
move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2222
diff
changeset
|
188 SystemToolbox::USleep(30000); |
765 | 189 |
190 return true; | |
191 } | |
192 }; | |
193 | |
768 | 194 |
770 | 195 static void Tata(ServerScheduler* s, ServerJob* j, bool* done) |
768 | 196 { |
1000
13e230bbd882
rename filter to command
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
197 typedef IServerCommand::ListOfStrings ListOfStrings; |
779 | 198 |
770 | 199 while (!(*done)) |
768 | 200 { |
201 ListOfStrings l; | |
202 s->GetListOfJobs(l); | |
1304 | 203 for (ListOfStrings::iterator it = l.begin(); it != l.end(); ++it) |
204 { | |
205 printf(">> %s: %0.1f\n", it->c_str(), 100.0f * s->GetProgress(*it)); | |
206 } | |
2242
4e8e0ad2001c
move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2222
diff
changeset
|
207 SystemToolbox::USleep(3000); |
768 | 208 } |
209 } | |
210 | |
211 | |
1009
26642cecd36d
clearer job interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1005
diff
changeset
|
212 TEST(MultiThreading, ServerScheduler) |
765 | 213 { |
995
8c67382f44a7
limit number of jobs in the scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
994
diff
changeset
|
214 ServerScheduler scheduler(10); |
765 | 215 |
216 ServerJob job; | |
1000
13e230bbd882
rename filter to command
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
217 ServerCommandInstance& f2 = job.AddCommand(new Tutu(2)); |
13e230bbd882
rename filter to command
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
218 ServerCommandInstance& f3 = job.AddCommand(new Tutu(3)); |
13e230bbd882
rename filter to command
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
219 ServerCommandInstance& f4 = job.AddCommand(new Tutu(4)); |
13e230bbd882
rename filter to command
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
220 ServerCommandInstance& f5 = job.AddCommand(new Tutu(5)); |
765 | 221 f2.AddInput(boost::lexical_cast<std::string>(42)); |
222 //f3.AddInput(boost::lexical_cast<std::string>(42)); | |
223 //f4.AddInput(boost::lexical_cast<std::string>(42)); | |
1009
26642cecd36d
clearer job interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1005
diff
changeset
|
224 f2.ConnectOutput(f3); |
26642cecd36d
clearer job interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1005
diff
changeset
|
225 f3.ConnectOutput(f4); |
26642cecd36d
clearer job interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1005
diff
changeset
|
226 f4.ConnectOutput(f5); |
26642cecd36d
clearer job interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1005
diff
changeset
|
227 |
26642cecd36d
clearer job interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1005
diff
changeset
|
228 f3.SetConnectedToSink(true); |
26642cecd36d
clearer job interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1005
diff
changeset
|
229 f5.SetConnectedToSink(true); |
765 | 230 |
231 job.SetDescription("tutu"); | |
232 | |
770 | 233 bool done = false; |
234 boost::thread t(Tata, &scheduler, &job, &done); | |
768 | 235 |
236 | |
765 | 237 //scheduler.Submit(job); |
238 | |
1000
13e230bbd882
rename filter to command
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
239 IServerCommand::ListOfStrings l; |
765 | 240 scheduler.SubmitAndWait(l, job); |
241 | |
1492 | 242 ASSERT_EQ(2u, l.size()); |
1009
26642cecd36d
clearer job interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1005
diff
changeset
|
243 ASSERT_EQ(42 * 2 * 3, boost::lexical_cast<int>(l.front())); |
26642cecd36d
clearer job interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1005
diff
changeset
|
244 ASSERT_EQ(42 * 2 * 3 * 4 * 5, boost::lexical_cast<int>(l.back())); |
26642cecd36d
clearer job interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1005
diff
changeset
|
245 |
1000
13e230bbd882
rename filter to command
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
995
diff
changeset
|
246 for (IServerCommand::ListOfStrings::iterator i = l.begin(); i != l.end(); i++) |
765 | 247 { |
248 printf("** %s\n", i->c_str()); | |
249 } | |
250 | |
2140 | 251 //SystemToolbox::ServerBarrier(); |
2242
4e8e0ad2001c
move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2222
diff
changeset
|
252 //SystemToolbox::USleep(3000000); |
768 | 253 |
1453
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1427
diff
changeset
|
254 scheduler.Stop(); |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1427
diff
changeset
|
255 |
770 | 256 done = true; |
1453
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1427
diff
changeset
|
257 if (t.joinable()) |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1427
diff
changeset
|
258 { |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1427
diff
changeset
|
259 t.join(); |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1427
diff
changeset
|
260 } |
765 | 261 } |
2556 | 262 |
263 | |
264 | |
265 | |
266 | |
267 #if !defined(ORTHANC_SANDBOXED) | |
268 # error The macro ORTHANC_SANDBOXED must be defined | |
269 #endif | |
270 | |
271 #if ORTHANC_SANDBOXED == 1 | |
272 # error The job engine cannot be used in sandboxed environments | |
273 #endif | |
274 | |
2557 | 275 #include "../Core/Logging.h" |
276 | |
2559 | 277 #include <boost/math/special_functions/round.hpp> |
2556 | 278 #include <boost/date_time/posix_time/posix_time.hpp> |
2557 | 279 #include <queue> |
2556 | 280 |
281 namespace Orthanc | |
282 { | |
283 enum JobState | |
284 { | |
285 JobState_Pending, | |
286 JobState_Running, | |
287 JobState_Success, | |
288 JobState_Failure, | |
289 JobState_Paused, | |
290 JobState_Retry | |
291 }; | |
292 | |
2562 | 293 enum JobStepCode |
2556 | 294 { |
2562 | 295 JobStepCode_Success, |
296 JobStepCode_Failure, | |
297 JobStepCode_Continue, | |
298 JobStepCode_Retry | |
2556 | 299 }; |
300 | |
301 | |
2557 | 302 class JobStepResult |
2556 | 303 { |
304 private: | |
2562 | 305 JobStepCode status_; |
2556 | 306 |
307 public: | |
2562 | 308 explicit JobStepResult(JobStepCode status) : |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
309 status_(status) |
2556 | 310 { |
311 } | |
312 | |
2557 | 313 virtual ~JobStepResult() |
2556 | 314 { |
315 } | |
316 | |
2562 | 317 JobStepCode GetCode() const |
2556 | 318 { |
319 return status_; | |
320 } | |
321 }; | |
322 | |
323 | |
2557 | 324 class RetryResult : public JobStepResult |
2556 | 325 { |
326 private: | |
327 unsigned int timeout_; // Retry after "timeout_" milliseconds | |
328 | |
329 public: | |
330 RetryResult(unsigned int timeout) : | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
331 JobStepResult(JobStepCode_Retry), |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
332 timeout_(timeout) |
2556 | 333 { |
334 } | |
335 | |
336 unsigned int GetTimeout() const | |
337 { | |
338 return timeout_; | |
339 } | |
340 }; | |
341 | |
342 | |
343 class IJob : public boost::noncopyable | |
344 { | |
345 public: | |
346 virtual ~IJob() | |
347 { | |
348 } | |
349 | |
2557 | 350 virtual JobStepResult* ExecuteStep() = 0; |
2556 | 351 |
352 virtual void ReleaseResources() = 0; // For pausing jobs | |
353 | |
354 virtual float GetProgress() = 0; | |
355 | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
356 virtual void GetDescription(Json::Value& value) = 0; |
2556 | 357 }; |
358 | |
359 | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
360 class JobStatus |
2562 | 361 { |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
362 private: |
2562 | 363 ErrorCode errorCode_; |
364 float progress_; | |
365 Json::Value description_; | |
366 | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
367 public: |
2562 | 368 JobStatus() : |
369 errorCode_(ErrorCode_Success), | |
370 progress_(0), | |
371 description_(Json::objectValue) | |
372 { | |
373 } | |
374 | |
375 JobStatus(ErrorCode code, | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
376 IJob& job) : |
2562 | 377 errorCode_(code), |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
378 progress_(job.GetProgress()) |
2562 | 379 { |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
380 if (progress_ < 0 || |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
381 progress_ > 1) |
2562 | 382 { |
383 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
384 } | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
385 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
386 job.GetDescription(description_); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
387 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
388 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
389 ErrorCode GetErrorCode() const |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
390 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
391 return errorCode_; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
392 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
393 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
394 float GetProgress() const |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
395 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
396 return progress_; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
397 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
398 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
399 const Json::Value& GetDescription() const |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
400 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
401 return description_; |
2562 | 402 } |
403 }; | |
404 | |
405 | |
2559 | 406 class JobInfo |
407 { | |
408 private: | |
409 std::string id_; | |
410 int priority_; | |
411 ErrorCode errorCode_; | |
412 JobState state_; | |
413 boost::posix_time::ptime infoTime_; | |
414 boost::posix_time::ptime creationTime_; | |
415 boost::posix_time::time_duration runtime_; | |
416 boost::posix_time::ptime eta_; | |
2562 | 417 JobStatus status_; |
2559 | 418 |
419 public: | |
420 JobInfo(const std::string& id, | |
421 int priority, | |
422 JobState state, | |
2562 | 423 const JobStatus& status, |
2559 | 424 const boost::posix_time::ptime& creationTime, |
425 const boost::posix_time::time_duration& runtime) : | |
426 id_(id), | |
427 priority_(priority), | |
428 state_(state), | |
429 infoTime_(boost::posix_time::microsec_clock::universal_time()), | |
430 creationTime_(creationTime), | |
2562 | 431 runtime_(runtime), |
432 status_(status) | |
2559 | 433 { |
2562 | 434 float ms = static_cast<float>(runtime_.total_milliseconds()); |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
435 float remaining = boost::math::llround(1.0f - status_.GetProgress()) * ms; |
2562 | 436 eta_ = infoTime_ + boost::posix_time::milliseconds(remaining); |
2559 | 437 } |
438 | |
439 const std::string& GetIdentifier() const | |
440 { | |
441 return id_; | |
442 } | |
443 | |
444 int GetPriority() const | |
445 { | |
446 return priority_; | |
447 } | |
448 | |
449 ErrorCode GetErrorCode() const | |
450 { | |
451 return errorCode_; | |
452 } | |
453 | |
454 JobState GetState() const | |
455 { | |
456 return state_; | |
457 } | |
458 | |
459 const boost::posix_time::ptime& GetInfoTime() const | |
460 { | |
461 return infoTime_; | |
462 } | |
463 | |
464 const boost::posix_time::ptime& GetCreationTime() const | |
465 { | |
466 return creationTime_; | |
467 } | |
468 | |
469 const boost::posix_time::time_duration& GetRuntime() const | |
470 { | |
471 return runtime_; | |
472 } | |
473 | |
474 const boost::posix_time::ptime& GetEstimatedTimeOfArrival() const | |
475 { | |
476 return eta_; | |
477 } | |
478 | |
2562 | 479 const JobStatus& GetStatus() const |
2559 | 480 { |
481 return status_; | |
482 } | |
483 | |
2562 | 484 JobStatus& GetStatus() |
2559 | 485 { |
486 return status_; | |
487 } | |
488 }; | |
489 | |
490 | |
2557 | 491 class JobHandler : public boost::noncopyable |
2562 | 492 { |
2557 | 493 private: |
2559 | 494 std::string id_; |
495 JobState state_; | |
496 std::auto_ptr<IJob> job_; | |
497 int priority_; // "+inf()" means highest priority | |
498 boost::posix_time::ptime creationTime_; | |
2562 | 499 boost::posix_time::ptime lastStateChangeTime_; |
500 boost::posix_time::time_duration runtime_; | |
2559 | 501 boost::posix_time::ptime retryTime_; |
502 bool pauseScheduled_; | |
2562 | 503 JobStatus lastStatus_; |
2557 | 504 |
505 void SetStateInternal(JobState state) | |
506 { | |
507 const boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time(); | |
508 | |
509 if (state_ == JobState_Running) | |
510 { | |
2562 | 511 runtime_ += (now - lastStateChangeTime_); |
2557 | 512 } |
513 | |
514 state_ = state; | |
2562 | 515 lastStateChangeTime_ = now; |
2557 | 516 pauseScheduled_ = false; |
517 } | |
518 | |
519 public: | |
520 JobHandler(IJob* job, | |
521 int priority) : | |
522 id_(Toolbox::GenerateUuid()), | |
523 state_(JobState_Pending), | |
524 job_(job), | |
525 priority_(priority), | |
526 creationTime_(boost::posix_time::microsec_clock::universal_time()), | |
2562 | 527 lastStateChangeTime_(creationTime_), |
2559 | 528 runtime_(boost::posix_time::milliseconds(0)), |
2562 | 529 retryTime_(creationTime_), |
530 pauseScheduled_(false) | |
2557 | 531 { |
532 if (job == NULL) | |
533 { | |
534 throw OrthancException(ErrorCode_NullPointer); | |
535 } | |
536 } | |
537 | |
538 const std::string& GetId() const | |
539 { | |
540 return id_; | |
541 } | |
542 | |
2562 | 543 IJob& GetJob() const |
544 { | |
545 assert(job_.get() != NULL); | |
546 return *job_; | |
547 } | |
548 | |
2557 | 549 void SetPriority(int priority) |
550 { | |
551 priority_ = priority; | |
552 } | |
553 | |
554 int GetPriority() const | |
555 { | |
556 return priority_; | |
557 } | |
558 | |
559 JobState GetState() const | |
560 { | |
561 return state_; | |
562 } | |
563 | |
564 void SetState(JobState state) | |
565 { | |
566 if (state == JobState_Retry) | |
567 { | |
568 // Use "SetRetryState()" | |
569 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
570 } | |
571 else | |
572 { | |
573 SetStateInternal(state); | |
574 } | |
575 } | |
576 | |
577 void SetRetryState(unsigned int timeout) | |
578 { | |
579 if (state_ == JobState_Running) | |
580 { | |
581 SetStateInternal(JobState_Retry); | |
582 retryTime_ = (boost::posix_time::microsec_clock::universal_time() + | |
583 boost::posix_time::milliseconds(timeout)); | |
584 } | |
585 else | |
586 { | |
587 // Only valid for running jobs | |
588 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
589 } | |
590 } | |
591 | |
592 void SchedulePause() | |
593 { | |
594 if (state_ == JobState_Running) | |
595 { | |
596 pauseScheduled_ = true; | |
597 } | |
598 else | |
599 { | |
600 // Only valid for running jobs | |
601 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
602 } | |
603 } | |
604 | |
605 bool IsPauseScheduled() | |
606 { | |
607 return pauseScheduled_; | |
608 } | |
609 | |
610 bool IsRetryReady(const boost::posix_time::ptime& now) const | |
611 { | |
612 if (state_ != JobState_Retry) | |
613 { | |
614 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
615 } | |
616 else | |
617 { | |
2558 | 618 return retryTime_ <= now; |
2557 | 619 } |
620 } | |
2559 | 621 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
622 const JobStatus& GetLastStatus() const |
2559 | 623 { |
2562 | 624 return lastStatus_; |
625 } | |
2559 | 626 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
627 void SetLastStatus(const JobStatus& status) |
2562 | 628 { |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
629 lastStatus_ = status; |
2562 | 630 } |
2557 | 631 }; |
632 | |
633 | |
2558 | 634 class JobsRegistry : public boost::noncopyable |
2556 | 635 { |
636 private: | |
2557 | 637 struct PriorityComparator |
2556 | 638 { |
2557 | 639 bool operator() (JobHandler*& a, |
640 JobHandler*& b) const | |
641 { | |
642 return a->GetPriority() < b->GetPriority(); | |
643 } | |
644 }; | |
645 | |
646 typedef std::map<std::string, JobHandler*> JobsIndex; | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
647 typedef std::list<JobHandler*> CompletedJobs; |
2557 | 648 typedef std::set<JobHandler*> RetryJobs; |
649 typedef std::priority_queue<JobHandler*, | |
650 std::vector<JobHandler*>, // Could be a "std::deque" | |
651 PriorityComparator> PendingJobs; | |
652 | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
653 boost::mutex mutex_; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
654 JobsIndex jobsIndex_; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
655 PendingJobs pendingJobs_; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
656 CompletedJobs completedJobs_; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
657 RetryJobs retryJobs_; |
2557 | 658 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
659 boost::condition_variable pendingJobAvailable_; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
660 size_t maxCompletedJobs_; |
2557 | 661 |
662 | |
663 #ifndef NDEBUG | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
664 bool IsPendingJob(const JobHandler& job) const |
2557 | 665 { |
666 PendingJobs copy = pendingJobs_; | |
667 while (!copy.empty()) | |
668 { | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
669 if (copy.top() == &job) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
670 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
671 return true; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
672 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
673 |
2557 | 674 copy.pop(); |
675 } | |
676 | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
677 return false; |
2557 | 678 } |
679 | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
680 bool IsCompletedJob(JobHandler& job) const |
2557 | 681 { |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
682 for (CompletedJobs::const_iterator it = completedJobs_.begin(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
683 it != completedJobs_.end(); ++it) |
2556 | 684 { |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
685 if (*it == &job) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
686 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
687 return true; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
688 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
689 } |
2557 | 690 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
691 return false; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
692 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
693 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
694 bool IsRetryJob(JobHandler& job) const |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
695 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
696 return retryJobs_.find(&job) != retryJobs_.end(); |
2557 | 697 } |
698 #endif | |
2558 | 699 |
700 | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
701 void CheckInvariants() const |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
702 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
703 #ifndef NDEBUG |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
704 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
705 PendingJobs copy = pendingJobs_; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
706 while (!copy.empty()) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
707 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
708 assert(copy.top()->GetState() == JobState_Pending); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
709 copy.pop(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
710 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
711 } |
2557 | 712 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
713 assert(completedJobs_.size() <= maxCompletedJobs_); |
2558 | 714 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
715 for (CompletedJobs::const_iterator it = completedJobs_.begin(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
716 it != completedJobs_.end(); ++it) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
717 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
718 assert((*it)->GetState() == JobState_Success || |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
719 (*it)->GetState() == JobState_Failure); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
720 } |
2557 | 721 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
722 for (RetryJobs::const_iterator it = retryJobs_.begin(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
723 it != retryJobs_.end(); ++it) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
724 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
725 assert((*it)->GetState() == JobState_Retry); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
726 } |
2558 | 727 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
728 for (JobsIndex::const_iterator it = jobsIndex_.begin(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
729 it != jobsIndex_.end(); ++it) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
730 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
731 JobHandler& job = *it->second; |
2557 | 732 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
733 assert(job.GetId() == it->first); |
2557 | 734 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
735 switch (job.GetState()) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
736 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
737 case JobState_Pending: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
738 assert(!IsRetryJob(job) && IsPendingJob(job) && !IsCompletedJob(job)); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
739 break; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
740 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
741 case JobState_Success: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
742 case JobState_Failure: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
743 assert(!IsRetryJob(job) && !IsPendingJob(job) && IsCompletedJob(job)); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
744 break; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
745 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
746 case JobState_Retry: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
747 assert(IsRetryJob(job) && !IsPendingJob(job) && !IsCompletedJob(job)); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
748 break; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
749 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
750 case JobState_Running: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
751 case JobState_Paused: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
752 assert(!IsRetryJob(job) && !IsPendingJob(job) && !IsCompletedJob(job)); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
753 break; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
754 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
755 default: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
756 throw OrthancException(ErrorCode_InternalError); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
757 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
758 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
759 #endif |
2558 | 760 } |
2557 | 761 |
762 | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
763 void ForgetOldCompletedJobs() |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
764 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
765 if (maxCompletedJobs_ != 0) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
766 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
767 while (completedJobs_.size() > maxCompletedJobs_) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
768 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
769 assert(completedJobs_.front() != NULL); |
2557 | 770 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
771 std::string id = completedJobs_.front()->GetId(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
772 assert(jobsIndex_.find(id) != jobsIndex_.end()); |
2557 | 773 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
774 jobsIndex_.erase(id); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
775 delete(completedJobs_.front()); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
776 completedJobs_.pop_front(); |
2558 | 777 } |
2557 | 778 } |
779 } | |
780 | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
781 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
782 void MarkRunningAsCompleted(JobHandler& job, |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
783 bool success) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
784 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
785 LOG(INFO) << "Job has completed with " << (success ? "success" : "failure") |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
786 << ": " << job.GetId(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
787 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
788 CheckInvariants(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
789 assert(job.GetState() == JobState_Running); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
790 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
791 job.SetState(success ? JobState_Success : JobState_Failure); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
792 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
793 completedJobs_.push_back(&job); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
794 ForgetOldCompletedJobs(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
795 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
796 CheckInvariants(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
797 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
798 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
799 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
800 void MarkRunningAsRetry(JobHandler& job, |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
801 unsigned int timeout) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
802 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
803 LOG(INFO) << "Job scheduled for retry in " << timeout << "ms: " << job.GetId(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
804 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
805 CheckInvariants(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
806 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
807 assert(job.GetState() == JobState_Running && |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
808 retryJobs_.find(&job) == retryJobs_.end()); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
809 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
810 retryJobs_.insert(&job); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
811 job.SetRetryState(timeout); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
812 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
813 CheckInvariants(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
814 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
815 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
816 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
817 void MarkRunningAsPaused(JobHandler& job) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
818 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
819 LOG(INFO) << "Job paused: " << job.GetId(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
820 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
821 CheckInvariants(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
822 assert(job.GetState() == JobState_Running); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
823 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
824 job.SetState(JobState_Paused); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
825 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
826 CheckInvariants(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
827 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
828 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
829 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
830 public: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
831 JobsRegistry() : |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
832 maxCompletedJobs_(10) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
833 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
834 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
835 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
836 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
837 ~JobsRegistry() |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
838 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
839 for (JobsIndex::iterator it = jobsIndex_.begin(); it != jobsIndex_.end(); ++it) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
840 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
841 assert(it->second != NULL); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
842 delete it->second; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
843 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
844 } |
2557 | 845 |
846 | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
847 void SetMaxCompletedJobs(size_t i) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
848 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
849 boost::mutex::scoped_lock lock(mutex_); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
850 CheckInvariants(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
851 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
852 maxCompletedJobs_ = i; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
853 ForgetOldCompletedJobs(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
854 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
855 CheckInvariants(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
856 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
857 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
858 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
859 void ListJobs(std::set<std::string>& target) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
860 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
861 boost::mutex::scoped_lock lock(mutex_); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
862 CheckInvariants(); |
2557 | 863 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
864 for (JobsIndex::const_iterator it = jobsIndex_.begin(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
865 it != jobsIndex_.end(); ++it) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
866 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
867 target.insert(it->first); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
868 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
869 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
870 |
2557 | 871 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
872 void Submit(std::string& id, |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
873 IJob* job, // Takes ownership |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
874 int priority) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
875 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
876 std::auto_ptr<JobHandler> handler(new JobHandler(job, priority)); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
877 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
878 boost::mutex::scoped_lock lock(mutex_); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
879 CheckInvariants(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
880 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
881 id = handler->GetId(); |
2557 | 882 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
883 pendingJobs_.push(handler.get()); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
884 pendingJobAvailable_.notify_one(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
885 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
886 jobsIndex_.insert(std::make_pair(id, handler.release())); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
887 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
888 LOG(INFO) << "New job submitted: " << id; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
889 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
890 CheckInvariants(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
891 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
892 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
893 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
894 void Submit(IJob* job, // Takes ownership |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
895 int priority) |
2557 | 896 { |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
897 std::string id; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
898 Submit(id, job, priority); |
2557 | 899 } |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
900 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
901 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
902 void SetPriority(const std::string& id, |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
903 int priority) |
2556 | 904 { |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
905 LOG(INFO) << "Changing priority to " << priority << " for job: " << id; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
906 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
907 boost::mutex::scoped_lock lock(mutex_); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
908 CheckInvariants(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
909 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
910 JobsIndex::iterator found = jobsIndex_.find(id); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
911 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
912 if (found == jobsIndex_.end()) |
2557 | 913 { |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
914 LOG(WARNING) << "Unknown job: " << id; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
915 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
916 else |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
917 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
918 found->second->SetPriority(priority); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
919 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
920 if (found->second->GetState() == JobState_Pending) |
2557 | 921 { |
922 // If the job is pending, we need to reconstruct the | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
923 // priority queue, as the heap condition has changed |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
924 |
2557 | 925 PendingJobs copy; |
926 std::swap(copy, pendingJobs_); | |
927 | |
928 assert(pendingJobs_.empty()); | |
929 while (!copy.empty()) | |
930 { | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
931 pendingJobs_.push(copy.top()); |
2557 | 932 copy.pop(); |
933 } | |
934 } | |
935 } | |
936 | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
937 CheckInvariants(); |
2557 | 938 } |
939 | |
940 | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
941 void Pause(const std::string& id) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
942 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
943 LOG(INFO) << "Pausing job: " << id; |
2558 | 944 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
945 boost::mutex::scoped_lock lock(mutex_); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
946 CheckInvariants(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
947 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
948 JobsIndex::iterator found = jobsIndex_.find(id); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
949 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
950 if (found == jobsIndex_.end()) |
2557 | 951 { |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
952 LOG(WARNING) << "Unknown job: " << id; |
2557 | 953 } |
954 else | |
955 { | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
956 switch (found->second->GetState()) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
957 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
958 case JobState_Pending: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
959 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
960 // If the job is pending, we need to reconstruct the |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
961 // priority queue to remove it |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
962 PendingJobs copy; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
963 std::swap(copy, pendingJobs_); |
2557 | 964 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
965 assert(pendingJobs_.empty()); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
966 while (!copy.empty()) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
967 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
968 if (copy.top()->GetId() != id) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
969 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
970 pendingJobs_.push(copy.top()); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
971 } |
2557 | 972 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
973 copy.pop(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
974 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
975 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
976 found->second->SetState(JobState_Paused); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
977 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
978 break; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
979 } |
2558 | 980 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
981 case JobState_Retry: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
982 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
983 RetryJobs::iterator item = retryJobs_.find(found->second); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
984 assert(item != retryJobs_.end()); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
985 retryJobs_.erase(item); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
986 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
987 found->second->SetState(JobState_Paused); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
988 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
989 break; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
990 } |
2558 | 991 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
992 case JobState_Paused: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
993 case JobState_Success: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
994 case JobState_Failure: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
995 // Nothing to be done |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
996 break; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
997 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
998 case JobState_Running: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
999 found->second->SchedulePause(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1000 break; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1001 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1002 default: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1003 throw OrthancException(ErrorCode_InternalError); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1004 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1005 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1006 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1007 CheckInvariants(); |
2558 | 1008 } |
1009 | |
1010 | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1011 void Resume(const std::string& id) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1012 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1013 LOG(INFO) << "Resuming job: " << id; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1014 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1015 boost::mutex::scoped_lock lock(mutex_); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1016 CheckInvariants(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1017 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1018 JobsIndex::iterator found = jobsIndex_.find(id); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1019 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1020 if (found == jobsIndex_.end()) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1021 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1022 LOG(WARNING) << "Unknown job: " << id; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1023 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1024 else if (found->second->GetState() != JobState_Paused) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1025 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1026 LOG(WARNING) << "Cannot resume a job that is not paused: " << id; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1027 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1028 else |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1029 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1030 found->second->SetState(JobState_Pending); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1031 pendingJobs_.push(found->second); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1032 pendingJobAvailable_.notify_one(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1033 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1034 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1035 CheckInvariants(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1036 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1037 |
2562 | 1038 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1039 void Resubmit(const std::string& id) |
2558 | 1040 { |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1041 LOG(INFO) << "Resubmitting failed job: " << id; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1042 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1043 boost::mutex::scoped_lock lock(mutex_); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1044 CheckInvariants(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1045 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1046 JobsIndex::iterator found = jobsIndex_.find(id); |
2562 | 1047 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1048 if (found == jobsIndex_.end()) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1049 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1050 LOG(WARNING) << "Unknown job: " << id; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1051 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1052 else if (found->second->GetState() != JobState_Failure) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1053 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1054 LOG(WARNING) << "Cannot resubmit a job that has not failed: " << id; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1055 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1056 else |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1057 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1058 bool ok = false; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1059 for (CompletedJobs::iterator it = completedJobs_.begin(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1060 it != completedJobs_.end(); ++it) |
2562 | 1061 { |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1062 if (*it == found->second) |
2562 | 1063 { |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1064 ok = true; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1065 completedJobs_.erase(it); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1066 break; |
2562 | 1067 } |
1068 } | |
1069 | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1070 assert(ok); |
2562 | 1071 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1072 found->second->SetState(JobState_Pending); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1073 pendingJobs_.push(found->second); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1074 pendingJobAvailable_.notify_one(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1075 } |
2562 | 1076 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1077 CheckInvariants(); |
2558 | 1078 } |
2557 | 1079 |
2562 | 1080 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1081 void ScheduleRetries() |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1082 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1083 boost::mutex::scoped_lock lock(mutex_); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1084 CheckInvariants(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1085 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1086 RetryJobs copy; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1087 std::swap(copy, retryJobs_); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1088 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1089 const boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time(); |
2557 | 1090 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1091 assert(retryJobs_.empty()); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1092 for (RetryJobs::iterator it = copy.begin(); it != copy.end(); ++it) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1093 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1094 if ((*it)->IsRetryReady(now)) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1095 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1096 LOG(INFO) << "Retrying job: " << (*it)->GetId(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1097 (*it)->SetState(JobState_Pending); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1098 pendingJobs_.push(*it); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1099 pendingJobAvailable_.notify_one(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1100 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1101 else |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1102 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1103 retryJobs_.insert(*it); |
2557 | 1104 } |
2556 | 1105 } |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1106 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1107 CheckInvariants(); |
2558 | 1108 } |
2556 | 1109 |
2558 | 1110 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1111 bool GetState(JobState& state, |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1112 const std::string& id) |
2558 | 1113 { |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1114 boost::mutex::scoped_lock lock(mutex_); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1115 CheckInvariants(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1116 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1117 JobsIndex::const_iterator it = jobsIndex_.find(id); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1118 if (it == jobsIndex_.end()) |
2556 | 1119 { |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1120 return false; |
2556 | 1121 } |
2558 | 1122 else |
2557 | 1123 { |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1124 state = it->second->GetState(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1125 return true; |
2562 | 1126 } |
2558 | 1127 } |
2557 | 1128 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1129 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1130 class RunningJob : public boost::noncopyable |
2558 | 1131 { |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1132 private: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1133 JobsRegistry& registry_; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1134 JobHandler* handler_; // Can only be accessed if the registry |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1135 // mutex is locked! |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1136 IJob* job_; // Will by design be in mutual exclusion, |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1137 // because only one RunningJob can be |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1138 // executed at a time on a JobHandler |
2558 | 1139 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1140 std::string id_; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1141 int priority_; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1142 JobState targetState_; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1143 unsigned int targetRetryTimeout_; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1144 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1145 public: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1146 RunningJob(JobsRegistry& registry, |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1147 unsigned int timeout) : |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1148 registry_(registry), |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1149 handler_(NULL), |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1150 targetState_(JobState_Failure), |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1151 targetRetryTimeout_(0) |
2562 | 1152 { |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1153 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1154 boost::mutex::scoped_lock lock(registry_.mutex_); |
2558 | 1155 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1156 while (registry_.pendingJobs_.empty()) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1157 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1158 if (timeout == 0) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1159 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1160 registry_.pendingJobAvailable_.wait(lock); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1161 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1162 else |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1163 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1164 bool success = registry_.pendingJobAvailable_.timed_wait |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1165 (lock, boost::posix_time::milliseconds(timeout)); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1166 if (!success) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1167 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1168 // No pending job |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1169 return; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1170 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1171 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1172 } |
2559 | 1173 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1174 handler_ = registry_.pendingJobs_.top(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1175 registry_.pendingJobs_.pop(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1176 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1177 assert(handler_->GetState() == JobState_Pending); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1178 handler_->SetState(JobState_Running); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1179 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1180 job_ = &handler_->GetJob(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1181 id_ = handler_->GetId(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1182 priority_ = handler_->GetPriority(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1183 } |
2559 | 1184 } |
1185 | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1186 ~RunningJob() |
2559 | 1187 { |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1188 if (IsValid()) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1189 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1190 boost::mutex::scoped_lock lock(registry_.mutex_); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1191 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1192 switch (targetState_) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1193 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1194 case JobState_Failure: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1195 registry_.MarkRunningAsCompleted(*handler_, false); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1196 break; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1197 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1198 case JobState_Success: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1199 registry_.MarkRunningAsCompleted(*handler_, true); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1200 break; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1201 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1202 case JobState_Paused: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1203 registry_.MarkRunningAsPaused(*handler_); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1204 break; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1205 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1206 case JobState_Retry: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1207 registry_.MarkRunningAsRetry(*handler_, targetRetryTimeout_); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1208 break; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1209 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1210 default: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1211 assert(0); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1212 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1213 } |
2559 | 1214 } |
1215 | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1216 bool IsValid() const |
2559 | 1217 { |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1218 return (handler_ != NULL && |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1219 job_ != NULL); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1220 } |
2559 | 1221 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1222 const std::string& GetId() const |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1223 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1224 if (!IsValid()) |
2559 | 1225 { |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1226 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1227 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1228 else |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1229 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1230 return id_; |
2559 | 1231 } |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1232 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1233 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1234 int GetPriority() const |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1235 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1236 if (!IsValid()) |
2559 | 1237 { |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1238 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
2559 | 1239 } |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1240 else |
2559 | 1241 { |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1242 return priority_; |
2559 | 1243 } |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1244 } |
2559 | 1245 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1246 bool IsPauseScheduled() |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1247 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1248 if (!IsValid()) |
2559 | 1249 { |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1250 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
2559 | 1251 } |
1252 else | |
1253 { | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1254 boost::mutex::scoped_lock lock(registry_.mutex_); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1255 registry_.CheckInvariants(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1256 assert(handler_->GetState() == JobState_Running); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1257 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1258 return handler_->IsPauseScheduled(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1259 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1260 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1261 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1262 void MarkSuccess() |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1263 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1264 if (!IsValid()) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1265 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1266 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1267 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1268 else |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1269 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1270 targetState_ = JobState_Success; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1271 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1272 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1273 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1274 void MarkFailure() |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1275 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1276 if (!IsValid()) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1277 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1278 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1279 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1280 else |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1281 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1282 targetState_ = JobState_Failure; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1283 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1284 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1285 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1286 void MarkPause() |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1287 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1288 if (!IsValid()) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1289 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1290 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1291 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1292 else |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1293 { |
2564 | 1294 job_->ReleaseResources(); |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1295 targetState_ = JobState_Paused; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1296 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1297 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1298 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1299 void MarkRetry(unsigned int timeout) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1300 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1301 if (!IsValid()) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1302 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1303 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1304 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1305 else |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1306 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1307 targetState_ = JobState_Retry; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1308 targetRetryTimeout_ = timeout; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1309 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1310 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1311 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1312 void UpdateStatus(const JobStatus& status) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1313 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1314 if (!IsValid()) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1315 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1316 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1317 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1318 else |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1319 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1320 boost::mutex::scoped_lock lock(registry_.mutex_); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1321 registry_.CheckInvariants(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1322 assert(handler_->GetState() == JobState_Running); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1323 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1324 handler_->SetLastStatus(status); |
2559 | 1325 } |
1326 } | |
1327 | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1328 bool ExecuteStep() |
2559 | 1329 { |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1330 if (!IsValid()) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1331 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1332 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1333 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1334 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1335 if (IsPauseScheduled()) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1336 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1337 targetState_ = JobState_Paused; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1338 return false; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1339 } |
2559 | 1340 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1341 std::auto_ptr<JobStepResult> result; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1342 ErrorCode code; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1343 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1344 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1345 bool ok = false; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1346 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1347 try |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1348 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1349 result.reset(job_->ExecuteStep()); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1350 ok = true; |
2559 | 1351 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1352 if (result->GetCode() == JobStepCode_Failure) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1353 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1354 code = ErrorCode_InternalError; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1355 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1356 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1357 catch (OrthancException& e) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1358 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1359 code = e.GetErrorCode(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1360 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1361 catch (boost::bad_lexical_cast&) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1362 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1363 code = ErrorCode_BadFileFormat; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1364 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1365 catch (...) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1366 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1367 code = ErrorCode_InternalError; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1368 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1369 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1370 if (ok) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1371 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1372 code = ErrorCode_Success; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1373 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1374 else |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1375 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1376 result.reset(new JobStepResult(JobStepCode_Failure)); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1377 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1378 } |
2559 | 1379 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1380 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1381 JobStatus status(code, *job_); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1382 UpdateStatus(status); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1383 } |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1384 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1385 switch (result->GetCode()) |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1386 { |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1387 case JobStepCode_Success: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1388 targetState_ = JobState_Success; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1389 return false; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1390 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1391 case JobStepCode_Failure: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1392 targetState_ = JobState_Failure; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1393 return false; |
2559 | 1394 |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1395 case JobStepCode_Retry: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1396 targetState_ = JobState_Retry; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1397 targetRetryTimeout_ = dynamic_cast<RetryResult&>(*result).GetTimeout(); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1398 return false; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1399 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1400 case JobStepCode_Continue: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1401 return true; |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1402 |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1403 default: |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1404 throw OrthancException(ErrorCode_InternalError); |
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1405 } |
2559 | 1406 } |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1407 }; |
2556 | 1408 }; |
1409 } | |
2557 | 1410 |
1411 | |
1412 | |
1413 class DummyJob : public Orthanc::IJob | |
1414 { | |
1415 private: | |
1416 JobStepResult result_; | |
1417 | |
1418 public: | |
1419 DummyJob() : | |
2562 | 1420 result_(Orthanc::JobStepCode_Success) |
2557 | 1421 { |
1422 } | |
1423 | |
1424 explicit DummyJob(JobStepResult result) : | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1425 result_(result) |
2557 | 1426 { |
1427 } | |
1428 | |
1429 virtual JobStepResult* ExecuteStep() | |
1430 { | |
1431 return new JobStepResult(result_); | |
1432 } | |
1433 | |
1434 virtual void ReleaseResources() | |
1435 { | |
1436 } | |
1437 | |
1438 virtual float GetProgress() | |
1439 { | |
1440 return 0; | |
1441 } | |
1442 | |
2563
98dfc1948d00
RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2562
diff
changeset
|
1443 virtual void GetDescription(Json::Value& value) |
2557 | 1444 { |
1445 } | |
1446 }; | |
1447 | |
1448 | |
2558 | 1449 static bool CheckState(Orthanc::JobsRegistry& registry, |
2557 | 1450 const std::string& id, |
1451 Orthanc::JobState state) | |
1452 { | |
1453 Orthanc::JobState s; | |
2558 | 1454 if (registry.GetState(s, id)) |
2557 | 1455 { |
1456 return state == s; | |
1457 } | |
1458 else | |
1459 { | |
1460 return false; | |
1461 } | |
1462 } | |
1463 | |
1464 | |
2558 | 1465 TEST(JobsRegistry, Priority) |
2557 | 1466 { |
2558 | 1467 JobsRegistry registry; |
2557 | 1468 |
1469 std::string i1, i2, i3, i4; | |
2558 | 1470 registry.Submit(i1, new DummyJob(), 10); |
1471 registry.Submit(i2, new DummyJob(), 30); | |
1472 registry.Submit(i3, new DummyJob(), 20); | |
1473 registry.Submit(i4, new DummyJob(), 5); | |
2557 | 1474 |
2558 | 1475 registry.SetMaxCompletedJobs(2); |
2557 | 1476 |
1477 std::set<std::string> id; | |
2558 | 1478 registry.ListJobs(id); |
2557 | 1479 |
1480 ASSERT_EQ(4u, id.size()); | |
1481 ASSERT_TRUE(id.find(i1) != id.end()); | |
1482 ASSERT_TRUE(id.find(i2) != id.end()); | |
1483 ASSERT_TRUE(id.find(i3) != id.end()); | |
1484 ASSERT_TRUE(id.find(i4) != id.end()); | |
1485 | |
2558 | 1486 ASSERT_TRUE(CheckState(registry, i2, Orthanc::JobState_Pending)); |
2557 | 1487 |
1488 { | |
2558 | 1489 JobsRegistry::RunningJob job(registry, 0); |
2557 | 1490 ASSERT_TRUE(job.IsValid()); |
1491 ASSERT_EQ(30, job.GetPriority()); | |
1492 ASSERT_EQ(i2, job.GetId()); | |
1493 | |
2558 | 1494 ASSERT_TRUE(CheckState(registry, i2, Orthanc::JobState_Running)); |
2557 | 1495 } |
1496 | |
2558 | 1497 ASSERT_TRUE(CheckState(registry, i2, Orthanc::JobState_Failure)); |
1498 ASSERT_TRUE(CheckState(registry, i3, Orthanc::JobState_Pending)); | |
2557 | 1499 |
1500 { | |
2558 | 1501 JobsRegistry::RunningJob job(registry, 0); |
2557 | 1502 ASSERT_TRUE(job.IsValid()); |
1503 ASSERT_EQ(20, job.GetPriority()); | |
1504 ASSERT_EQ(i3, job.GetId()); | |
1505 | |
1506 job.MarkSuccess(); | |
1507 | |
2558 | 1508 ASSERT_TRUE(CheckState(registry, i3, Orthanc::JobState_Running)); |
2557 | 1509 } |
1510 | |
2558 | 1511 ASSERT_TRUE(CheckState(registry, i3, Orthanc::JobState_Success)); |
2557 | 1512 |
1513 { | |
2558 | 1514 JobsRegistry::RunningJob job(registry, 0); |
2557 | 1515 ASSERT_TRUE(job.IsValid()); |
1516 ASSERT_EQ(10, job.GetPriority()); | |
1517 ASSERT_EQ(i1, job.GetId()); | |
1518 } | |
1519 | |
1520 { | |
2558 | 1521 JobsRegistry::RunningJob job(registry, 0); |
2557 | 1522 ASSERT_TRUE(job.IsValid()); |
1523 ASSERT_EQ(5, job.GetPriority()); | |
1524 ASSERT_EQ(i4, job.GetId()); | |
1525 } | |
1526 | |
1527 { | |
2558 | 1528 JobsRegistry::RunningJob job(registry, 1); |
2557 | 1529 ASSERT_FALSE(job.IsValid()); |
1530 } | |
1531 | |
1532 Orthanc::JobState s; | |
2558 | 1533 ASSERT_TRUE(registry.GetState(s, i1)); |
1534 ASSERT_FALSE(registry.GetState(s, i2)); // Removed because oldest | |
1535 ASSERT_FALSE(registry.GetState(s, i3)); // Removed because second oldest | |
1536 ASSERT_TRUE(registry.GetState(s, i4)); | |
2557 | 1537 |
2558 | 1538 registry.SetMaxCompletedJobs(1); // (*) |
1539 ASSERT_FALSE(registry.GetState(s, i1)); // Just discarded by (*) | |
1540 ASSERT_TRUE(registry.GetState(s, i4)); | |
2557 | 1541 } |
1542 | |
1543 | |
2558 | 1544 TEST(JobsRegistry, Simultaneous) |
2557 | 1545 { |
2558 | 1546 JobsRegistry registry; |
1547 | |
1548 std::string i1, i2; | |
1549 registry.Submit(i1, new DummyJob(), 20); | |
1550 registry.Submit(i2, new DummyJob(), 10); | |
1551 | |
1552 ASSERT_TRUE(CheckState(registry, i1, Orthanc::JobState_Pending)); | |
1553 ASSERT_TRUE(CheckState(registry, i2, Orthanc::JobState_Pending)); | |
1554 | |
1555 { | |
1556 JobsRegistry::RunningJob job1(registry, 0); | |
1557 JobsRegistry::RunningJob job2(registry, 0); | |
1558 | |
1559 ASSERT_TRUE(job1.IsValid()); | |
1560 ASSERT_TRUE(job2.IsValid()); | |
1561 | |
1562 job1.MarkFailure(); | |
1563 job2.MarkSuccess(); | |
1564 | |
1565 ASSERT_TRUE(CheckState(registry, i1, Orthanc::JobState_Running)); | |
1566 ASSERT_TRUE(CheckState(registry, i2, Orthanc::JobState_Running)); | |
1567 } | |
1568 | |
1569 ASSERT_TRUE(CheckState(registry, i1, Orthanc::JobState_Failure)); | |
1570 ASSERT_TRUE(CheckState(registry, i2, Orthanc::JobState_Success)); | |
1571 } | |
1572 | |
1573 | |
1574 TEST(JobsRegistry, Resubmit) | |
1575 { | |
1576 JobsRegistry registry; | |
2557 | 1577 |
1578 std::string id; | |
2558 | 1579 registry.Submit(id, new DummyJob(), 10); |
2557 | 1580 |
2558 | 1581 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); |
2557 | 1582 |
2558 | 1583 registry.Resubmit(id); |
1584 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); | |
2557 | 1585 |
1586 { | |
2558 | 1587 JobsRegistry::RunningJob job(registry, 0); |
2557 | 1588 ASSERT_TRUE(job.IsValid()); |
1589 job.MarkFailure(); | |
1590 | |
2558 | 1591 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); |
2557 | 1592 |
2558 | 1593 registry.Resubmit(id); |
1594 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); | |
2557 | 1595 } |
1596 | |
2558 | 1597 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Failure)); |
2557 | 1598 |
2558 | 1599 registry.Resubmit(id); |
1600 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); | |
2557 | 1601 |
1602 { | |
2558 | 1603 JobsRegistry::RunningJob job(registry, 0); |
2557 | 1604 ASSERT_TRUE(job.IsValid()); |
1605 ASSERT_EQ(id, job.GetId()); | |
1606 | |
1607 job.MarkSuccess(); | |
2558 | 1608 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); |
1609 } | |
1610 | |
1611 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Success)); | |
1612 | |
1613 registry.Resubmit(id); | |
1614 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Success)); | |
1615 } | |
1616 | |
1617 | |
1618 TEST(JobsRegistry, Retry) | |
1619 { | |
1620 JobsRegistry registry; | |
1621 | |
1622 std::string id; | |
1623 registry.Submit(id, new DummyJob(), 10); | |
1624 | |
1625 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); | |
1626 | |
1627 { | |
1628 JobsRegistry::RunningJob job(registry, 0); | |
1629 ASSERT_TRUE(job.IsValid()); | |
1630 job.MarkRetry(0); | |
1631 | |
1632 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); | |
1633 } | |
1634 | |
1635 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Retry)); | |
1636 | |
1637 registry.Resubmit(id); | |
1638 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Retry)); | |
1639 | |
1640 registry.ScheduleRetries(); | |
1641 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); | |
1642 | |
1643 { | |
1644 JobsRegistry::RunningJob job(registry, 0); | |
1645 ASSERT_TRUE(job.IsValid()); | |
1646 job.MarkSuccess(); | |
1647 | |
1648 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); | |
2557 | 1649 } |
1650 | |
2558 | 1651 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Success)); |
1652 } | |
1653 | |
1654 | |
1655 TEST(JobsRegistry, PausePending) | |
1656 { | |
1657 JobsRegistry registry; | |
1658 | |
1659 std::string id; | |
1660 registry.Submit(id, new DummyJob(), 10); | |
1661 | |
1662 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); | |
1663 | |
1664 registry.Pause(id); | |
1665 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Paused)); | |
1666 | |
1667 registry.Pause(id); | |
1668 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Paused)); | |
1669 | |
1670 registry.Resubmit(id); | |
1671 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Paused)); | |
1672 | |
1673 registry.Resume(id); | |
1674 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); | |
2557 | 1675 } |
2558 | 1676 |
1677 | |
1678 TEST(JobsRegistry, PauseRunning) | |
1679 { | |
1680 JobsRegistry registry; | |
1681 | |
1682 std::string id; | |
1683 registry.Submit(id, new DummyJob(), 10); | |
1684 | |
1685 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); | |
1686 | |
1687 { | |
1688 JobsRegistry::RunningJob job(registry, 0); | |
1689 ASSERT_TRUE(job.IsValid()); | |
1690 | |
1691 registry.Resubmit(id); | |
2562 | 1692 job.MarkPause(); |
2558 | 1693 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); |
1694 } | |
1695 | |
1696 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Paused)); | |
1697 | |
1698 registry.Resubmit(id); | |
1699 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Paused)); | |
1700 | |
1701 registry.Resume(id); | |
1702 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); | |
1703 | |
1704 { | |
1705 JobsRegistry::RunningJob job(registry, 0); | |
1706 ASSERT_TRUE(job.IsValid()); | |
1707 | |
1708 job.MarkSuccess(); | |
1709 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); | |
1710 } | |
1711 | |
1712 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Success)); | |
1713 } | |
1714 | |
1715 | |
1716 TEST(JobsRegistry, PauseRetry) | |
1717 { | |
1718 JobsRegistry registry; | |
1719 | |
1720 std::string id; | |
1721 registry.Submit(id, new DummyJob(), 10); | |
1722 | |
1723 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); | |
1724 | |
1725 { | |
1726 JobsRegistry::RunningJob job(registry, 0); | |
1727 ASSERT_TRUE(job.IsValid()); | |
1728 | |
1729 job.MarkRetry(0); | |
1730 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); | |
1731 } | |
1732 | |
1733 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Retry)); | |
1734 | |
1735 registry.Pause(id); | |
1736 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Paused)); | |
1737 | |
1738 registry.Resume(id); | |
1739 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); | |
1740 | |
1741 { | |
1742 JobsRegistry::RunningJob job(registry, 0); | |
1743 ASSERT_TRUE(job.IsValid()); | |
1744 | |
1745 job.MarkSuccess(); | |
1746 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); | |
1747 } | |
1748 | |
1749 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Success)); | |
1750 } |