Mercurial > hg > orthanc
annotate UnitTestsSources/MultiThreadingTests.cpp @ 2559:9b7680dee75d jobs
cont
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 03 May 2018 18:45:47 +0200 |
parents | 57f81b988713 |
children | 1e66fe3ddf9f |
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 | |
293 enum JobStepStatus | |
294 { | |
295 JobStepStatus_Success, | |
2557 | 296 JobStepStatus_Failure, |
2556 | 297 JobStepStatus_Continue, |
298 JobStepStatus_Retry | |
299 }; | |
300 | |
301 | |
2557 | 302 class JobStepResult |
2556 | 303 { |
304 private: | |
305 JobStepStatus status_; | |
306 | |
307 public: | |
2557 | 308 explicit JobStepResult(JobStepStatus status) : |
309 status_(status) | |
2556 | 310 { |
311 } | |
312 | |
2557 | 313 virtual ~JobStepResult() |
2556 | 314 { |
315 } | |
316 | |
317 JobStepStatus GetStatus() const | |
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) : | |
2557 | 331 JobStepResult(JobStepStatus_Retry), |
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 | |
356 virtual void FormatStatus(Json::Value& value) = 0; | |
357 }; | |
358 | |
359 | |
2559 | 360 class JobInfo |
361 { | |
362 private: | |
363 std::string id_; | |
364 int priority_; | |
365 ErrorCode errorCode_; | |
366 JobState state_; | |
367 float progress_; | |
368 boost::posix_time::ptime infoTime_; | |
369 boost::posix_time::ptime creationTime_; | |
370 boost::posix_time::time_duration runtime_; | |
371 boost::posix_time::ptime eta_; | |
372 Json::Value status_; | |
373 | |
374 public: | |
375 JobInfo(const std::string& id, | |
376 int priority, | |
377 ErrorCode errorCode, | |
378 JobState state, | |
379 float progress, | |
380 const boost::posix_time::ptime& creationTime, | |
381 const boost::posix_time::time_duration& runtime) : | |
382 id_(id), | |
383 priority_(priority), | |
384 errorCode_(errorCode), | |
385 state_(state), | |
386 progress_(progress), | |
387 infoTime_(boost::posix_time::microsec_clock::universal_time()), | |
388 creationTime_(creationTime), | |
389 runtime_(runtime) | |
390 { | |
391 if (progress < 0 || | |
392 progress > 1) | |
393 { | |
394 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
395 } | |
396 | |
397 float r = static_cast<float>(runtime_.total_milliseconds()); | |
398 | |
399 eta_ = infoTime_ + boost::posix_time::milliseconds(boost::math::llround(1.0f - progress) * r); | |
400 } | |
401 | |
402 const std::string& GetIdentifier() const | |
403 { | |
404 return id_; | |
405 } | |
406 | |
407 int GetPriority() const | |
408 { | |
409 return priority_; | |
410 } | |
411 | |
412 ErrorCode GetErrorCode() const | |
413 { | |
414 return errorCode_; | |
415 } | |
416 | |
417 JobState GetState() const | |
418 { | |
419 return state_; | |
420 } | |
421 | |
422 float GetProgress() const | |
423 { | |
424 return progress_; | |
425 } | |
426 | |
427 const boost::posix_time::ptime& GetInfoTime() const | |
428 { | |
429 return infoTime_; | |
430 } | |
431 | |
432 const boost::posix_time::ptime& GetCreationTime() const | |
433 { | |
434 return creationTime_; | |
435 } | |
436 | |
437 const boost::posix_time::time_duration& GetRuntime() const | |
438 { | |
439 return runtime_; | |
440 } | |
441 | |
442 const boost::posix_time::ptime& GetEstimatedTimeOfArrival() const | |
443 { | |
444 return eta_; | |
445 } | |
446 | |
447 const Json::Value& GetStatus() const | |
448 { | |
449 return status_; | |
450 } | |
451 | |
452 Json::Value& GetStatus() | |
453 { | |
454 return status_; | |
455 } | |
456 }; | |
457 | |
458 | |
2557 | 459 class JobHandler : public boost::noncopyable |
460 { | |
461 private: | |
2559 | 462 std::string id_; |
463 JobState state_; | |
464 boost::mutex jobMutex_; | |
465 std::auto_ptr<IJob> job_; | |
466 int priority_; // "+inf()" means highest priority | |
467 boost::posix_time::ptime creationTime_; | |
468 boost::posix_time::ptime lastStateChangeTime; | |
469 boost::posix_time::ptime retryTime_; | |
470 boost::posix_time::time_duration runtime_; | |
471 bool pauseScheduled_; | |
472 ErrorCode lastErrorCode_; | |
473 float lastProgress_; | |
474 Json::Value lastStatus_; | |
2557 | 475 |
476 void SetStateInternal(JobState state) | |
477 { | |
478 const boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time(); | |
479 | |
480 if (state_ == JobState_Running) | |
481 { | |
2559 | 482 runtime_ += (now - lastStateChangeTime); |
2557 | 483 } |
484 | |
485 state_ = state; | |
2559 | 486 lastStateChangeTime = now; |
2557 | 487 pauseScheduled_ = false; |
488 } | |
489 | |
490 public: | |
491 JobHandler(IJob* job, | |
492 int priority) : | |
493 id_(Toolbox::GenerateUuid()), | |
494 state_(JobState_Pending), | |
495 job_(job), | |
496 priority_(priority), | |
497 creationTime_(boost::posix_time::microsec_clock::universal_time()), | |
2559 | 498 lastStateChangeTime(creationTime_), |
499 runtime_(boost::posix_time::milliseconds(0)), | |
500 pauseScheduled_(false), | |
501 lastErrorCode_(ErrorCode_Success), | |
502 lastProgress_(0) | |
2557 | 503 { |
504 if (job == NULL) | |
505 { | |
506 throw OrthancException(ErrorCode_NullPointer); | |
507 } | |
508 } | |
509 | |
510 const std::string& GetId() const | |
511 { | |
512 return id_; | |
513 } | |
514 | |
515 void SetPriority(int priority) | |
516 { | |
517 priority_ = priority; | |
518 } | |
519 | |
520 int GetPriority() const | |
521 { | |
522 return priority_; | |
523 } | |
524 | |
525 JobState GetState() const | |
526 { | |
527 return state_; | |
528 } | |
529 | |
530 void SetState(JobState state) | |
531 { | |
532 if (state == JobState_Retry) | |
533 { | |
534 // Use "SetRetryState()" | |
535 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
536 } | |
537 else | |
538 { | |
539 SetStateInternal(state); | |
540 } | |
541 } | |
542 | |
543 void SetRetryState(unsigned int timeout) | |
544 { | |
545 if (state_ == JobState_Running) | |
546 { | |
547 SetStateInternal(JobState_Retry); | |
548 retryTime_ = (boost::posix_time::microsec_clock::universal_time() + | |
549 boost::posix_time::milliseconds(timeout)); | |
550 } | |
551 else | |
552 { | |
553 // Only valid for running jobs | |
554 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
555 } | |
556 } | |
557 | |
558 void SchedulePause() | |
559 { | |
560 if (state_ == JobState_Running) | |
561 { | |
562 pauseScheduled_ = true; | |
563 } | |
564 else | |
565 { | |
566 // Only valid for running jobs | |
567 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
568 } | |
569 } | |
570 | |
571 bool IsPauseScheduled() | |
572 { | |
573 return pauseScheduled_; | |
574 } | |
575 | |
576 bool IsRetryReady(const boost::posix_time::ptime& now) const | |
577 { | |
578 if (state_ != JobState_Retry) | |
579 { | |
580 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
581 } | |
582 else | |
583 { | |
2558 | 584 return retryTime_ <= now; |
2557 | 585 } |
586 } | |
2559 | 587 |
588 class JobLock | |
589 { | |
590 private: | |
591 boost::mutex::scoped_lock lock_; | |
592 JobHandler& handler_; | |
593 | |
594 public: | |
595 JobLock(JobHandler& handler) : | |
596 lock_(handler.jobMutex_), | |
597 handler_(handler) | |
598 { | |
599 } | |
600 | |
601 IJob& GetJob() | |
602 { | |
603 return *handler_.job_; | |
604 } | |
605 | |
606 void UpdateStatus() | |
607 { | |
608 handler_.lastProgress_ = handler_.job_->GetProgress(); | |
609 handler_.job_->FormatStatus(handler_.lastStatus_); | |
610 } | |
611 | |
612 void SetLastErrorCode(ErrorCode code) | |
613 { | |
614 handler_.lastErrorCode_ = code; | |
615 } | |
616 }; | |
2557 | 617 }; |
618 | |
619 | |
2558 | 620 class JobsRegistry : public boost::noncopyable |
2556 | 621 { |
622 private: | |
2557 | 623 struct PriorityComparator |
2556 | 624 { |
2557 | 625 bool operator() (JobHandler*& a, |
626 JobHandler*& b) const | |
627 { | |
628 return a->GetPriority() < b->GetPriority(); | |
629 } | |
630 }; | |
631 | |
632 typedef std::map<std::string, JobHandler*> JobsIndex; | |
633 typedef std::list<const JobHandler*> CompletedJobs; | |
634 typedef std::set<JobHandler*> RetryJobs; | |
635 typedef std::priority_queue<JobHandler*, | |
636 std::vector<JobHandler*>, // Could be a "std::deque" | |
637 PriorityComparator> PendingJobs; | |
638 | |
2558 | 639 boost::mutex mutex_; |
640 JobsIndex jobsIndex_; | |
641 PendingJobs pendingJobs_; | |
642 CompletedJobs completedJobs_; | |
643 RetryJobs retryJobs_; | |
2557 | 644 |
2558 | 645 boost::condition_variable pendingJobAvailable_; |
646 size_t maxCompletedJobs_; | |
2557 | 647 |
648 | |
649 #ifndef NDEBUG | |
2558 | 650 bool IsPendingJob(const JobHandler& job) const |
651 { | |
652 PendingJobs copy = pendingJobs_; | |
653 while (!copy.empty()) | |
654 { | |
655 if (copy.top() == &job) | |
656 { | |
657 return true; | |
658 } | |
659 | |
660 copy.pop(); | |
661 } | |
662 | |
663 return false; | |
664 } | |
665 | |
666 bool IsCompletedJob(const JobHandler& job) const | |
667 { | |
668 for (CompletedJobs::const_iterator it = completedJobs_.begin(); | |
669 it != completedJobs_.end(); ++it) | |
670 { | |
671 if (*it == &job) | |
672 { | |
673 return true; | |
674 } | |
675 } | |
676 | |
677 return false; | |
678 } | |
679 | |
680 bool IsRetryJob(JobHandler& job) const | |
681 { | |
682 return retryJobs_.find(&job) != retryJobs_.end(); | |
683 } | |
684 #endif | |
685 | |
686 | |
687 void CheckInvariants() | |
688 { | |
689 #ifndef NDEBUG | |
2557 | 690 { |
691 PendingJobs copy = pendingJobs_; | |
692 while (!copy.empty()) | |
693 { | |
2558 | 694 assert(copy.top()->GetState() == JobState_Pending); |
2557 | 695 copy.pop(); |
696 } | |
2558 | 697 } |
2557 | 698 |
2558 | 699 assert(completedJobs_.size() <= maxCompletedJobs_); |
700 | |
701 for (CompletedJobs::const_iterator it = completedJobs_.begin(); | |
702 it != completedJobs_.end(); ++it) | |
703 { | |
704 assert((*it)->GetState() == JobState_Success || | |
705 (*it)->GetState() == JobState_Failure); | |
706 } | |
707 | |
708 for (RetryJobs::const_iterator it = retryJobs_.begin(); | |
709 it != retryJobs_.end(); ++it) | |
710 { | |
711 assert((*it)->GetState() == JobState_Retry); | |
2557 | 712 } |
713 | |
2558 | 714 for (JobsIndex::iterator it = jobsIndex_.begin(); |
715 it != jobsIndex_.end(); ++it) | |
2557 | 716 { |
2558 | 717 JobHandler& job = *it->second; |
718 | |
719 assert(job.GetId() == it->first); | |
720 | |
721 switch (job.GetState()) | |
2556 | 722 { |
2558 | 723 case JobState_Pending: |
724 assert(!IsRetryJob(job) && IsPendingJob(job) && !IsCompletedJob(job)); | |
725 break; | |
726 | |
727 case JobState_Success: | |
728 case JobState_Failure: | |
729 assert(!IsRetryJob(job) && !IsPendingJob(job) && IsCompletedJob(job)); | |
730 break; | |
731 | |
732 case JobState_Retry: | |
733 assert(IsRetryJob(job) && !IsPendingJob(job) && !IsCompletedJob(job)); | |
734 break; | |
735 | |
736 case JobState_Running: | |
737 case JobState_Paused: | |
738 assert(!IsRetryJob(job) && !IsPendingJob(job) && !IsCompletedJob(job)); | |
739 break; | |
2557 | 740 |
2558 | 741 default: |
742 throw OrthancException(ErrorCode_InternalError); | |
743 } | |
2557 | 744 } |
745 #endif | |
2558 | 746 } |
2557 | 747 |
748 | |
2558 | 749 void ForgetOldCompletedJobs() |
750 { | |
751 if (maxCompletedJobs_ != 0) | |
2557 | 752 { |
2558 | 753 while (completedJobs_.size() > maxCompletedJobs_) |
2557 | 754 { |
2558 | 755 assert(completedJobs_.front() != NULL); |
756 | |
757 std::string id = completedJobs_.front()->GetId(); | |
758 assert(jobsIndex_.find(id) != jobsIndex_.end()); | |
2557 | 759 |
2558 | 760 jobsIndex_.erase(id); |
761 delete(completedJobs_.front()); | |
762 completedJobs_.pop_front(); | |
763 } | |
764 } | |
765 } | |
766 | |
2557 | 767 |
2558 | 768 void MarkRunningAsCompleted(JobHandler& job, |
769 bool success) | |
770 { | |
771 LOG(INFO) << "Job has completed with " << (success ? "success" : "failure") | |
772 << ": " << job.GetId(); | |
2557 | 773 |
2558 | 774 boost::mutex::scoped_lock lock(mutex_); |
775 CheckInvariants(); | |
776 assert(job.GetState() == JobState_Running); | |
777 | |
778 job.SetState(success ? JobState_Success : JobState_Failure); | |
779 | |
780 completedJobs_.push_back(&job); | |
781 ForgetOldCompletedJobs(); | |
782 | |
783 CheckInvariants(); | |
784 } | |
2557 | 785 |
786 | |
2558 | 787 void MarkRunningAsRetry(JobHandler& job, |
788 unsigned int timeout) | |
789 { | |
790 LOG(INFO) << "Job scheduled for retry in " << timeout << "ms: " << job.GetId(); | |
791 | |
792 boost::mutex::scoped_lock lock(mutex_); | |
793 CheckInvariants(); | |
794 | |
795 assert(job.GetState() == JobState_Running && | |
796 retryJobs_.find(&job) == retryJobs_.end()); | |
797 | |
798 retryJobs_.insert(&job); | |
799 job.SetRetryState(timeout); | |
800 | |
801 CheckInvariants(); | |
802 } | |
803 | |
804 | |
805 void MarkRunningAsPaused(JobHandler& job) | |
806 { | |
807 LOG(INFO) << "Job paused: " << job.GetId(); | |
2557 | 808 |
2558 | 809 boost::mutex::scoped_lock lock(mutex_); |
810 CheckInvariants(); | |
811 assert(job.GetState() == JobState_Running); | |
812 | |
813 job.SetState(JobState_Paused); | |
2557 | 814 |
2558 | 815 CheckInvariants(); |
816 } | |
817 | |
818 | |
819 JobHandler* WaitPendingJob(unsigned int timeout) | |
820 { | |
821 boost::mutex::scoped_lock lock(mutex_); | |
2557 | 822 |
2558 | 823 while (pendingJobs_.empty()) |
824 { | |
825 if (timeout == 0) | |
826 { | |
827 pendingJobAvailable_.wait(lock); | |
828 } | |
829 else | |
830 { | |
831 bool success = pendingJobAvailable_.timed_wait | |
832 (lock, boost::posix_time::milliseconds(timeout)); | |
833 if (!success) | |
834 { | |
835 return NULL; | |
2557 | 836 } |
2556 | 837 } |
2557 | 838 } |
839 | |
2558 | 840 JobHandler* job = pendingJobs_.top(); |
841 pendingJobs_.pop(); | |
842 | |
843 job->SetState(JobState_Running); | |
844 return job; | |
845 } | |
2557 | 846 |
847 | |
2558 | 848 public: |
849 JobsRegistry() : | |
850 maxCompletedJobs_(10) | |
851 { | |
852 } | |
2557 | 853 |
854 | |
2558 | 855 ~JobsRegistry() |
856 { | |
857 for (JobsIndex::iterator it = jobsIndex_.begin(); it != jobsIndex_.end(); ++it) | |
2557 | 858 { |
2558 | 859 assert(it->second != NULL); |
860 delete it->second; | |
861 } | |
862 } | |
863 | |
864 | |
865 void SetMaxCompletedJobs(size_t i) | |
866 { | |
867 boost::mutex::scoped_lock lock(mutex_); | |
868 CheckInvariants(); | |
2557 | 869 |
2558 | 870 maxCompletedJobs_ = i; |
871 ForgetOldCompletedJobs(); | |
872 | |
873 CheckInvariants(); | |
874 } | |
875 | |
2557 | 876 |
2558 | 877 void ListJobs(std::set<std::string>& target) |
878 { | |
879 boost::mutex::scoped_lock lock(mutex_); | |
880 CheckInvariants(); | |
2557 | 881 |
2558 | 882 for (JobsIndex::const_iterator it = jobsIndex_.begin(); |
883 it != jobsIndex_.end(); ++it) | |
884 { | |
885 target.insert(it->first); | |
2557 | 886 } |
2558 | 887 } |
2557 | 888 |
889 | |
2558 | 890 void Submit(std::string& id, |
891 IJob* job, // Takes ownership | |
892 int priority) | |
893 { | |
894 std::auto_ptr<JobHandler> handler(new JobHandler(job, priority)); | |
895 | |
896 boost::mutex::scoped_lock lock(mutex_); | |
897 CheckInvariants(); | |
898 | |
899 id = handler->GetId(); | |
900 | |
901 pendingJobs_.push(handler.get()); | |
902 pendingJobAvailable_.notify_one(); | |
2557 | 903 |
2558 | 904 jobsIndex_.insert(std::make_pair(id, handler.release())); |
905 | |
906 LOG(INFO) << "New job submitted: " << id; | |
907 | |
908 CheckInvariants(); | |
909 } | |
2557 | 910 |
2558 | 911 |
912 void Submit(IJob* job, // Takes ownership | |
913 int priority) | |
914 { | |
915 std::string id; | |
916 Submit(id, job, priority); | |
917 } | |
2557 | 918 |
919 | |
2558 | 920 void SetPriority(const std::string& id, |
921 int priority) | |
922 { | |
923 LOG(INFO) << "Changing priority to " << priority << " for job: " << id; | |
2557 | 924 |
2558 | 925 boost::mutex::scoped_lock lock(mutex_); |
926 CheckInvariants(); | |
927 | |
928 JobsIndex::iterator found = jobsIndex_.find(id); | |
2557 | 929 |
2558 | 930 if (found == jobsIndex_.end()) |
931 { | |
932 LOG(WARNING) << "Unknown job: " << id; | |
2557 | 933 } |
2558 | 934 else |
935 { | |
936 found->second->SetPriority(priority); | |
2556 | 937 |
2558 | 938 if (found->second->GetState() == JobState_Pending) |
939 { | |
940 // If the job is pending, we need to reconstruct the | |
941 // priority queue, as the heap condition has changed | |
2557 | 942 |
2558 | 943 PendingJobs copy; |
944 std::swap(copy, pendingJobs_); | |
945 | |
946 assert(pendingJobs_.empty()); | |
947 while (!copy.empty()) | |
948 { | |
949 pendingJobs_.push(copy.top()); | |
950 copy.pop(); | |
951 } | |
2557 | 952 } |
953 } | |
954 | |
2558 | 955 CheckInvariants(); |
956 } | |
2557 | 957 |
958 | |
2558 | 959 void Pause(const std::string& id) |
960 { | |
961 LOG(INFO) << "Pausing job: " << id; | |
2557 | 962 |
2558 | 963 boost::mutex::scoped_lock lock(mutex_); |
964 CheckInvariants(); | |
2557 | 965 |
2558 | 966 JobsIndex::iterator found = jobsIndex_.find(id); |
2557 | 967 |
2558 | 968 if (found == jobsIndex_.end()) |
2557 | 969 { |
2558 | 970 LOG(WARNING) << "Unknown job: " << id; |
2557 | 971 } |
2558 | 972 else |
2556 | 973 { |
2558 | 974 switch (found->second->GetState()) |
2557 | 975 { |
2558 | 976 case JobState_Pending: |
2557 | 977 { |
978 // If the job is pending, we need to reconstruct the | |
2558 | 979 // priority queue to remove it |
2557 | 980 PendingJobs copy; |
981 std::swap(copy, pendingJobs_); | |
982 | |
983 assert(pendingJobs_.empty()); | |
984 while (!copy.empty()) | |
985 { | |
2558 | 986 if (copy.top()->GetId() != id) |
987 { | |
988 pendingJobs_.push(copy.top()); | |
989 } | |
990 | |
2557 | 991 copy.pop(); |
992 } | |
2558 | 993 |
994 found->second->SetState(JobState_Paused); | |
995 | |
996 break; | |
2557 | 997 } |
2558 | 998 |
999 case JobState_Retry: | |
1000 { | |
1001 RetryJobs::iterator item = retryJobs_.find(found->second); | |
1002 assert(item != retryJobs_.end()); | |
1003 retryJobs_.erase(item); | |
1004 | |
1005 found->second->SetState(JobState_Paused); | |
1006 | |
1007 break; | |
1008 } | |
1009 | |
1010 case JobState_Paused: | |
1011 case JobState_Success: | |
1012 case JobState_Failure: | |
1013 // Nothing to be done | |
1014 break; | |
1015 | |
1016 case JobState_Running: | |
1017 found->second->SchedulePause(); | |
1018 break; | |
1019 | |
1020 default: | |
1021 throw OrthancException(ErrorCode_InternalError); | |
2557 | 1022 } |
2556 | 1023 } |
1024 | |
2558 | 1025 CheckInvariants(); |
1026 } | |
2556 | 1027 |
2557 | 1028 |
2558 | 1029 void Resume(const std::string& id) |
1030 { | |
1031 LOG(INFO) << "Resuming job: " << id; | |
2557 | 1032 |
2558 | 1033 boost::mutex::scoped_lock lock(mutex_); |
1034 CheckInvariants(); | |
1035 | |
1036 JobsIndex::iterator found = jobsIndex_.find(id); | |
2557 | 1037 |
2558 | 1038 if (found == jobsIndex_.end()) |
1039 { | |
1040 LOG(WARNING) << "Unknown job: " << id; | |
1041 } | |
1042 else if (found->second->GetState() != JobState_Paused) | |
1043 { | |
1044 LOG(WARNING) << "Cannot resume a job that is not paused: " << id; | |
1045 } | |
1046 else | |
1047 { | |
1048 found->second->SetState(JobState_Pending); | |
1049 pendingJobs_.push(found->second); | |
1050 pendingJobAvailable_.notify_one(); | |
1051 } | |
2557 | 1052 |
2558 | 1053 CheckInvariants(); |
1054 } | |
2556 | 1055 |
2557 | 1056 |
2558 | 1057 void Resubmit(const std::string& id) |
1058 { | |
1059 LOG(INFO) << "Resubmitting failed job: " << id; | |
2557 | 1060 |
2558 | 1061 boost::mutex::scoped_lock lock(mutex_); |
1062 CheckInvariants(); | |
1063 | |
1064 JobsIndex::iterator found = jobsIndex_.find(id); | |
2557 | 1065 |
2558 | 1066 if (found == jobsIndex_.end()) |
1067 { | |
1068 LOG(WARNING) << "Unknown job: " << id; | |
2556 | 1069 } |
2558 | 1070 else if (found->second->GetState() != JobState_Failure) |
1071 { | |
1072 LOG(WARNING) << "Cannot resubmit a job that has not failed: " << id; | |
1073 } | |
1074 else | |
2557 | 1075 { |
2558 | 1076 bool ok = false; |
1077 for (CompletedJobs::iterator it = completedJobs_.begin(); | |
1078 it != completedJobs_.end(); ++it) | |
2557 | 1079 { |
2558 | 1080 if (*it == found->second) |
2557 | 1081 { |
2558 | 1082 ok = true; |
1083 completedJobs_.erase(it); | |
1084 break; | |
2557 | 1085 } |
1086 } | |
1087 | |
2558 | 1088 assert(ok); |
1089 | |
1090 found->second->SetState(JobState_Pending); | |
1091 pendingJobs_.push(found->second); | |
1092 pendingJobAvailable_.notify_one(); | |
2557 | 1093 } |
1094 | |
2558 | 1095 CheckInvariants(); |
1096 } | |
1097 | |
2557 | 1098 |
2558 | 1099 void ScheduleRetries() |
1100 { | |
1101 boost::mutex::scoped_lock lock(mutex_); | |
1102 CheckInvariants(); | |
1103 | |
1104 RetryJobs copy; | |
1105 std::swap(copy, retryJobs_); | |
1106 | |
1107 const boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time(); | |
1108 | |
1109 assert(retryJobs_.empty()); | |
1110 for (RetryJobs::iterator it = copy.begin(); it != copy.end(); ++it) | |
2557 | 1111 { |
2558 | 1112 if ((*it)->IsRetryReady(now)) |
2557 | 1113 { |
2558 | 1114 LOG(INFO) << "Retrying job: " << (*it)->GetId(); |
1115 (*it)->SetState(JobState_Pending); | |
1116 pendingJobs_.push(*it); | |
1117 pendingJobAvailable_.notify_one(); | |
2557 | 1118 } |
1119 else | |
1120 { | |
2558 | 1121 retryJobs_.insert(*it); |
2557 | 1122 } |
1123 } | |
1124 | |
2558 | 1125 CheckInvariants(); |
1126 } | |
2557 | 1127 |
2558 | 1128 |
1129 bool GetState(JobState& state, | |
1130 const std::string& id) | |
1131 { | |
1132 boost::mutex::scoped_lock lock(mutex_); | |
1133 CheckInvariants(); | |
1134 | |
1135 JobsIndex::const_iterator it = jobsIndex_.find(id); | |
1136 if (it == jobsIndex_.end()) | |
2556 | 1137 { |
2558 | 1138 return false; |
1139 } | |
1140 else | |
1141 { | |
1142 state = it->second->GetState(); | |
1143 return true; | |
1144 } | |
1145 } | |
1146 | |
1147 | |
1148 class RunningJob : public boost::noncopyable | |
1149 { | |
1150 private: | |
1151 JobsRegistry& that_; | |
2559 | 1152 JobHandler* handler_; |
1153 JobState targetState_; | |
1154 unsigned int retryTimeout_; | |
2556 | 1155 |
2558 | 1156 public: |
1157 RunningJob(JobsRegistry& that, | |
1158 unsigned int timeout) : | |
1159 that_(that), | |
1160 handler_(NULL), | |
1161 targetState_(JobState_Failure), | |
1162 retryTimeout_(0) | |
1163 { | |
1164 handler_ = that_.WaitPendingJob(timeout); | |
1165 } | |
2557 | 1166 |
2558 | 1167 ~RunningJob() |
1168 { | |
1169 if (IsValid()) | |
2556 | 1170 { |
2558 | 1171 switch (targetState_) |
2557 | 1172 { |
2558 | 1173 case JobState_Failure: |
1174 that_.MarkRunningAsCompleted(*handler_, false); | |
1175 break; | |
2557 | 1176 |
2558 | 1177 case JobState_Success: |
1178 that_.MarkRunningAsCompleted(*handler_, true); | |
1179 break; | |
2557 | 1180 |
2558 | 1181 case JobState_Paused: |
1182 that_.MarkRunningAsPaused(*handler_); | |
1183 break; | |
2557 | 1184 |
2558 | 1185 case JobState_Retry: |
1186 that_.MarkRunningAsRetry(*handler_, retryTimeout_); | |
1187 break; | |
2557 | 1188 |
2558 | 1189 default: |
1190 assert(0); | |
2557 | 1191 } |
2556 | 1192 } |
2558 | 1193 } |
2556 | 1194 |
2558 | 1195 bool IsValid() const |
1196 { | |
1197 return handler_ != NULL; | |
1198 } | |
1199 | |
1200 const std::string& GetId() const | |
1201 { | |
1202 if (IsValid()) | |
2556 | 1203 { |
2558 | 1204 return handler_->GetId(); |
2556 | 1205 } |
2558 | 1206 else |
2557 | 1207 { |
2558 | 1208 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
1209 } | |
1210 } | |
1211 | |
1212 int GetPriority() const | |
1213 { | |
1214 if (IsValid()) | |
1215 { | |
1216 return handler_->GetPriority(); | |
1217 } | |
1218 else | |
1219 { | |
1220 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
1221 } | |
1222 } | |
1223 | |
1224 bool IsPauseScheduled() | |
1225 { | |
1226 if (!IsValid()) | |
1227 { | |
1228 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
2557 | 1229 } |
1230 | |
2558 | 1231 boost::mutex::scoped_lock lock(that_.mutex_); |
1232 that_.CheckInvariants(); | |
1233 assert(handler_->GetState() == JobState_Running); | |
1234 | |
1235 return handler_->IsPauseScheduled(); | |
1236 } | |
1237 | |
1238 void MarkSuccess() | |
1239 { | |
1240 if (!IsValid()) | |
2557 | 1241 { |
2558 | 1242 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
2557 | 1243 } |
1244 | |
2558 | 1245 targetState_ = JobState_Success; |
1246 } | |
2557 | 1247 |
2558 | 1248 void MarkFailure() |
1249 { | |
1250 if (!IsValid()) | |
2557 | 1251 { |
2558 | 1252 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
2557 | 1253 } |
1254 | |
2558 | 1255 targetState_ = JobState_Failure; |
1256 } | |
1257 | |
1258 void SchedulePause() | |
1259 { | |
1260 if (!IsValid()) | |
2557 | 1261 { |
2558 | 1262 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
2557 | 1263 } |
1264 | |
2558 | 1265 targetState_ = JobState_Paused; |
1266 } | |
1267 | |
1268 void MarkRetry(unsigned int timeout) | |
1269 { | |
1270 if (!IsValid()) | |
2557 | 1271 { |
2558 | 1272 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
1273 } | |
2557 | 1274 |
2558 | 1275 targetState_ = JobState_Retry; |
1276 retryTimeout_ = timeout; | |
1277 } | |
2559 | 1278 |
1279 void ExecuteStep() | |
1280 { | |
1281 if (!IsValid()) | |
1282 { | |
1283 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
1284 } | |
1285 | |
1286 if (handler_->IsPauseScheduled()) | |
1287 { | |
1288 targetState_ = JobState_Paused; | |
1289 return; | |
1290 } | |
1291 | |
1292 std::auto_ptr<JobStepResult> result; | |
1293 | |
1294 { | |
1295 JobHandler::JobLock lock(*handler_); | |
1296 | |
1297 bool ok = false; | |
1298 | |
1299 try | |
1300 { | |
1301 result.reset(lock.GetJob().ExecuteStep()); | |
1302 lock.UpdateStatus(); | |
1303 ok = true; | |
1304 } | |
1305 catch (OrthancException& e) | |
1306 { | |
1307 lock.SetLastErrorCode(e.GetErrorCode()); | |
1308 } | |
1309 catch (boost::bad_lexical_cast&) | |
1310 { | |
1311 lock.SetLastErrorCode(ErrorCode_BadFileFormat); | |
1312 } | |
1313 catch (...) | |
1314 { | |
1315 lock.SetLastErrorCode(ErrorCode_InternalError); | |
1316 } | |
1317 | |
1318 if (ok) | |
1319 { | |
1320 lock.SetLastErrorCode(ErrorCode_Success); | |
1321 } | |
1322 else | |
1323 { | |
1324 result.reset(new JobStepResult(JobStepStatus_Failure)); | |
1325 } | |
1326 } | |
1327 | |
1328 switch (result->GetStatus()) | |
1329 { | |
1330 case JobStepStatus_Success: | |
1331 targetState_ = JobState_Success; | |
1332 break; | |
1333 | |
1334 case JobStepStatus_Failure: | |
1335 targetState_ = JobState_Failure; | |
1336 break; | |
1337 | |
1338 case JobStepStatus_Continue: | |
1339 targetState_ = JobState_Running; | |
1340 break; | |
1341 | |
1342 case JobStepStatus_Retry: | |
1343 targetState_ = JobState_Retry; | |
1344 retryTimeout_ = dynamic_cast<RetryResult&>(*result).GetTimeout(); | |
1345 break; | |
1346 | |
1347 default: | |
1348 throw OrthancException(ErrorCode_InternalError); | |
1349 } | |
1350 } | |
2556 | 1351 }; |
2558 | 1352 }; |
2556 | 1353 } |
2557 | 1354 |
1355 | |
1356 | |
1357 class DummyJob : public Orthanc::IJob | |
1358 { | |
1359 private: | |
1360 JobStepResult result_; | |
1361 | |
1362 public: | |
1363 DummyJob() : | |
1364 result_(Orthanc::JobStepStatus_Success) | |
1365 { | |
1366 } | |
1367 | |
1368 explicit DummyJob(JobStepResult result) : | |
1369 result_(result) | |
1370 { | |
1371 } | |
1372 | |
1373 virtual JobStepResult* ExecuteStep() | |
1374 { | |
1375 return new JobStepResult(result_); | |
1376 } | |
1377 | |
1378 virtual void ReleaseResources() | |
1379 { | |
1380 } | |
1381 | |
1382 virtual float GetProgress() | |
1383 { | |
1384 return 0; | |
1385 } | |
1386 | |
1387 virtual void FormatStatus(Json::Value& value) | |
1388 { | |
1389 } | |
1390 }; | |
1391 | |
1392 | |
2558 | 1393 static bool CheckState(Orthanc::JobsRegistry& registry, |
2557 | 1394 const std::string& id, |
1395 Orthanc::JobState state) | |
1396 { | |
1397 Orthanc::JobState s; | |
2558 | 1398 if (registry.GetState(s, id)) |
2557 | 1399 { |
1400 return state == s; | |
1401 } | |
1402 else | |
1403 { | |
1404 return false; | |
1405 } | |
1406 } | |
1407 | |
1408 | |
2558 | 1409 TEST(JobsRegistry, Priority) |
2557 | 1410 { |
2558 | 1411 JobsRegistry registry; |
2557 | 1412 |
1413 std::string i1, i2, i3, i4; | |
2558 | 1414 registry.Submit(i1, new DummyJob(), 10); |
1415 registry.Submit(i2, new DummyJob(), 30); | |
1416 registry.Submit(i3, new DummyJob(), 20); | |
1417 registry.Submit(i4, new DummyJob(), 5); | |
2557 | 1418 |
2558 | 1419 registry.SetMaxCompletedJobs(2); |
2557 | 1420 |
1421 std::set<std::string> id; | |
2558 | 1422 registry.ListJobs(id); |
2557 | 1423 |
1424 ASSERT_EQ(4u, id.size()); | |
1425 ASSERT_TRUE(id.find(i1) != id.end()); | |
1426 ASSERT_TRUE(id.find(i2) != id.end()); | |
1427 ASSERT_TRUE(id.find(i3) != id.end()); | |
1428 ASSERT_TRUE(id.find(i4) != id.end()); | |
1429 | |
2558 | 1430 ASSERT_TRUE(CheckState(registry, i2, Orthanc::JobState_Pending)); |
2557 | 1431 |
1432 { | |
2558 | 1433 JobsRegistry::RunningJob job(registry, 0); |
2557 | 1434 ASSERT_TRUE(job.IsValid()); |
1435 ASSERT_EQ(30, job.GetPriority()); | |
1436 ASSERT_EQ(i2, job.GetId()); | |
1437 | |
2558 | 1438 ASSERT_TRUE(CheckState(registry, i2, Orthanc::JobState_Running)); |
2557 | 1439 } |
1440 | |
2558 | 1441 ASSERT_TRUE(CheckState(registry, i2, Orthanc::JobState_Failure)); |
1442 ASSERT_TRUE(CheckState(registry, i3, Orthanc::JobState_Pending)); | |
2557 | 1443 |
1444 { | |
2558 | 1445 JobsRegistry::RunningJob job(registry, 0); |
2557 | 1446 ASSERT_TRUE(job.IsValid()); |
1447 ASSERT_EQ(20, job.GetPriority()); | |
1448 ASSERT_EQ(i3, job.GetId()); | |
1449 | |
1450 job.MarkSuccess(); | |
1451 | |
2558 | 1452 ASSERT_TRUE(CheckState(registry, i3, Orthanc::JobState_Running)); |
2557 | 1453 } |
1454 | |
2558 | 1455 ASSERT_TRUE(CheckState(registry, i3, Orthanc::JobState_Success)); |
2557 | 1456 |
1457 { | |
2558 | 1458 JobsRegistry::RunningJob job(registry, 0); |
2557 | 1459 ASSERT_TRUE(job.IsValid()); |
1460 ASSERT_EQ(10, job.GetPriority()); | |
1461 ASSERT_EQ(i1, job.GetId()); | |
1462 } | |
1463 | |
1464 { | |
2558 | 1465 JobsRegistry::RunningJob job(registry, 0); |
2557 | 1466 ASSERT_TRUE(job.IsValid()); |
1467 ASSERT_EQ(5, job.GetPriority()); | |
1468 ASSERT_EQ(i4, job.GetId()); | |
1469 } | |
1470 | |
1471 { | |
2558 | 1472 JobsRegistry::RunningJob job(registry, 1); |
2557 | 1473 ASSERT_FALSE(job.IsValid()); |
1474 } | |
1475 | |
1476 Orthanc::JobState s; | |
2558 | 1477 ASSERT_TRUE(registry.GetState(s, i1)); |
1478 ASSERT_FALSE(registry.GetState(s, i2)); // Removed because oldest | |
1479 ASSERT_FALSE(registry.GetState(s, i3)); // Removed because second oldest | |
1480 ASSERT_TRUE(registry.GetState(s, i4)); | |
2557 | 1481 |
2558 | 1482 registry.SetMaxCompletedJobs(1); // (*) |
1483 ASSERT_FALSE(registry.GetState(s, i1)); // Just discarded by (*) | |
1484 ASSERT_TRUE(registry.GetState(s, i4)); | |
2557 | 1485 } |
1486 | |
1487 | |
2558 | 1488 TEST(JobsRegistry, Simultaneous) |
2557 | 1489 { |
2558 | 1490 JobsRegistry registry; |
1491 | |
1492 std::string i1, i2; | |
1493 registry.Submit(i1, new DummyJob(), 20); | |
1494 registry.Submit(i2, new DummyJob(), 10); | |
1495 | |
1496 ASSERT_TRUE(CheckState(registry, i1, Orthanc::JobState_Pending)); | |
1497 ASSERT_TRUE(CheckState(registry, i2, Orthanc::JobState_Pending)); | |
1498 | |
1499 { | |
1500 JobsRegistry::RunningJob job1(registry, 0); | |
1501 JobsRegistry::RunningJob job2(registry, 0); | |
1502 | |
1503 ASSERT_TRUE(job1.IsValid()); | |
1504 ASSERT_TRUE(job2.IsValid()); | |
1505 | |
1506 job1.MarkFailure(); | |
1507 job2.MarkSuccess(); | |
1508 | |
1509 ASSERT_TRUE(CheckState(registry, i1, Orthanc::JobState_Running)); | |
1510 ASSERT_TRUE(CheckState(registry, i2, Orthanc::JobState_Running)); | |
1511 } | |
1512 | |
1513 ASSERT_TRUE(CheckState(registry, i1, Orthanc::JobState_Failure)); | |
1514 ASSERT_TRUE(CheckState(registry, i2, Orthanc::JobState_Success)); | |
1515 } | |
1516 | |
1517 | |
1518 TEST(JobsRegistry, Resubmit) | |
1519 { | |
1520 JobsRegistry registry; | |
2557 | 1521 |
1522 std::string id; | |
2558 | 1523 registry.Submit(id, new DummyJob(), 10); |
2557 | 1524 |
2558 | 1525 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); |
2557 | 1526 |
2558 | 1527 registry.Resubmit(id); |
1528 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); | |
2557 | 1529 |
1530 { | |
2558 | 1531 JobsRegistry::RunningJob job(registry, 0); |
2557 | 1532 ASSERT_TRUE(job.IsValid()); |
1533 job.MarkFailure(); | |
1534 | |
2558 | 1535 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); |
2557 | 1536 |
2558 | 1537 registry.Resubmit(id); |
1538 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); | |
2557 | 1539 } |
1540 | |
2558 | 1541 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Failure)); |
2557 | 1542 |
2558 | 1543 registry.Resubmit(id); |
1544 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); | |
2557 | 1545 |
1546 { | |
2558 | 1547 JobsRegistry::RunningJob job(registry, 0); |
2557 | 1548 ASSERT_TRUE(job.IsValid()); |
1549 ASSERT_EQ(id, job.GetId()); | |
1550 | |
1551 job.MarkSuccess(); | |
2558 | 1552 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); |
1553 } | |
1554 | |
1555 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Success)); | |
1556 | |
1557 registry.Resubmit(id); | |
1558 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Success)); | |
1559 } | |
1560 | |
1561 | |
1562 TEST(JobsRegistry, Retry) | |
1563 { | |
1564 JobsRegistry registry; | |
1565 | |
1566 std::string id; | |
1567 registry.Submit(id, new DummyJob(), 10); | |
1568 | |
1569 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); | |
1570 | |
1571 { | |
1572 JobsRegistry::RunningJob job(registry, 0); | |
1573 ASSERT_TRUE(job.IsValid()); | |
1574 job.MarkRetry(0); | |
1575 | |
1576 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); | |
1577 } | |
1578 | |
1579 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Retry)); | |
1580 | |
1581 registry.Resubmit(id); | |
1582 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Retry)); | |
1583 | |
1584 registry.ScheduleRetries(); | |
1585 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); | |
1586 | |
1587 { | |
1588 JobsRegistry::RunningJob job(registry, 0); | |
1589 ASSERT_TRUE(job.IsValid()); | |
1590 job.MarkSuccess(); | |
1591 | |
1592 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); | |
2557 | 1593 } |
1594 | |
2558 | 1595 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Success)); |
1596 } | |
1597 | |
1598 | |
1599 TEST(JobsRegistry, PausePending) | |
1600 { | |
1601 JobsRegistry registry; | |
1602 | |
1603 std::string id; | |
1604 registry.Submit(id, new DummyJob(), 10); | |
1605 | |
1606 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); | |
1607 | |
1608 registry.Pause(id); | |
1609 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Paused)); | |
1610 | |
1611 registry.Pause(id); | |
1612 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Paused)); | |
1613 | |
1614 registry.Resubmit(id); | |
1615 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Paused)); | |
1616 | |
1617 registry.Resume(id); | |
1618 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); | |
2557 | 1619 } |
2558 | 1620 |
1621 | |
1622 TEST(JobsRegistry, PauseRunning) | |
1623 { | |
1624 JobsRegistry registry; | |
1625 | |
1626 std::string id; | |
1627 registry.Submit(id, new DummyJob(), 10); | |
1628 | |
1629 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); | |
1630 | |
1631 { | |
1632 JobsRegistry::RunningJob job(registry, 0); | |
1633 ASSERT_TRUE(job.IsValid()); | |
1634 | |
1635 registry.Resubmit(id); | |
1636 job.SchedulePause(); | |
1637 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); | |
1638 } | |
1639 | |
1640 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Paused)); | |
1641 | |
1642 registry.Resubmit(id); | |
1643 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Paused)); | |
1644 | |
1645 registry.Resume(id); | |
1646 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); | |
1647 | |
1648 { | |
1649 JobsRegistry::RunningJob job(registry, 0); | |
1650 ASSERT_TRUE(job.IsValid()); | |
1651 | |
1652 job.MarkSuccess(); | |
1653 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); | |
1654 } | |
1655 | |
1656 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Success)); | |
1657 } | |
1658 | |
1659 | |
1660 TEST(JobsRegistry, PauseRetry) | |
1661 { | |
1662 JobsRegistry registry; | |
1663 | |
1664 std::string id; | |
1665 registry.Submit(id, new DummyJob(), 10); | |
1666 | |
1667 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); | |
1668 | |
1669 { | |
1670 JobsRegistry::RunningJob job(registry, 0); | |
1671 ASSERT_TRUE(job.IsValid()); | |
1672 | |
1673 job.MarkRetry(0); | |
1674 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); | |
1675 } | |
1676 | |
1677 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Retry)); | |
1678 | |
1679 registry.Pause(id); | |
1680 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Paused)); | |
1681 | |
1682 registry.Resume(id); | |
1683 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending)); | |
1684 | |
1685 { | |
1686 JobsRegistry::RunningJob job(registry, 0); | |
1687 ASSERT_TRUE(job.IsValid()); | |
1688 | |
1689 job.MarkSuccess(); | |
1690 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running)); | |
1691 } | |
1692 | |
1693 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Success)); | |
1694 } |