annotate UnitTestsSources/MultiThreadingTests.cpp @ 2557:b4516a6f214b jobs

state machine
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 03 May 2018 13:45:31 +0200
parents 91e944c8389b
children 57f81b988713
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
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
277 #include <boost/date_time/posix_time/posix_time.hpp>
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
278 #include <queue>
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
279
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
280 namespace Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
281 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
282 enum JobState
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
283 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
284 JobState_Pending,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
285 JobState_Running,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
286 JobState_Success,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
287 JobState_Failure,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
288 JobState_Paused,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
289 JobState_Retry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
290 };
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
291
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
292 enum JobStepStatus
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
293 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
294 JobStepStatus_Success,
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
295 JobStepStatus_Failure,
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
296 JobStepStatus_Continue,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
297 JobStepStatus_Retry
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
298 };
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
299
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
300
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
301 class JobStepResult
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
302 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
303 private:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
304 JobStepStatus status_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
305
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
306 public:
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
307 explicit JobStepResult(JobStepStatus status) :
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
308 status_(status)
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
309 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
310 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
311
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
312 virtual ~JobStepResult()
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
313 {
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 JobStepStatus GetStatus() const
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
317 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
318 return status_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
319 }
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
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
323 class RetryResult : public JobStepResult
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
324 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
325 private:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
326 unsigned int timeout_; // Retry after "timeout_" milliseconds
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
327
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
328 public:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
329 RetryResult(unsigned int timeout) :
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
330 JobStepResult(JobStepStatus_Retry),
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
331 timeout_(timeout)
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
332 {
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 unsigned int GetTimeout() const
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
336 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
337 return timeout_;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
338 }
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 class IJob : public boost::noncopyable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
343 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
344 public:
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
345 virtual ~IJob()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
346 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
347 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
348
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
349 virtual JobStepResult* ExecuteStep() = 0;
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
350
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
351 virtual void ReleaseResources() = 0; // For pausing jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
352
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
353 virtual float GetProgress() = 0;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
354
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
355 virtual void FormatStatus(Json::Value& value) = 0;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
356 };
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
357
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
358
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
359 class JobHandler : public boost::noncopyable
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
360 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
361 private:
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
362 std::string id_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
363 JobState state_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
364 std::auto_ptr<IJob> job_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
365 int priority_; // "+inf()" means highest priority
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
366 boost::posix_time::ptime creationTime_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
367 boost::posix_time::ptime lastUpdateTime_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
368 boost::posix_time::ptime retryTime_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
369 uint64_t runtime_; // In milliseconds
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
370 bool pauseScheduled_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
371
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
372 void SetStateInternal(JobState state)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
373 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
374 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
375
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
376 if (state_ == JobState_Running)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
377 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
378 runtime_ += (now - lastUpdateTime_).total_milliseconds();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
379 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
380
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
381 state_ = state;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
382 lastUpdateTime_ = now;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
383 pauseScheduled_ = false;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
384 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
385
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
386 public:
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
387 JobHandler(IJob* job,
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
388 int priority) :
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
389 id_(Toolbox::GenerateUuid()),
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
390 state_(JobState_Pending),
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
391 job_(job),
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
392 priority_(priority),
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
393 creationTime_(boost::posix_time::microsec_clock::universal_time()),
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
394 lastUpdateTime_(creationTime_),
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
395 runtime_(0),
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
396 pauseScheduled_(false)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
397 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
398 if (job == NULL)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
399 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
400 throw OrthancException(ErrorCode_NullPointer);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
401 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
402 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
403
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
404 const std::string& GetId() const
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
405 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
406 return id_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
407 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
408
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
409 IJob& GetJob() const
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
410 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
411 assert(job_.get() != NULL);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
412 return *job_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
413 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
414
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
415 void SetPriority(int priority)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
416 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
417 priority_ = priority;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
418 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
419
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
420 int GetPriority() const
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
421 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
422 return priority_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
423 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
424
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
425 JobState GetState() const
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
426 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
427 return state_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
428 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
429
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
430 void SetState(JobState state)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
431 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
432 if (state == JobState_Retry)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
433 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
434 // Use "SetRetryState()"
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
435 throw OrthancException(ErrorCode_BadSequenceOfCalls);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
436 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
437 else
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
438 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
439 SetStateInternal(state);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
440 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
441 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
442
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
443 void SetRetryState(unsigned int timeout)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
444 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
445 if (state_ == JobState_Running)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
446 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
447 SetStateInternal(JobState_Retry);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
448 retryTime_ = (boost::posix_time::microsec_clock::universal_time() +
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
449 boost::posix_time::milliseconds(timeout));
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
450 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
451 else
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
452 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
453 // Only valid for running jobs
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
454 throw OrthancException(ErrorCode_BadSequenceOfCalls);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
455 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
456 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
457
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
458 void SchedulePause()
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
459 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
460 if (state_ == JobState_Running)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
461 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
462 pauseScheduled_ = true;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
463 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
464 else
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
465 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
466 // Only valid for running jobs
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
467 throw OrthancException(ErrorCode_BadSequenceOfCalls);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
468 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
469 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
470
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
471 bool IsPauseScheduled()
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
472 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
473 return pauseScheduled_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
474 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
475
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
476 bool IsRetryReady(const boost::posix_time::ptime& now) const
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
477 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
478 if (state_ != JobState_Retry)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
479 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
480 throw OrthancException(ErrorCode_BadSequenceOfCalls);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
481 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
482 else
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
483 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
484 return retryTime_ >= now;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
485 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
486 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
487 };
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
488
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
489
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
490 class JobsMonitor : public boost::noncopyable
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
491 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
492 private:
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
493 struct PriorityComparator
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
494 {
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
495 bool operator() (JobHandler*& a,
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
496 JobHandler*& b) const
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
497 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
498 return a->GetPriority() < b->GetPriority();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
499 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
500 };
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
501
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
502 typedef std::map<std::string, JobHandler*> JobsIndex;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
503 typedef std::list<const JobHandler*> CompletedJobs;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
504 typedef std::set<JobHandler*> RetryJobs;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
505 typedef std::priority_queue<JobHandler*,
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
506 std::vector<JobHandler*>, // Could be a "std::deque"
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
507 PriorityComparator> PendingJobs;
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 boost::mutex mutex_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
510 JobsIndex jobsIndex_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
511 PendingJobs pendingJobs_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
512 CompletedJobs completedJobs_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
513 RetryJobs retryJobs_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
514
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
515 boost::condition_variable pendingJobAvailable_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
516 size_t maxCompletedJobs_;
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 #ifndef NDEBUG
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
520 bool IsPendingJob(const JobHandler& job) const
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
521 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
522 PendingJobs copy = pendingJobs_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
523 while (!copy.empty())
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
524 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
525 if (copy.top() == &job)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
526 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
527 return true;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
528 }
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
529
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
530 copy.pop();
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
533 return false;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
534 }
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 bool IsCompletedJob(const JobHandler& job) const
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 for (CompletedJobs::const_iterator it = completedJobs_.begin();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
539 it != completedJobs_.end(); ++it)
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
540 {
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
541 if (*it == &job)
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
542 {
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
543 return true;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
544 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
545 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
546
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
547 return false;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
548 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
549
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
550 bool IsRetryJob(JobHandler& job) const
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
551 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
552 return retryJobs_.find(&job) != retryJobs_.end();
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 #endif
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
557 void CheckInvariants()
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 #ifndef NDEBUG
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 PendingJobs copy = pendingJobs_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
562 while (!copy.empty())
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 assert(copy.top()->GetState() == JobState_Pending);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
565 copy.pop();
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
566 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
567 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
568
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
569 assert(completedJobs_.size() <= maxCompletedJobs_);
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 for (CompletedJobs::const_iterator it = completedJobs_.begin();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
572 it != completedJobs_.end(); ++it)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
573 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
574 assert((*it)->GetState() == JobState_Success ||
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
575 (*it)->GetState() == JobState_Failure);
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
578 for (RetryJobs::const_iterator it = retryJobs_.begin();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
579 it != retryJobs_.end(); ++it)
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 assert((*it)->GetState() == JobState_Retry);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
582 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
583
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
584 for (JobsIndex::iterator it = jobsIndex_.begin();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
585 it != jobsIndex_.end(); ++it)
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 JobHandler& job = *it->second;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
588
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
589 assert(job.GetId() == it->first);
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 switch (job.GetState())
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
592 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
593 case JobState_Pending:
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
594 assert(!IsRetryJob(job) && IsPendingJob(job) && !IsCompletedJob(job));
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
595 break;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
596
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
597 case JobState_Success:
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
598 case JobState_Failure:
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
599 assert(!IsRetryJob(job) && !IsPendingJob(job) && IsCompletedJob(job));
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
600 break;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
601
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
602 case JobState_Retry:
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
603 assert(IsRetryJob(job) && !IsPendingJob(job) && !IsCompletedJob(job));
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
604 break;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
605
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
606 case JobState_Running:
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
607 case JobState_Paused:
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
608 assert(!IsRetryJob(job) && !IsPendingJob(job) && !IsCompletedJob(job));
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
609 break;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
610
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
611 default:
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
612 throw OrthancException(ErrorCode_InternalError);
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 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
615 #endif
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
616 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
617
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
618
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
619 void ForgetOldCompletedJobs()
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
620 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
621 if (maxCompletedJobs_ != 0)
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
622 {
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
623 while (completedJobs_.size() > maxCompletedJobs_)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
624 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
625 assert(completedJobs_.front() != NULL);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
626
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
627 std::string id = completedJobs_.front()->GetId();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
628
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
629 assert(jobsIndex_.find(id) != jobsIndex_.end());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
630
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
631 jobsIndex_.erase(id);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
632 delete(completedJobs_.front());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
633 completedJobs_.pop_front();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
634 }
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
635 }
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
636 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
637
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
638
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
639 void MarkRunningAsCompleted(JobHandler& job,
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
640 bool success)
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 boost::mutex::scoped_lock lock(mutex_);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
643 CheckInvariants();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
644 assert(job.GetState() == JobState_Running);
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 job.SetState(success ? JobState_Success : JobState_Failure);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
647
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
648 completedJobs_.push_back(&job);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
649 ForgetOldCompletedJobs();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
650
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
651 CheckInvariants();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
652 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
653
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
654
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
655 void MarkRunningAsRetry(JobHandler& job,
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
656 unsigned int timeout)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
657 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
658 boost::mutex::scoped_lock lock(mutex_);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
659 CheckInvariants();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
660
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
661 assert(job.GetState() == JobState_Running &&
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
662 retryJobs_.find(&job) == retryJobs_.end());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
663
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
664 retryJobs_.insert(&job);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
665 job.SetRetryState(timeout);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
666
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
667 CheckInvariants();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
668 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
669
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
670
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
671 void MarkRunningAsPaused(JobHandler& job)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
672 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
673 boost::mutex::scoped_lock lock(mutex_);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
674 CheckInvariants();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
675 assert(job.GetState() == JobState_Running);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
676
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
677 job.SetState(JobState_Paused);
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 CheckInvariants();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
680 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
681
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
682
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
683 JobHandler* WaitPendingJob(unsigned int timeout)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
684 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
685 boost::mutex::scoped_lock lock(mutex_);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
686
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
687 while (pendingJobs_.empty())
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
688 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
689 if (timeout == 0)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
690 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
691 pendingJobAvailable_.wait(lock);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
692 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
693 else
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
694 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
695 bool success = pendingJobAvailable_.timed_wait
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
696 (lock, boost::posix_time::milliseconds(timeout));
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
697 if (!success)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
698 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
699 return NULL;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
700 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
701 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
702 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
703
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
704 JobHandler* job = pendingJobs_.top();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
705 pendingJobs_.pop();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
706
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
707 job->SetState(JobState_Running);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
708 return job;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
709 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
710
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
711
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
712 public:
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
713 JobsMonitor() :
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
714 maxCompletedJobs_(10)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
715 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
716 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
717
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
718
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
719 ~JobsMonitor()
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
720 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
721 for (JobsIndex::iterator it = jobsIndex_.begin(); it != jobsIndex_.end(); ++it)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
722 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
723 assert(it->second != NULL);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
724 delete it->second;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
725 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
726 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
727
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
728
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
729 void SetMaxCompletedJobs(size_t i)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
730 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
731 boost::mutex::scoped_lock lock(mutex_);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
732 CheckInvariants();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
733
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
734 maxCompletedJobs_ = i;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
735 ForgetOldCompletedJobs();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
736
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
737 CheckInvariants();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
738 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
739
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
740
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
741 void ListJobs(std::set<std::string>& target)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
742 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
743 boost::mutex::scoped_lock lock(mutex_);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
744 CheckInvariants();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
745
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
746 for (JobsIndex::const_iterator it = jobsIndex_.begin();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
747 it != jobsIndex_.end(); ++it)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
748 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
749 target.insert(it->first);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
750 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
751 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
752
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
753
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
754 void Submit(std::string& id,
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
755 IJob* job, // Takes ownership
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
756 int priority)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
757 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
758 std::auto_ptr<JobHandler> handler(new JobHandler(job, priority));
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
759
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
760 boost::mutex::scoped_lock lock(mutex_);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
761 CheckInvariants();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
762
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
763 id = handler->GetId();
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
764 pendingJobs_.push(handler.get());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
765 jobsIndex_.insert(std::make_pair(id, handler.release()));
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
766
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
767 pendingJobAvailable_.notify_one();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
768
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
769 CheckInvariants();
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
770 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
771
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
772
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
773 void Submit(IJob* job, // Takes ownership
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
774 int priority)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
775 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
776 std::string id;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
777 Submit(id, job, priority);
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
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
781 void SetPriority(const std::string& id,
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
782 int priority)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
783 {
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
784 boost::mutex::scoped_lock lock(mutex_);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
785 CheckInvariants();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
786
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
787 JobsIndex::iterator found = jobsIndex_.find(id);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
788
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
789 if (found == jobsIndex_.end())
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
790 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
791 LOG(WARNING) << "Unknown job: " << id;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
792 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
793 else
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
794 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
795 found->second->SetPriority(priority);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
796
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
797 if (found->second->GetState() == JobState_Pending)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
798 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
799 // If the job is pending, we need to reconstruct the
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
800 // priority queue, as the heap condition has changed
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
801
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
802 PendingJobs copy;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
803 std::swap(copy, pendingJobs_);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
804
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
805 assert(pendingJobs_.empty());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
806 while (!copy.empty())
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
807 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
808 pendingJobs_.push(copy.top());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
809 copy.pop();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
810 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
811 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
812 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
813
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
814 CheckInvariants();
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
815 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
816
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
817
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
818 void Pause(const std::string& id)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
819 {
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
820 boost::mutex::scoped_lock lock(mutex_);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
821 CheckInvariants();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
822
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
823 JobsIndex::iterator found = jobsIndex_.find(id);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
824
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
825 if (found == jobsIndex_.end())
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
826 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
827 LOG(WARNING) << "Unknown job: " << id;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
828 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
829 else
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
830 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
831 switch (found->second->GetState())
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
832 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
833 case JobState_Pending:
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
834 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
835 // If the job is pending, we need to reconstruct the
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
836 // priority queue to remove it
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
837 PendingJobs copy;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
838 std::swap(copy, pendingJobs_);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
839
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
840 assert(pendingJobs_.empty());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
841 while (!copy.empty())
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
842 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
843 if (copy.top()->GetId() != id)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
844 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
845 pendingJobs_.push(copy.top());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
846 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
847
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
848 copy.pop();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
849 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
850
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
851 found->second->SetState(JobState_Paused);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
852
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
853 break;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
854 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
855
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
856 case JobState_Retry:
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
857 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
858 RetryJobs::iterator item = retryJobs_.find(found->second);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
859 assert(item != retryJobs_.end());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
860 retryJobs_.erase(item);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
861
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
862 found->second->SetState(JobState_Paused);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
863
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
864 break;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
865 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
866
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
867 case JobState_Paused:
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
868 case JobState_Success:
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
869 case JobState_Failure:
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
870 // Nothing to be done
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
871 break;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
872
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
873 case JobState_Running:
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
874 found->second->SchedulePause();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
875 break;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
876
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
877 default:
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
878 throw OrthancException(ErrorCode_InternalError);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
879 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
880 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
881
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
882 CheckInvariants();
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
883 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
884
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
885
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
886 void Resume(const std::string& id)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
887 {
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
888 boost::mutex::scoped_lock lock(mutex_);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
889 CheckInvariants();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
890
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
891 JobsIndex::iterator found = jobsIndex_.find(id);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
892
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
893 if (found == jobsIndex_.end())
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
894 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
895 LOG(WARNING) << "Unknown job: " << id;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
896 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
897 else if (found->second->GetState() != JobState_Paused)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
898 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
899 LOG(WARNING) << "Cannot resume a job that is not paused: " << id;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
900 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
901 else
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
902 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
903 found->second->SetState(JobState_Pending);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
904 pendingJobs_.push(found->second);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
905 pendingJobAvailable_.notify_one();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
906 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
907
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
908 CheckInvariants();
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
909 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
910
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
911
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
912 void Resubmit(const std::string& id)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
913 {
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
914 boost::mutex::scoped_lock lock(mutex_);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
915 CheckInvariants();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
916
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
917 JobsIndex::iterator found = jobsIndex_.find(id);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
918
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
919 if (found == jobsIndex_.end())
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
920 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
921 LOG(WARNING) << "Unknown job: " << id;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
922 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
923 else if (found->second->GetState() != JobState_Failure)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
924 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
925 LOG(WARNING) << "Cannot resubmit a job that has not failed: " << id;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
926 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
927 else
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
928 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
929 bool ok = false;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
930 for (CompletedJobs::iterator it = completedJobs_.begin();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
931 it != completedJobs_.end(); ++it)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
932 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
933 if (*it == found->second)
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 ok = true;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
936 completedJobs_.erase(it);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
937 break;
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
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
941 assert(ok);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
942
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
943 found->second->SetState(JobState_Pending);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
944 pendingJobs_.push(found->second);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
945 pendingJobAvailable_.notify_one();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
946 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
947
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
948 CheckInvariants();
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
949 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
950
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
951
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
952 void ScheduleRetries()
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 boost::mutex::scoped_lock lock(mutex_);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
955 CheckInvariants();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
956
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
957 RetryJobs copy;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
958 std::swap(copy, retryJobs_);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
959
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
960 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
961
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
962 assert(retryJobs_.empty());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
963 for (RetryJobs::iterator it = copy.begin(); it != copy.end(); ++it)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
964 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
965 if ((*it)->IsRetryReady(now))
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
966 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
967 (*it)->SetState(JobState_Pending);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
968 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
969 else
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
970 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
971 retryJobs_.insert(*it);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
972 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
973 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
974
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
975 CheckInvariants();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
976 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
977
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
978
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
979 bool GetState(JobState& state,
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
980 const std::string& id)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
981 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
982 boost::mutex::scoped_lock lock(mutex_);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
983 CheckInvariants();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
984
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
985 JobsIndex::const_iterator it = jobsIndex_.find(id);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
986 if (it == jobsIndex_.end())
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
987 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
988 return false;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
989 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
990 else
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
991 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
992 state = it->second->GetState();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
993 return true;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
994 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
995 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
996
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
997
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
998 class RunningJob : public boost::noncopyable
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
999 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
1000 private:
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1001 JobsMonitor& that_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1002 JobHandler* handler_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1003 JobState targetState_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1004 unsigned int retryTimeout_;
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
1005
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
1006 public:
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1007 RunningJob(JobsMonitor& that,
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1008 unsigned int timeout) :
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1009 that_(that),
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1010 handler_(NULL),
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1011 targetState_(JobState_Failure),
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1012 retryTimeout_(0)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1013 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1014 handler_ = that_.WaitPendingJob(timeout);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1015 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1016
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1017 ~RunningJob()
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
1018 {
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1019 if (IsValid())
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1020 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1021 switch (targetState_)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1022 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1023 case JobState_Failure:
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1024 that_.MarkRunningAsCompleted(*handler_, false);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1025 break;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1026
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1027 case JobState_Success:
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1028 that_.MarkRunningAsCompleted(*handler_, true);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1029 break;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1030
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1031 case JobState_Paused:
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1032 that_.MarkRunningAsPaused(*handler_);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1033 break;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1034
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1035 case JobState_Retry:
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1036 that_.MarkRunningAsRetry(*handler_, retryTimeout_);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1037 break;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1038
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1039 default:
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1040 assert(0);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1041 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1042 }
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
1043 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
1044
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
1045 bool IsValid() const
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
1046 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
1047 return handler_ != NULL;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
1048 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
1049
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1050 const std::string& GetId() const
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1051 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1052 if (IsValid())
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1053 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1054 return handler_->GetId();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1055 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1056 else
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1057 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1058 throw OrthancException(ErrorCode_BadSequenceOfCalls);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1059 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1060 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1061
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1062 int GetPriority() const
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1063 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1064 if (IsValid())
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1065 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1066 return handler_->GetPriority();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1067 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1068 else
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1069 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1070 throw OrthancException(ErrorCode_BadSequenceOfCalls);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1071 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1072 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1073
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1074 bool IsPauseScheduled()
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1075 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1076 if (!IsValid())
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1077 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1078 throw OrthancException(ErrorCode_BadSequenceOfCalls);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1079 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1080
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1081 boost::mutex::scoped_lock lock(that_.mutex_);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1082 that_.CheckInvariants();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1083 assert(handler_->GetState() == JobState_Running);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1084
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1085 return handler_->IsPauseScheduled();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1086 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1087
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1088 IJob& GetJob()
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1089 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1090 if (!IsValid())
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1091 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1092 throw OrthancException(ErrorCode_BadSequenceOfCalls);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1093 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1094
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1095 boost::mutex::scoped_lock lock(that_.mutex_);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1096 that_.CheckInvariants();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1097 assert(handler_->GetState() == JobState_Running);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1098
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1099 return handler_->GetJob();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1100 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1101
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1102 void MarkSuccess()
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1103 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1104 if (!IsValid())
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1105 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1106 throw OrthancException(ErrorCode_BadSequenceOfCalls);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1107 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1108
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1109 targetState_ = JobState_Success;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1110 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1111
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1112 void MarkFailure()
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1113 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1114 if (!IsValid())
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1115 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1116 throw OrthancException(ErrorCode_BadSequenceOfCalls);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1117 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1118
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1119 targetState_ = JobState_Failure;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1120 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1121
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1122 void MarkPause()
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1123 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1124 if (!IsValid())
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1125 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1126 throw OrthancException(ErrorCode_BadSequenceOfCalls);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1127 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1128
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1129 targetState_ = JobState_Paused;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1130 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1131
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1132 void MarkRetry(unsigned int timeout)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1133 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1134 if (!IsValid())
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1135 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1136 throw OrthancException(ErrorCode_BadSequenceOfCalls);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1137 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1138
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1139 targetState_ = JobState_Retry;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1140 retryTimeout_ = timeout;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1141 }
2556
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
1142 };
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
1143 };
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
1144 }
2557
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1145
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1146
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1147
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1148 class DummyJob : public Orthanc::IJob
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1149 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1150 private:
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1151 JobStepResult result_;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1152
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1153 public:
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1154 DummyJob() :
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1155 result_(Orthanc::JobStepStatus_Success)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1156 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1157 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1158
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1159 explicit DummyJob(JobStepResult result) :
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1160 result_(result)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1161 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1162 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1163
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1164 virtual JobStepResult* ExecuteStep()
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1165 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1166 return new JobStepResult(result_);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1167 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1168
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1169 virtual void ReleaseResources()
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1170 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1171 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1172
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1173 virtual float GetProgress()
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1174 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1175 return 0;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1176 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1177
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1178 virtual void FormatStatus(Json::Value& value)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1179 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1180 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1181 };
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1182
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1183
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1184 static bool CheckState(Orthanc::JobsMonitor& monitor,
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1185 const std::string& id,
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1186 Orthanc::JobState state)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1187 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1188 Orthanc::JobState s;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1189 if (monitor.GetState(s, id))
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1190 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1191 return state == s;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1192 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1193 else
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1194 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1195 return false;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1196 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1197 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1198
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1199
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1200 TEST(JobsMonitor, Priority)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1201 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1202 JobsMonitor monitor;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1203
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1204 std::string i1, i2, i3, i4;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1205 monitor.Submit(i1, new DummyJob(), 10);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1206 monitor.Submit(i2, new DummyJob(), 30);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1207 monitor.Submit(i3, new DummyJob(), 20);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1208 monitor.Submit(i4, new DummyJob(), 5);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1209
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1210 monitor.SetMaxCompletedJobs(2);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1211
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1212 std::set<std::string> id;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1213 monitor.ListJobs(id);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1214
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1215 ASSERT_EQ(4u, id.size());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1216 ASSERT_TRUE(id.find(i1) != id.end());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1217 ASSERT_TRUE(id.find(i2) != id.end());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1218 ASSERT_TRUE(id.find(i3) != id.end());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1219 ASSERT_TRUE(id.find(i4) != id.end());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1220
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1221 ASSERT_TRUE(CheckState(monitor, i2, Orthanc::JobState_Pending));
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1222
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1223 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1224 JobsMonitor::RunningJob job(monitor, 0);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1225 ASSERT_TRUE(job.IsValid());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1226 ASSERT_EQ(30, job.GetPriority());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1227 ASSERT_EQ(i2, job.GetId());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1228
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1229 ASSERT_TRUE(CheckState(monitor, i2, Orthanc::JobState_Running));
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1230 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1231
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1232 ASSERT_TRUE(CheckState(monitor, i2, Orthanc::JobState_Failure));
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1233 ASSERT_TRUE(CheckState(monitor, i3, Orthanc::JobState_Pending));
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1234
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1235 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1236 JobsMonitor::RunningJob job(monitor, 0);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1237 ASSERT_TRUE(job.IsValid());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1238 ASSERT_EQ(20, job.GetPriority());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1239 ASSERT_EQ(i3, job.GetId());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1240
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1241 job.MarkSuccess();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1242
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1243 ASSERT_TRUE(CheckState(monitor, i3, Orthanc::JobState_Running));
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1244 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1245
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1246 ASSERT_TRUE(CheckState(monitor, i3, Orthanc::JobState_Success));
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1247
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1248 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1249 JobsMonitor::RunningJob job(monitor, 0);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1250 ASSERT_TRUE(job.IsValid());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1251 ASSERT_EQ(10, job.GetPriority());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1252 ASSERT_EQ(i1, job.GetId());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1253 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1254
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1255 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1256 JobsMonitor::RunningJob job(monitor, 0);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1257 ASSERT_TRUE(job.IsValid());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1258 ASSERT_EQ(5, job.GetPriority());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1259 ASSERT_EQ(i4, job.GetId());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1260 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1261
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1262 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1263 JobsMonitor::RunningJob job(monitor, 1);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1264 ASSERT_FALSE(job.IsValid());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1265 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1266
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1267 Orthanc::JobState s;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1268 ASSERT_TRUE(monitor.GetState(s, i1));
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1269 ASSERT_FALSE(monitor.GetState(s, i2)); // Removed because oldest
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1270 ASSERT_FALSE(monitor.GetState(s, i3)); // Removed because second oldest
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1271 ASSERT_TRUE(monitor.GetState(s, i4));
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1272
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1273 monitor.SetMaxCompletedJobs(1); // (*)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1274 ASSERT_FALSE(monitor.GetState(s, i1)); // Just discarded by (*)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1275 ASSERT_TRUE(monitor.GetState(s, i4));
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1276 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1277
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1278
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1279 TEST(JobsMonitor, Resubmit)
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1280 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1281 JobsMonitor monitor;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1282
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1283 std::string id;
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1284 monitor.Submit(id, new DummyJob(), 10);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1285
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1286 ASSERT_TRUE(CheckState(monitor, id, Orthanc::JobState_Pending));
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1287
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1288 monitor.Resubmit(id);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1289 ASSERT_TRUE(CheckState(monitor, id, Orthanc::JobState_Pending));
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1290
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1291 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1292 JobsMonitor::RunningJob job(monitor, 0);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1293 ASSERT_TRUE(job.IsValid());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1294 job.MarkFailure();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1295
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1296 ASSERT_TRUE(CheckState(monitor, id, Orthanc::JobState_Running));
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1297
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1298 monitor.Resubmit(id);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1299 ASSERT_TRUE(CheckState(monitor, id, Orthanc::JobState_Running));
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1300 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1301
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1302 ASSERT_TRUE(CheckState(monitor, id, Orthanc::JobState_Failure));
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1303
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1304 monitor.Resubmit(id);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1305 ASSERT_TRUE(CheckState(monitor, id, Orthanc::JobState_Pending));
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1306
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1307 {
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1308 JobsMonitor::RunningJob job(monitor, 0);
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1309 ASSERT_TRUE(job.IsValid());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1310 ASSERT_EQ(id, job.GetId());
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1311
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1312 job.MarkSuccess();
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1313 ASSERT_TRUE(CheckState(monitor, id, Orthanc::JobState_Running));
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1314 }
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1315
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1316 ASSERT_TRUE(CheckState(monitor, id, Orthanc::JobState_Success));
b4516a6f214b state machine
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2556
diff changeset
1317 }