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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1681
diff changeset
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
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 #include "gtest/gtest.h"
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
781
f0ac3a53ccf2 scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 779
diff changeset
37 #include "../OrthancServer/Scheduler/ServerScheduler.h"
723
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 #include "../Core/Toolbox.h"
760
b2a62f22fbe8 lock abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 723
diff changeset
41 #include "../Core/MultiThreading/Locker.h"
b2a62f22fbe8 lock abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 723
diff changeset
42 #include "../Core/MultiThreading/Mutex.h"
b2a62f22fbe8 lock abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 723
diff changeset
43 #include "../Core/MultiThreading/ReaderWriterLock.h"
723
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 using namespace Orthanc;
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 namespace
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 {
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 private:
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 int value_;
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 std::set<int>& target_;
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 public:
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 DynamicInteger(int value, std::set<int>& target) :
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 value_(value), target_(target)
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 {
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 }
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 int GetValue() const
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 {
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 return value_;
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 }
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 };
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 }
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 TEST(MultiThreading, SharedMessageQueueBasic)
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 {
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 std::set<int> s;
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 SharedMessageQueue q;
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 ASSERT_TRUE(q.WaitEmpty(0));
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 q.Enqueue(new DynamicInteger(10, s));
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 ASSERT_FALSE(q.WaitEmpty(1));
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 q.Enqueue(new DynamicInteger(20, s));
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 q.Enqueue(new DynamicInteger(30, s));
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 q.Enqueue(new DynamicInteger(40, s));
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 std::auto_ptr<DynamicInteger> i;
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 i.reset(dynamic_cast<DynamicInteger*>(q.Dequeue(1))); ASSERT_EQ(10, i->GetValue());
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 i.reset(dynamic_cast<DynamicInteger*>(q.Dequeue(1))); ASSERT_EQ(20, i->GetValue());
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 i.reset(dynamic_cast<DynamicInteger*>(q.Dequeue(1))); ASSERT_EQ(30, i->GetValue());
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 ASSERT_FALSE(q.WaitEmpty(1));
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 i.reset(dynamic_cast<DynamicInteger*>(q.Dequeue(1))); ASSERT_EQ(40, i->GetValue());
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 ASSERT_TRUE(q.WaitEmpty(0));
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 ASSERT_EQ(NULL, q.Dequeue(1));
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 }
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 TEST(MultiThreading, SharedMessageQueueClean)
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 {
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 std::set<int> s;
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 try
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 {
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 SharedMessageQueue q;
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 q.Enqueue(new DynamicInteger(10, s));
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 }
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 catch (OrthancException&)
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 {
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 }
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 }
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107
0da078f3affc multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108
760
b2a62f22fbe8 lock abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 723
diff changeset
109 TEST(MultiThreading, Mutex)
b2a62f22fbe8 lock abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 723
diff changeset
110 {
b2a62f22fbe8 lock abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 723
diff changeset
111 Mutex mutex;
b2a62f22fbe8 lock abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 723
diff changeset
112 Locker locker(mutex);
b2a62f22fbe8 lock abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 723
diff changeset
113 }
b2a62f22fbe8 lock abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 723
diff changeset
114
b2a62f22fbe8 lock abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 723
diff changeset
115
b2a62f22fbe8 lock abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 723
diff changeset
116 TEST(MultiThreading, ReaderWriterLock)
b2a62f22fbe8 lock abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 723
diff changeset
117 {
b2a62f22fbe8 lock abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 723
diff changeset
118 ReaderWriterLock lock;
b2a62f22fbe8 lock abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 723
diff changeset
119
b2a62f22fbe8 lock abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 723
diff changeset
120 {
b2a62f22fbe8 lock abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 723
diff changeset
121 Locker locker1(lock.ForReader());
b2a62f22fbe8 lock abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 723
diff changeset
122 Locker locker2(lock.ForReader());
b2a62f22fbe8 lock abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 723
diff changeset
123 }
b2a62f22fbe8 lock abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 723
diff changeset
124
b2a62f22fbe8 lock abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 723
diff changeset
125 {
b2a62f22fbe8 lock abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 723
diff changeset
126 Locker locker3(lock.ForWriter());
b2a62f22fbe8 lock abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 723
diff changeset
127 }
b2a62f22fbe8 lock abstraction
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 723
diff changeset
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>
parents: 770 769
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
aa4b8895cd23 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1907
diff changeset
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
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
159
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
160
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
161
1000
13e230bbd882 rename filter to command
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 995
diff changeset
162 class Tutu : public IServerCommand
765
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
163 {
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
164 private:
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
165 int factor_;
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
166
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
167 public:
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
168 Tutu(int f) : factor_(f)
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
169 {
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
170 }
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
171
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
172 virtual bool Apply(ListOfStrings& outputs,
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
173 const ListOfStrings& inputs)
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
174 {
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
175 for (ListOfStrings::const_iterator
1304
7b6925b0890d cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
176 it = inputs.begin(); it != inputs.end(); ++it)
765
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
177 {
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
178 int a = boost::lexical_cast<int>(*it);
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
179 int b = factor_ * a;
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
180
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
181 printf("%d * %d = %d\n", a, factor_, b);
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
182
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
183 //if (a == 84) { printf("BREAK\n"); return false; }
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
184
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
185 outputs.push_back(boost::lexical_cast<std::string>(b));
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
186 }
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
187
2242
4e8e0ad2001c move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2222
diff changeset
188 SystemToolbox::USleep(30000);
765
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
189
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
190 return true;
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
191 }
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
192 };
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
193
768
476febedc516 improvements
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 765
diff changeset
194
770
a64ca424e0e2 fix test
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 768
diff changeset
195 static void Tata(ServerScheduler* s, ServerJob* j, bool* done)
768
476febedc516 improvements
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 765
diff changeset
196 {
1000
13e230bbd882 rename filter to command
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 995
diff changeset
197 typedef IServerCommand::ListOfStrings ListOfStrings;
779
76eb563f08f0 improvements
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 777
diff changeset
198
770
a64ca424e0e2 fix test
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 768
diff changeset
199 while (!(*done))
768
476febedc516 improvements
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 765
diff changeset
200 {
476febedc516 improvements
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 765
diff changeset
201 ListOfStrings l;
476febedc516 improvements
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 765
diff changeset
202 s->GetListOfJobs(l);
1304
7b6925b0890d cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
203 for (ListOfStrings::iterator it = l.begin(); it != l.end(); ++it)
7b6925b0890d cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
204 {
7b6925b0890d cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
205 printf(">> %s: %0.1f\n", it->c_str(), 100.0f * s->GetProgress(*it));
7b6925b0890d cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
206 }
2242
4e8e0ad2001c move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2222
diff changeset
207 SystemToolbox::USleep(3000);
768
476febedc516 improvements
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 765
diff changeset
208 }
476febedc516 improvements
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 765
diff changeset
209 }
476febedc516 improvements
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 765
diff changeset
210
476febedc516 improvements
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 765
diff changeset
211
1009
26642cecd36d clearer job interface
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1005
diff changeset
212 TEST(MultiThreading, ServerScheduler)
765
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
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
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
215
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
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
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
221 f2.AddInput(boost::lexical_cast<std::string>(42));
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
222 //f3.AddInput(boost::lexical_cast<std::string>(42));
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
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
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
230
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
231 job.SetDescription("tutu");
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
232
770
a64ca424e0e2 fix test
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 768
diff changeset
233 bool done = false;
a64ca424e0e2 fix test
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 768
diff changeset
234 boost::thread t(Tata, &scheduler, &job, &done);
768
476febedc516 improvements
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 765
diff changeset
235
476febedc516 improvements
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 765
diff changeset
236
765
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
237 //scheduler.Submit(job);
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
238
1000
13e230bbd882 rename filter to command
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 995
diff changeset
239 IServerCommand::ListOfStrings l;
765
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
240 scheduler.SubmitAndWait(l, job);
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
241
1492
9f66a12eb8fc fix warnings for gcc 5
jodogne
parents: 1486
diff changeset
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
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
247 {
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
248 printf("** %s\n", i->c_str());
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
249 }
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
250
2140
aa4b8895cd23 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1907
diff changeset
251 //SystemToolbox::ServerBarrier();
2242
4e8e0ad2001c move USleep() in SystemToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2222
diff changeset
252 //SystemToolbox::USleep(3000000);
768
476febedc516 improvements
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 765
diff changeset
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
a64ca424e0e2 fix test
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 768
diff changeset
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
fc97f762834c scheduler
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 760
diff changeset
261 }
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
262
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
263
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
264
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
265
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
266
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
267 #if !defined(ORTHANC_SANDBOXED)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
268 # error The macro ORTHANC_SANDBOXED must be defined
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
269 #endif
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
270
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
271 #if ORTHANC_SANDBOXED == 1
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
272 # error The job engine cannot be used in sandboxed environments
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
273 #endif
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
274
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
275 #include "../Core/Logging.h"
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
276
2559
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
277 #include <boost/math/special_functions/round.hpp>
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
278 #include <boost/date_time/posix_time/posix_time.hpp>
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
279 #include <queue>
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
280
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
281 namespace Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
282 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
283 enum JobState
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
284 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
285 JobState_Pending,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
286 JobState_Running,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
287 JobState_Success,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
288 JobState_Failure,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
289 JobState_Paused,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
290 JobState_Retry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
291 };
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
292
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
293 enum JobStepCode
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
294 {
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
295 JobStepCode_Success,
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
296 JobStepCode_Failure,
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
297 JobStepCode_Continue,
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
298 JobStepCode_Retry
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
299 };
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
300
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
301
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
302 class JobStepResult
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
303 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
304 private:
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
305 JobStepCode status_;
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
306
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
307 public:
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
308 explicit JobStepResult(JobStepCode status) :
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
309 status_(status)
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
310 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
311 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
312
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
313 virtual ~JobStepResult()
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
314 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
315 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
316
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
317 JobStepCode GetCode() const
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
318 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
319 return status_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
320 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
321 };
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
322
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
323
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
324 class RetryResult : public JobStepResult
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
325 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
326 private:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
327 unsigned int timeout_; // Retry after "timeout_" milliseconds
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
328
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
329 public:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
333 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
334 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
335
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
336 unsigned int GetTimeout() const
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
337 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
338 return timeout_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
339 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
340 };
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
341
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
342
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
343 class IJob : public boost::noncopyable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
344 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
345 public:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
346 virtual ~IJob()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
347 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
348 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
349
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
350 virtual JobStepResult* ExecuteStep() = 0;
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
351
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
352 virtual void ReleaseResources() = 0; // For pausing jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
353
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
354 virtual float GetProgress() = 0;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
355
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
356 virtual void GetDescription(Json::Value& value) = 0;
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
357 };
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
358
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
359
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
360 class JobStatus
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
361 {
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
362 private:
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
363 ErrorCode errorCode_;
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
364 float progress_;
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
365 Json::Value description_;
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
366
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
367 public:
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
368 JobStatus() :
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
369 errorCode_(ErrorCode_Success),
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
370 progress_(0),
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
371 description_(Json::objectValue)
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
372 {
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
373 }
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
374
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
375 JobStatus(ErrorCode code,
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
376 IJob& job) :
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
377 errorCode_(code),
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
378 progress_(job.GetProgress())
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
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
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
382 {
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
383 throw OrthancException(ErrorCode_ParameterOutOfRange);
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
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
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
402 }
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
403 };
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
404
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
405
2559
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
406 class JobInfo
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
407 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
408 private:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
409 std::string id_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
410 int priority_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
411 ErrorCode errorCode_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
412 JobState state_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
413 boost::posix_time::ptime infoTime_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
414 boost::posix_time::ptime creationTime_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
415 boost::posix_time::time_duration runtime_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
416 boost::posix_time::ptime eta_;
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
417 JobStatus status_;
2559
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
418
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
419 public:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
420 JobInfo(const std::string& id,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
421 int priority,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
422 JobState state,
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
423 const JobStatus& status,
2559
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
424 const boost::posix_time::ptime& creationTime,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
425 const boost::posix_time::time_duration& runtime) :
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
426 id_(id),
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
427 priority_(priority),
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
428 state_(state),
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
429 infoTime_(boost::posix_time::microsec_clock::universal_time()),
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
430 creationTime_(creationTime),
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
431 runtime_(runtime),
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
432 status_(status)
2559
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
433 {
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
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
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
436 eta_ = infoTime_ + boost::posix_time::milliseconds(remaining);
2559
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
437 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
438
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
439 const std::string& GetIdentifier() const
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
440 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
441 return id_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
442 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
443
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
444 int GetPriority() const
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
445 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
446 return priority_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
447 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
448
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
449 ErrorCode GetErrorCode() const
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
450 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
451 return errorCode_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
452 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
453
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
454 JobState GetState() const
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
455 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
456 return state_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
457 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
458
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
459 const boost::posix_time::ptime& GetInfoTime() const
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
460 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
461 return infoTime_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
462 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
463
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
464 const boost::posix_time::ptime& GetCreationTime() const
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
465 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
466 return creationTime_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
467 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
468
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
469 const boost::posix_time::time_duration& GetRuntime() const
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
470 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
471 return runtime_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
472 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
473
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
474 const boost::posix_time::ptime& GetEstimatedTimeOfArrival() const
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
475 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
476 return eta_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
477 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
478
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
479 const JobStatus& GetStatus() const
2559
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
480 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
481 return status_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
482 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
483
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
484 JobStatus& GetStatus()
2559
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
485 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
486 return status_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
487 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
488 };
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
489
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
490
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
491 class JobHandler : public boost::noncopyable
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
492 {
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
493 private:
2559
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
494 std::string id_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
495 JobState state_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
496 std::auto_ptr<IJob> job_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
497 int priority_; // "+inf()" means highest priority
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
498 boost::posix_time::ptime creationTime_;
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
499 boost::posix_time::ptime lastStateChangeTime_;
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
500 boost::posix_time::time_duration runtime_;
2559
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
501 boost::posix_time::ptime retryTime_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
502 bool pauseScheduled_;
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
503 JobStatus lastStatus_;
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
504
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
505 void SetStateInternal(JobState state)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
506 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
507 const boost::posix_time::ptime now = boost::posix_time::microsec_clock::universal_time();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
508
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
509 if (state_ == JobState_Running)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
510 {
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
511 runtime_ += (now - lastStateChangeTime_);
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
512 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
513
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
514 state_ = state;
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
515 lastStateChangeTime_ = now;
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
516 pauseScheduled_ = false;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
517 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
518
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
519 public:
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
520 JobHandler(IJob* job,
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
521 int priority) :
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
522 id_(Toolbox::GenerateUuid()),
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
523 state_(JobState_Pending),
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
524 job_(job),
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
525 priority_(priority),
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
526 creationTime_(boost::posix_time::microsec_clock::universal_time()),
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
527 lastStateChangeTime_(creationTime_),
2559
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
528 runtime_(boost::posix_time::milliseconds(0)),
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
529 retryTime_(creationTime_),
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
530 pauseScheduled_(false)
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
531 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
532 if (job == NULL)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
533 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
534 throw OrthancException(ErrorCode_NullPointer);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
535 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
536 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
537
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
538 const std::string& GetId() const
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
539 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
540 return id_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
541 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
542
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
543 IJob& GetJob() const
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
544 {
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
545 assert(job_.get() != NULL);
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
546 return *job_;
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
547 }
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
548
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
549 void SetPriority(int priority)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
550 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
551 priority_ = priority;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
552 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
553
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
554 int GetPriority() const
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
555 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
556 return priority_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
557 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
558
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
559 JobState GetState() const
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
560 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
561 return state_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
562 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
563
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
564 void SetState(JobState state)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
565 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
566 if (state == JobState_Retry)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
567 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
568 // Use "SetRetryState()"
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
569 throw OrthancException(ErrorCode_BadSequenceOfCalls);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
570 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
571 else
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
572 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
573 SetStateInternal(state);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
574 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
575 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
576
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
577 void SetRetryState(unsigned int timeout)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
578 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
579 if (state_ == JobState_Running)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
580 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
581 SetStateInternal(JobState_Retry);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
582 retryTime_ = (boost::posix_time::microsec_clock::universal_time() +
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
583 boost::posix_time::milliseconds(timeout));
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
584 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
585 else
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
586 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
587 // Only valid for running jobs
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
588 throw OrthancException(ErrorCode_BadSequenceOfCalls);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
589 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
590 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
591
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
592 void SchedulePause()
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
593 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
594 if (state_ == JobState_Running)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
595 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
596 pauseScheduled_ = true;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
597 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
598 else
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
599 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
600 // Only valid for running jobs
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
601 throw OrthancException(ErrorCode_BadSequenceOfCalls);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
602 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
603 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
604
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
605 bool IsPauseScheduled()
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
606 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
607 return pauseScheduled_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
608 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
609
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
610 bool IsRetryReady(const boost::posix_time::ptime& now) const
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
611 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
612 if (state_ != JobState_Retry)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
613 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
614 throw OrthancException(ErrorCode_BadSequenceOfCalls);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
615 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
616 else
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
617 {
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
618 return retryTime_ <= now;
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
619 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
620 }
2559
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
621
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
622 const JobStatus& GetLastStatus() const
2559
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
623 {
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
624 return lastStatus_;
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
625 }
2559
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
626
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
627 void SetLastStatus(const JobStatus& status)
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
628 {
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
629 lastStatus_ = status;
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
630 }
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
631 };
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
632
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
633
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
634 class JobsRegistry : public boost::noncopyable
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
635 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
636 private:
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
637 struct PriorityComparator
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
638 {
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
639 bool operator() (JobHandler*& a,
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
640 JobHandler*& b) const
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
641 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
642 return a->GetPriority() < b->GetPriority();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
643 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
644 };
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
645
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
648 typedef std::set<JobHandler*> RetryJobs;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
649 typedef std::priority_queue<JobHandler*,
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
650 std::vector<JobHandler*>, // Could be a "std::deque"
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
651 PriorityComparator> PendingJobs;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
661
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
662
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
665 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
666 PendingJobs copy = pendingJobs_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
667 while (!copy.empty())
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
674 copy.pop();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
675 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
676
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
677 return false;
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
678 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
679
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
680 bool IsCompletedJob(JobHandler& job) const
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
697 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
698 #endif
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
699
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
712
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
713 assert(completedJobs_.size() <= maxCompletedJobs_);
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
732
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
733 assert(job.GetId() == it->first);
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
760 }
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
761
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
777 }
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
778 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
779 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
845
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
921 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
925 PendingJobs copy;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
926 std::swap(copy, pendingJobs_);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
927
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
928 assert(pendingJobs_.empty());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
929 while (!copy.empty())
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
930 {
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
931 pendingJobs_.push(copy.top());
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
932 copy.pop();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
933 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
934 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
935 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
936
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
937 CheckInvariants();
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
938 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
939
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
951 {
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
952 LOG(WARNING) << "Unknown job: " << id;
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
953 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
954 else
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1008 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1009
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
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
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
1038
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
1039 void Resubmit(const std::string& id)
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
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
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
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
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
1061 {
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
1062 if (*it == found->second)
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
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
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
1067 }
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
1068 }
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
1069
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
1070 assert(ok);
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
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
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
1076
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
1077 CheckInvariants();
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1078 }
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1079
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1104 }
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1108 }
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
1109
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
1119 {
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
1120 return false;
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
1121 }
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1122 else
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
1126 }
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1127 }
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
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
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
1184 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
1185
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
1186 ~RunningJob()
2559
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
1214 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
1215
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
1216 bool IsValid() const
2559
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
1237 {
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
1238 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2559
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
1239 }
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
1240 else
2559
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
1241 {
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
1242 return priority_;
2559
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
1243 }
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
1244 }
2559
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
1249 {
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
1250 throw OrthancException(ErrorCode_BadSequenceOfCalls);
2559
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
1251 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
1252 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2563
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
1325 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
1326 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
1327
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
1328 bool ExecuteStep()
2559
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
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
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2558
diff changeset
1406 }
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
1407 };
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
1408 };
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
1409 }
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1410
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1411
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1412
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1413 class DummyJob : public Orthanc::IJob
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1414 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1415 private:
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1416 JobStepResult result_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1417
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1418 public:
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1419 DummyJob() :
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
1420 result_(Orthanc::JobStepCode_Success)
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1421 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1422 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1423
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1424 explicit DummyJob(JobStepResult result) :
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
1425 result_(result)
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1426 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1427 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1428
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1429 virtual JobStepResult* ExecuteStep()
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1430 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1431 return new JobStepResult(result_);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1432 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1433
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1434 virtual void ReleaseResources()
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1435 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1436 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1437
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1438 virtual float GetProgress()
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1439 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1440 return 0;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1441 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1442
2563
98dfc1948d00 RunningJob::ExecuteStep()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2562
diff changeset
1443 virtual void GetDescription(Json::Value& value)
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1444 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1445 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1446 };
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1447
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1448
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1449 static bool CheckState(Orthanc::JobsRegistry& registry,
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1450 const std::string& id,
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1451 Orthanc::JobState state)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1452 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1453 Orthanc::JobState s;
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1454 if (registry.GetState(s, id))
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1455 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1456 return state == s;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1457 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1458 else
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1459 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1460 return false;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1461 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1462 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1463
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1464
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1465 TEST(JobsRegistry, Priority)
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1466 {
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1467 JobsRegistry registry;
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1468
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1469 std::string i1, i2, i3, i4;
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1470 registry.Submit(i1, new DummyJob(), 10);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1471 registry.Submit(i2, new DummyJob(), 30);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1472 registry.Submit(i3, new DummyJob(), 20);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1473 registry.Submit(i4, new DummyJob(), 5);
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1474
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1475 registry.SetMaxCompletedJobs(2);
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1476
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1477 std::set<std::string> id;
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1478 registry.ListJobs(id);
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1479
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1480 ASSERT_EQ(4u, id.size());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1481 ASSERT_TRUE(id.find(i1) != id.end());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1482 ASSERT_TRUE(id.find(i2) != id.end());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1483 ASSERT_TRUE(id.find(i3) != id.end());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1484 ASSERT_TRUE(id.find(i4) != id.end());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1485
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1486 ASSERT_TRUE(CheckState(registry, i2, Orthanc::JobState_Pending));
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1487
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1488 {
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1489 JobsRegistry::RunningJob job(registry, 0);
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1490 ASSERT_TRUE(job.IsValid());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1491 ASSERT_EQ(30, job.GetPriority());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1492 ASSERT_EQ(i2, job.GetId());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1493
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1494 ASSERT_TRUE(CheckState(registry, i2, Orthanc::JobState_Running));
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1495 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1496
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1497 ASSERT_TRUE(CheckState(registry, i2, Orthanc::JobState_Failure));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1498 ASSERT_TRUE(CheckState(registry, i3, Orthanc::JobState_Pending));
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1499
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1500 {
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1501 JobsRegistry::RunningJob job(registry, 0);
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1502 ASSERT_TRUE(job.IsValid());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1503 ASSERT_EQ(20, job.GetPriority());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1504 ASSERT_EQ(i3, job.GetId());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1505
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1506 job.MarkSuccess();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1507
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1508 ASSERT_TRUE(CheckState(registry, i3, Orthanc::JobState_Running));
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1509 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1510
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1511 ASSERT_TRUE(CheckState(registry, i3, Orthanc::JobState_Success));
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1512
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1513 {
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1514 JobsRegistry::RunningJob job(registry, 0);
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1515 ASSERT_TRUE(job.IsValid());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1516 ASSERT_EQ(10, job.GetPriority());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1517 ASSERT_EQ(i1, job.GetId());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1518 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1519
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1520 {
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1521 JobsRegistry::RunningJob job(registry, 0);
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1522 ASSERT_TRUE(job.IsValid());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1523 ASSERT_EQ(5, job.GetPriority());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1524 ASSERT_EQ(i4, job.GetId());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1525 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1526
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1527 {
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1528 JobsRegistry::RunningJob job(registry, 1);
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1529 ASSERT_FALSE(job.IsValid());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1530 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1531
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1532 Orthanc::JobState s;
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1533 ASSERT_TRUE(registry.GetState(s, i1));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1534 ASSERT_FALSE(registry.GetState(s, i2)); // Removed because oldest
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1535 ASSERT_FALSE(registry.GetState(s, i3)); // Removed because second oldest
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1536 ASSERT_TRUE(registry.GetState(s, i4));
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1537
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1538 registry.SetMaxCompletedJobs(1); // (*)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1539 ASSERT_FALSE(registry.GetState(s, i1)); // Just discarded by (*)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1540 ASSERT_TRUE(registry.GetState(s, i4));
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1541 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1542
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1543
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1544 TEST(JobsRegistry, Simultaneous)
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1545 {
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1546 JobsRegistry registry;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1547
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1548 std::string i1, i2;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1549 registry.Submit(i1, new DummyJob(), 20);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1550 registry.Submit(i2, new DummyJob(), 10);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1551
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1552 ASSERT_TRUE(CheckState(registry, i1, Orthanc::JobState_Pending));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1553 ASSERT_TRUE(CheckState(registry, i2, Orthanc::JobState_Pending));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1554
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1555 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1556 JobsRegistry::RunningJob job1(registry, 0);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1557 JobsRegistry::RunningJob job2(registry, 0);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1559 ASSERT_TRUE(job1.IsValid());
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1560 ASSERT_TRUE(job2.IsValid());
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1561
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1562 job1.MarkFailure();
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1563 job2.MarkSuccess();
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1564
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1565 ASSERT_TRUE(CheckState(registry, i1, Orthanc::JobState_Running));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1566 ASSERT_TRUE(CheckState(registry, i2, Orthanc::JobState_Running));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1567 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1568
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1569 ASSERT_TRUE(CheckState(registry, i1, Orthanc::JobState_Failure));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1570 ASSERT_TRUE(CheckState(registry, i2, Orthanc::JobState_Success));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1571 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1572
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1573
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1574 TEST(JobsRegistry, Resubmit)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1575 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1576 JobsRegistry registry;
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1577
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1578 std::string id;
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1579 registry.Submit(id, new DummyJob(), 10);
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1580
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1581 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending));
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1582
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1583 registry.Resubmit(id);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1584 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending));
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1585
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1586 {
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1587 JobsRegistry::RunningJob job(registry, 0);
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1588 ASSERT_TRUE(job.IsValid());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1589 job.MarkFailure();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1590
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1591 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running));
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1592
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1593 registry.Resubmit(id);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1594 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running));
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1595 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1596
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1597 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Failure));
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1598
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1599 registry.Resubmit(id);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1600 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending));
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1601
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1602 {
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1603 JobsRegistry::RunningJob job(registry, 0);
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1604 ASSERT_TRUE(job.IsValid());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1605 ASSERT_EQ(id, job.GetId());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1606
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1607 job.MarkSuccess();
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1608 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1609 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1610
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1611 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Success));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1612
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1613 registry.Resubmit(id);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1614 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Success));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1615 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1616
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1617
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1618 TEST(JobsRegistry, Retry)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1619 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1620 JobsRegistry registry;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1621
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1622 std::string id;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1623 registry.Submit(id, new DummyJob(), 10);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1624
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1625 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1626
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1627 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1628 JobsRegistry::RunningJob job(registry, 0);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1629 ASSERT_TRUE(job.IsValid());
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1630 job.MarkRetry(0);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1631
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1632 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1633 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1634
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1635 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Retry));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1636
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1637 registry.Resubmit(id);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1638 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Retry));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1639
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1640 registry.ScheduleRetries();
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1641 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1642
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1643 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1644 JobsRegistry::RunningJob job(registry, 0);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1645 ASSERT_TRUE(job.IsValid());
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1646 job.MarkSuccess();
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1647
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1648 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running));
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1649 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1650
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1651 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Success));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1652 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1653
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1654
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1655 TEST(JobsRegistry, PausePending)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1656 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1657 JobsRegistry registry;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1658
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1659 std::string id;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1660 registry.Submit(id, new DummyJob(), 10);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1661
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1662 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1663
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1664 registry.Pause(id);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1665 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Paused));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1666
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1667 registry.Pause(id);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1668 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Paused));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1669
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1670 registry.Resubmit(id);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1671 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Paused));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1672
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1673 registry.Resume(id);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1674 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending));
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1675 }
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1676
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1677
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1678 TEST(JobsRegistry, PauseRunning)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1679 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1680 JobsRegistry registry;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1681
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1682 std::string id;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1683 registry.Submit(id, new DummyJob(), 10);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1684
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1685 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1686
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1687 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1688 JobsRegistry::RunningJob job(registry, 0);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1689 ASSERT_TRUE(job.IsValid());
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1690
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1691 registry.Resubmit(id);
2562
1e66fe3ddf9f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2559
diff changeset
1692 job.MarkPause();
2558
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1693 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1694 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1695
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1696 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Paused));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1697
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1698 registry.Resubmit(id);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1699 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Paused));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1700
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1701 registry.Resume(id);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1702 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1703
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1704 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1705 JobsRegistry::RunningJob job(registry, 0);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1706 ASSERT_TRUE(job.IsValid());
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1707
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1708 job.MarkSuccess();
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1709 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1710 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1711
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1712 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Success));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1713 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1714
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1715
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1716 TEST(JobsRegistry, PauseRetry)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1717 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1718 JobsRegistry registry;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1719
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1720 std::string id;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1721 registry.Submit(id, new DummyJob(), 10);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1722
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1723 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1724
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1725 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1726 JobsRegistry::RunningJob job(registry, 0);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1727 ASSERT_TRUE(job.IsValid());
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1728
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1729 job.MarkRetry(0);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1730 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1731 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1732
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1733 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Retry));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1734
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1735 registry.Pause(id);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1736 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Paused));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1737
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1738 registry.Resume(id);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1739 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Pending));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1740
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1741 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1742 JobsRegistry::RunningJob job(registry, 0);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1743 ASSERT_TRUE(job.IsValid());
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1744
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1745 job.MarkSuccess();
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1746 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Running));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1747 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1748
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1749 ASSERT_TRUE(CheckState(registry, id, Orthanc::JobState_Success));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2557
diff changeset
1750 }